Quantcast
Channel: Tweaks
Viewing all articles
Browse latest Browse all 66

How to Merge PDF Files using SAP.. absolutely Free?

$
0
0

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:

  1. Merging two PDFs from external source
  2. Merging two SAP Adobe forms
  3. 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:

  1. Install PDFTK and the required class – Please go through the attached link and perform the steps as given. Merge PDF files in ABAP
  2. Create class ZCL_PDF_MERGE using the SAP PDF Merge Nugget
  3. Write the driver program to implement this functionality.

Below are the steps to be performed in the driver program:

  1. 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.
  2. 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.
  3. 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.
  4. 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).
  5. 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 


Viewing all articles
Browse latest Browse all 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>