Are you tired of using free online PDF merger tools available on internet with lots of unwanted advertisements and limitations? Ever tried merging PDFs using SAP? No worries this tutorial will help you to merge multiple PDF using SAP.
There can be 3 common possibilities to merge PDFs:
- Merging two PDFs from external source
- Merging two SAP Adobe forms
- Merging an SAP Adobe form and a PDF from an external source. Note: ADS doesn’t support merging PDFs yet, so its tricky one.
A. Merging two PDFs from external source
First case is pretty simple and we have a standard SAP program to help us. Program – RSPO_TEST_MERGE_PDF_FILES.
Just you need to select the PDFs and execute the program and Ta-Da!
your merged file is ready.
Also Read: How I used SAP Adobe Form as my personal PDF editor?
B. Merging two SAP Adobe Forms or Merging an SAP Adobe Form and a PDF From an external source.
Now comes the tricky part. How to merge two or more SAP Adobe forms or an Adobe form with an external PDF?
For this we have PDFTK(PDF Tool Kit) as our savior. It is an external tool. You need some help from Basis and Security team to install the tool and get the required authorizations. So have good terms with your Basis and Security friends.. SAP is a team game and you cannot win it alone.
Let’s begin merging..
Here are steps that you need to perform before writing your code:
- Install PDFTK and the required class – Please go through the attached link and perform the steps as given. Merge PDF files in ABAP
- Create class ZCL_PDF_MERGE using the SAP PDF Merge Nugget
- Write the driver program to implement this functionality.
Below are the steps to be performed in the driver program:
- Get all the PDF content (ADS, PDF from presentation server, PDF from application server, PDF from DMS server or any other source) in XSTRING format.
- Append all the XSTRING PDFs into one internal table IT_PDF. Loop IT_PDF and call ADD_PDF() method of the class ZCL_PDF_MERGE . This will create temporary PDFs in your application server path ZPDFMERGE created by BASIS team.
- Finally, call GET_MERGED() method which will create the final merged PDF and return the final PDF XSTRING in the variable v_pdf_merged, also it will delete all the temporary files those were created above.
- Now you have the final merged PDF data in XSTRING format and you can use it as per your requirement (to send mails, to save in presentation server or application server , to show on the SAP screen just like an adobe form output).
- Below is an example code snippet to merge two adobe form with a PDF from the application server and then save it in the presentation server.
Also Check:How to Email Smartform as PDF Attachment to Multiple Users?
Driver Program Code Snippet:
Prerequisites- Authorization object S_LOG_COM should be assigned to you and also you should have ZPDFMERGE path access.
REPORT zsapyard_pdf_merge.
PARAMETERS : p_path TYPE sdok_filnm. ” app server path
DATA: lo_pdfmerge TYPE REF TO zcl_pdf_merge,
v_pdf_merged TYPE xstring,
v_pdf TYPE xstring,
it_pdf TYPE STANDARD TABLE OF xstring,
wa_formoutput1 TYPE fpformoutput,
wa_outputparams TYPE sfpoutputparams,
wa_docparams TYPE sfpdocparams,
wa_formoutput2 TYPE fpformoutput,
v_formname1 TYPE fpname,
v_formname2 TYPE fpname,
v_function_name1 TYPE rs38l_fnam,
v_function_name2 TYPE rs38l_fnam,
bin_tab TYPE STANDARD TABLE OF tabl1024,
lo_gui TYPE REF TO cl_gui_frontend_services,
path TYPE string,
fullpath TYPE string,
length TYPE i,
filter TYPE string,
uact TYPE i,
name TYPE string.
*first adobe form.
v_formname1 = ‘ZTEST1’.
*get function module names for Adobe forms
TRY.
CALL FUNCTION ‘FP_FUNCTION_MODULE_NAME’
EXPORTING
i_name = v_formname1
IMPORTING
e_funcname = v_function_name1.
* e_interface_type = lv_interface_type1.
CATCH cx_fp_api_internal
cx_fp_api_repository
cx_fp_api_usage.
* MOVE sy-subrc TO lv_subrc.
ENDTRY.
* second adobe form
v_formname2 = ‘ZTEST2’.
TRY.
CALL FUNCTION ‘FP_FUNCTION_MODULE_NAME’
EXPORTING
i_name = v_formname2
IMPORTING
e_funcname = v_function_name2.
* e_interface_type = lv_interface_type2.
CATCH cx_fp_api_internal
cx_fp_api_repository
cx_fp_api_usage.
* MOVE sy-subrc TO lv_subrc.
ENDTRY.
wa_outputparams-nodialog = ‘X’.
wa_outputparams-preview = abap_true.
wa_outputparams-reqnew = ‘X’.
wa_outputparams-nopdf = ‘X’.
wa_outputparams-arcmode = ‘1’.
* Set parameters (print/send/output device etc)
CALL FUNCTION ‘FP_JOB_OPEN’
CHANGING
ie_outputparams = wa_outputparams
EXCEPTIONS
cancel = 1
usage_error = 2
system_error = 3
internal_error = 4
OTHERS = 5.
IF sy-subrc <> 0.
* MOVE sy-subrc TO lv_subrc.
ENDIF.
wa_docparams-langu = ‘E’.
wa_docparams-replangu2 = ‘E’.
CALL FUNCTION v_function_name1
EXPORTING
/1bcdwb/docparams = wa_docparams
IMPORTING
/1bcdwb/formoutput = wa_formoutput1 “1st adobe form
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
* MOVE sy-subrc TO lv_subrc.
ENDIF.
CALL FUNCTION v_function_name2
EXPORTING
/1bcdwb/docparams = wa_docparams
IMPORTING
/1bcdwb/formoutput = wa_formoutput2 “2nd adobe form
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
* MOVE sy-subrc TO lv_subrc.
ENDIF.
*get external source PDF data from the application server
OPEN DATASET p_path FOR INPUT IN BINARY MODE.
IF sy-subrc EQ 0.
READ DATASET p_path INTO v_pdf.
IF sy-subrc EQ 0.
ENDIF.
ENDIF.
CLOSE DATASET p_path.
* create table with all xstring data
APPEND v_pdf TO it_pdf.
v_pdf = wa_formoutput1-pdf.
APPEND v_pdf TO it_pdf.
v_pdf = wa_formoutput2-pdf.
APPEND v_pdf TO it_pdf.
* use ZCL_PDF_MERGE class to get the PDF xstring after merging all PDF’s
CREATE OBJECT lo_pdfmerge.
LOOP AT it_pdf INTO v_pdf.
lo_pdfmerge->add_pdf( v_pdf ).
ENDLOOP.
v_pdf_merged = lo_pdfmerge->get_merged( ). “final merged data
*download merged PDF to your desktop
CREATE OBJECT lo_gui.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = v_pdf_merged
IMPORTING
output_length = length
TABLES
binary_tab = bin_tab.
CALL METHOD lo_gui->file_save_dialog
EXPORTING
default_extension = ‘pdf’
default_file_name = ‘merged.pdf’
file_filter = filter
CHANGING
filename = name
path = path
fullpath = fullpath
user_action = uact.
IF uact = lo_gui->action_cancel.
EXIT.
ENDIF.
lo_gui->gui_download( EXPORTING
filename = fullpath
filetype = ‘BIN’
bin_filesize = length
CHANGING
data_tab = bin_tab ).
And, your merged file is ready. You can also create a spool request for this or show this on your screen as per your requirement.
We have a very active Telegram (App) SAP Technical Group with more than 3600+ SAP Technical Practitioners from 6 Continents of the SAP World. Please join it using below link.
Telegram SAP Technical Discuss Group. You need to install the Telegram App first on your mobile device. Once you have it on your mobile, you can join the group and also access it from the Web on your computer and laptop.
Check some Tweaks
- Automatic Population of Values during Table Maintenance
- Expensive SQL Statements
- Some Tips
- Transport ABAP Report Variants into a Work Bench Request
- Dynamic Where Condition usage in Database queries
- License Key Tables in SAP
- Steps to stop debugger at ELM BADI CRM_MKTLIST_BADI from Web UI Screen
- Retrofit in SAP
- Save the data in Customizing table without using transport number
- Download excel file with leading zeroes using GUI_DOWNLOAD
- How to get Technical Type of BOM
- How to enable table entries maintenance in SE16N (Alternative for &SAP_EDIT and UASE16N)
- Pop Up Debugging
- Sample program to unlock the editor of a program.
- Trick to adjust the variants
- New line ‘#’ character at the end of every line in SAP Application Server File
- Unwanted error: Maintenance planning plant xxxx not permitted
- Decode the Standard Service Catalog Item of Service Master (ASMD-STLVPOS)
- Unpack the PACKNO of Service Entry Sheet (SES)
- Issue in opening Notepad attachment using class CL_FITV_GOS=>GET_CONTENT
- FMs giving you tough time in debugging?
- Is data element WDY_BOOLEAN and Flag (Char1) same for Web Dynpro ALV?
- Can you really restrict any developer from executing any t-code?
- Can we avoid Table Type declaration for Attributes section in Web Dynpro?
- How I used SAP Adobe Form as my personal PDF editor
- Just a key and two clicks for ALV consistency check
- Simple SAP Security Breach
- Unwanted character ‘#’ in the short text print outs and reports
- Create & Change Variants without Fire Fighter
- Delete Foreign Lock Entries in Debug
- Disable User Personalization in Web Dynpro Screen
- Maths in ABAP
- SAP ABAP Tips
- SAP HANA Tips
- Creating Dynamic Internal Table
- Mastering SAP Debugging
- Is data element BOOLE_D and CHAR1 same in SAP Selection Screen?
- How to update custom field of PRPS table
- GPS like tool in SAP using Google Map API
- Lazy and Smart ABAPers
- Logical Lock Vs Physical Lock in SAP
- Know Who is doing What in your SAP System
- How to upload the PDF format directly into Adobe form layout
- Real Time Exchange Rate with Real Time Data Using Yahoo Finance API
- Applying Enterprise Integration Patterns in SAP ABAP
- External Debugging of an Application of another SAP User in other Location in another Machine/System
- How to Restore SAP Number Range Object?
- How to Get Accurate Pricing in SD for Customer and Material?
- How to Add and Change PO Service Line using BAPI BAPI_PO_CHANGE?
- SAP CoPilot – SAP’s own Digital Assistant Introduction