Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
MiljanDjuric
Explorer
426

Our report required the ability to save or print a bundle of PDF forms, each provided as an entry in an internal table. Initially, we attempted to use the standard approach with the function module FP_GET_PDF_TABLE. However, we encountered limitations specifically, FP_JOB_OPEN was unable to handle the necessary parameters for bundled output in our scenario.

As we did not have the option to modify the Adobe forms themselves or apply relevant SAP Notes, we explored an alternative solution. By leveraging the CL_RSPO_PDF_MERGE class, we successfully implemented PDF merging at runtime. This approach provided a stable and effective way to combine individual PDFs into a single document, ready for download or direct printing.

This is a sample code that provide this functionality:

TYPES: ty_documents_tab TYPE STANDARD TABLE OF ty_document WITH DEFAULT KEY.

FORM generate_merged_pdf USING pt_documents TYPE ty_documents_tab.

  DATA: lt_binary_pdf     TYPE TABLE OF x255,
        lt_merged_pdf     TYPE TABLE OF x255,
        lv_xstring        TYPE xstring,
        lv_merged_xstring TYPE xstring,
        lv_filename       TYPE string,
        lv_path           TYPE string,
        lv_fullpath       TYPE string,
        ls_formoutput     TYPE fpformoutput,
        ls_joboutput      TYPE sfpjoboutput.
*------------------------------------------------------------
* Field symbols
*------------------------------------------------------------
  FIELD-SYMBOLS: <fs_document> TYPE ty_document.
*------------------------------------------------------------
* form and doc parametars
*------------------------------------------------------------
  DATA: l_fm_name    TYPE rs38l_fnam,
        fp_docparams TYPE sfpdocparams,
        lv_formname  TYPE fpname.
*------------------------------------------------------------
* Set Adobe Form output parameters
*------------------------------------------------------------
* Initialize PDF merger class
*------------------------------------------------------------
  TRY.
      DATA(lo_merger) = NEW cl_rspo_pdf_merge( ).
    CATCH cx_rspo_pdf_merge INTO DATA(lo_error).
      MESSAGE 'PDF merger initialization failed.' TYPE 'E'.
      RETURN.
  ENDTRY.
*------------------------------------------------------------
* Open Adobe Forms spool job
*------------------------------------------------------------
  CALL FUNCTION 'FP_JOB_OPEN'
    CHANGING
      ie_outputparams = fp_outputparams
    EXCEPTIONS
      OTHERS          = 1.
*------------------------------------------------------------
* Loop through entries and generate individual PDFs
*------------------------------------------------------------
  LOOP AT lt_documents ASSIGNING <fs_document>. 
*------------------------------------------------------------
* Set form name 
*------------------------------------------------------------
    lv_formname = 'Z_YOUR_PDF_FORM'.
*------------------------------------------------------------
* Get generated function module name
*------------------------------------------------------------
    CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
      EXPORTING
        i_name     = lv_formname
      IMPORTING
        e_funcname = l_fm_name.
*------------------------------------------------------------
* Set form parameters
*------------------------------------------------------------
    CLEAR fp_docparams.
    fp_docparams-langu   = sy-langu.
    fp_docparams-country = 'XX'.
*------------------------------------------------------------
* Call form generation
*------------------------------------------------------------
    CALL FUNCTION l_fm_name
      EXPORTING
        /1bcdwb/docparams  = fp_docparams
        i_data             = <fs_document>
      IMPORTING
        /1bcdwb/formoutput = ls_formoutput
      EXCEPTIONS
        OTHERS             = 1.
*------------------------------------------------------------
* Add document to merger
*------------------------------------------------------------
    lv_xstring = ls_formoutput-pdf.
    lo_merger->add_document( lv_xstring ).
  ENDLOOP.
*------------------------------------------------------------
* Close Adobe Forms spool job
*------------------------------------------------------------
  CALL FUNCTION 'FP_JOB_CLOSE'
    IMPORTING
      e_result = ls_joboutput
    EXCEPTIONS
      OTHERS   = 1.
*------------------------------------------------------------
* Merge all added PDF documents
*------------------------------------------------------------
  DATA lv_rc TYPE i.
  lo_merger->merge_documents(
    IMPORTING
      merged_document = lv_merged_xstring
      rc              = lv_rc ).

  CHECK lv_rc = 0.
*------------------------------------------------------------
* Convert final merged XSTRING to binary
*------------------------------------------------------------
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = lv_merged_xstring
    TABLES
      binary_tab = lt_merged_pdf.
*------------------------------------------------------------
* Prompt user for file location
*------------------------------------------------------------
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    CHANGING
      filename = lv_filename
      path     = lv_path
      fullpath = lv_fullpath
    EXCEPTIONS
      OTHERS   = 1.

  IF sy-subrc <> 0.
    MESSAGE 'File path selection cancelled.' TYPE 'I'.
    RETURN.
  ENDIF.
*------------------------------------------------------------
* Download the merged PDF to selected path
*------------------------------------------------------------
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename = lv_fullpath
      filetype = 'BIN'
    TABLES
      data_tab = lt_merged_pdf.

ENDFORM.

In addition, parameters can be assigned to fp_outputparams to suppress the print dialog or to prefill predefined values, enabling a more streamlined and user-friendly output process. If no parameters are specified, the system will apply the default print settings.