How To Merge Multiple PDF Forms Into Single One and Write in Application Serve1
How To Merge Multiple PDF Forms Into Single One and Write in Application Serve1
Today I will try to explain how we can merge multiple PDF forms output into single
one and write the same PDF file into application server.
Brief requirement: The real life scenario demanded to send a bundle of different SAP
system generated PDF forms to a third party document repository ( has access to the
SAP application server ) . The form bundle contains different forms like cover letter,
loan agreement, direct debit, third party authorization, terms and condition forms etc.
In the below example we will see how the 5 different PDF forms are getting stitched
into one. Here is the sample code of the driver program. Just use it to play and observe
the behavior:
Code Block 1: Data Declaration. Below 5 constants are the different PDF forms created
in the system.
*&&-- Below are the 5 different Adobe form names created for Contract
CONSTANTS: lc_form_cover TYPE fpname VALUE
'ZFI_CP_COVER_LETTER',
lc_form_agreement TYPE fpname VALUE
'ZFI_CP_LOAN_AGREEMENT',
lc_form_debit TYPE fpname VALUE
'ZFI_CP_DIRECT_DEBIT',
lc_form_third_party TYPE fpname VALUE 'ZFI_CP_THIRD_PARTY',
lc_form_terms TYPE fpname VALUE
'ZFI_CP_TERMS_CONDITION'.
Code Block 2: Populate the multiple form names into internal table and pass desired
values to output parameters to trigger the form.
*&&-- Populate the multiple form names into an internal table
DATA(lt_forms) = VALUE lty_t_forms( ( form = lc_form_cover )
( form = lc_form_agreement )
( form = lc_form_debit )
( form = lc_form_third_party )
( form = lc_form_terms ) ).
lwa_outputparams-nodialog = abap_true.
lwa_outputparams-dest = 'LOCL'.
lwa_outputparams-getpdf = 'M'.
lwa_outputparams-bumode = 'M'. " Bundle Mode Multiple
Code Block 3: Trigger the 5 forms one by one inside LOOP. The interface import
parameters are same for 5 forms. You can mark import parameters in the interface as
optional so that different parameters can be used for different forms. Creating single
interface will be easier to maintain.
*&--------------------------------------------------------------------
-*
*& Form Processing: Call Form - Open
*&--------------------------------------------------------------------
-*
CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
ie_outputparams = lwa_outputparams
EXCEPTIONS
cancel = 1
usage_error = 2
system_error = 3
internal_error = 4
OTHERS = 5.
IF sy-subrc <> 0.
" Suitable Error Handling
ENDIF.
CATCH cx_fp_api.
ENDTRY.
IF sy-subrc <> 0.
ENDIF.
CLEAR: lwa_forms, lwa_formoutput, lv_fm_name.
ENDLOOP.
Code Block 4: Merge form’s PDF output into one. Internal table LT_FORMOUTPUT
will contain 5 rows with PDF data in XSTRING format for 5 different forms.
Code Block 5: Write the PDF file to application directory. The variable
LV_MERGED_DOCUMENT contains the merged XSTRING value of those 5 forms.
REFRESH: lt_formoutput.
lwa_formoutput-pdf = lv_merged_document.
*PDF Upload
IF lwa_formoutput-pdf IS NOT INITIAL.
lt_pdfcontent = cl_document_bcs=>xstring_to_solix( ip_xstring =
lwa_formoutput-pdf ).
IF sy-subrc = 0.
REFRESH lt_pdfcontent.
ENDIF.
ENDIF.
OUTPUT:
File has been written in application server successfully. This file can be downloaded in
PDF format using CG3Y transaction from application directory for verification. You
will not be able to open this PDF file directly from AL11.
Note: During the test, it was triggering error/merge failure message at the time of
merging. You may need to implement the below OSS note to resolve the error related to
class CL_RSPO_PDF_MERGE.
In the above example, we can send the output to the Spool as well. Just pass the below
parameters in the code lines of code block 2 , followed by code block 3.
lwa_outputparams-nodialog = abap_true.
lwa_outputparams-device = 'PRINTER'.
lwa_outputparams-reqnew = abap_true.
lwa_outputparams-dest = 'LOCL'.
lwa_outputparams-bumode = 'M'. " Bundle Mode Multiple
Spool request with single PDF will be generated. For our example, 36 pages have been
generated for 5 forms.
Once you open the PDF in spool, you can see the 5 forms. You can check one by one by
clicking arrow sign or you can click “Overall View On” to get a merged view.