ABAP Blog Posts
cancel
Showing results for 
Search instead for 
Did you mean: 
Gxalmet
Newcomer
405

Business Requirements

The business needs to implement a print preview functionality that:

  • Allows users to select multiple documents in a Fiori report

  • Merges all selected documents into a single PDF file

  • Enables users to print all selected documents at once

 

Implementation Steps

Backend Implementation: RAP Action with PDF Generation

  • Behavior Definition
action PrintPreview result [1] zspdfgeneration ;
  • Action Implementation
METHOD PrintPreview.
**** Generate the pdf 
....
....
....
    LOOP AT keys into data(keyLine).

      CALL FUNCTION 'FP_JOB_OPEN'
        CHANGING   ie_outputparams = ls_outputparams
        EXCEPTIONS cancel          = 1
                   usage_error     = 2
                   system_error    = 3
                   internal_error  = 4
                   OTHERS          = 5.

      " Call the Function Module Name for the Adobe Form
      TRY.
          CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
            EXPORTING i_name     = lv_function
            IMPORTING e_funcname = lv_funcname.

          CALL FUNCTION lv_funcname
            EXPORTING  /1bcdwb/docparams   = ls_docparams
....
            IMPORTING  /1bcdwb/formoutput  = ls_formout
            EXCEPTIONS usage_error         = 1
                       system_error        = 2
                       internal_error      = 3
                       OTHERS              = 4.

          ls_meta-title = |YourPrintFileName| & |{ key }|.

          DATA(lv_field_name) = |NameOfYourFile| & |{ {key} }_| & |{ sy-datum }_{ sy-uzeit }.pdf|.

          lv_field_name = condense( val  = lv_field_name
                                    from = ` `
                                    to   = `` ).

          ls_lheader-name  = 'Content-Disposition'.
          ls_lheader-value = |inline; filename={fileName}| & |{ ls_transport_order-TransportationOrderUUID }_|.
          TRY.
              " Create PDF Object.
              lo_fp = cl_fp=>get_reference( ).
              lo_pdfobj = lo_fp->create_pdf_object( connection = 'ADS' ).
              lo_pdfobj->set_document( pdfdata = ls_formout-pdf ).
              lo_pdfobj->set_metadata( metadata = ls_meta ).
              lo_pdfobj->execute( ).

              " Get the PDF content back with title
              lo_pdfobj->get_document( IMPORTING pdfdata = DATA(lv_value) ).
              lo_merger->add_document( ls_formout-pdf ).
              CLEAR lv_value.
            CATCH cx_fp_runtime_internal
                  cx_fp_runtime_system
                  cx_fp_runtime_usage INTO lx_fpex.
          ENDTRY.
        CATCH cx_root.
      ENDTRY.
    ENDLOOP.
   TRY.
        " Merge both documents and receive the result
        DATA(lv_merged_PDF) = lo_merger->merge_documents( ).
      CATCH cx_rspo_pdf_merger INTO DATA(l_exception). " TODO: variable is assigned but never used (ABAP cleaner)
        " Add a useful error handling here
    ENDTRY.
    DATA lv_base64 TYPE string.
    CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
      EXPORTING input  = lv_merged_PDF
      IMPORTING output = lv_base64.


    READ TABLE keys INTO DATA(keyOne) INDEX 1.
 result = VALUE #( ( %key-DOcType         = keyOne-DOcType
                        %key-WBOriginDoc     = keyOne-WBOriginDoc
                        %key-WBOriginDocItem = keyOne-WBOriginDocItem
                        %param               = VALUE zspdfgenration( field_name  = lv_field_name
mime_type   = 'application/pdf'
pdf_content = lv_base64 ) ) ).

ENDMETHOD.​
  • Metadata Annotation with ChangeSet
: {
  lineItem: [{
    type: #FOR_ACTION,
    dataAction: 'PrintPreview',
    label: 'Print Preview',
    invocationGrouping: #ChangeSet
  }]
}

Frontend Implementation: Controller Extension

  • Generate a controller extension and a custom action to call our print preview v4 action.
PrintPreviewV4: async function () {

const oExtensionAPI = this.base.getExtensionAPI();
const aSelectedContexts = oExtensionAPI.getSelectedContexts();
// Message if no rows selected
if (!aSelectedContexts || aSelectedContexts.length === 0) {
MessageToast.show("No rows selected.");
return;
}
// Call the action with the rows selected in only one call
try {
const oPageController = this.base.getView().getController();
const oResultContext = await oPageController.editFlow.invokeAction(
      "com.sap.gateway.srvd.yourService.YourAction",
      contexts: aSelectedContexts ,
      invocationGrouping: "ChangeSet"
}
);

const oData = oResultContext[0]?.value?.oBinding?.oCachePromise?.getResult()?.oPromise?.getResult();
// With the file then you can allow preview to the user or download directly 

},​

Conclusion

This implementation provides a solution for generating merged PDF previews from multiple selected documents in Fiori applications. The combination of RAP actions with ChangeSet grouping and proper frontend handling ensures good performance and user experience.