Skip to Content
Technical Articles

Convert images into PDF and Merge with an existing Adobe forms

It’s very common business case where we need to add an image (jpg/bmp etc.) in an adobe form. We can use Image Field UI and bind the required image. But how we can merge an image in existing forms in Adobe? Recently, one of community members asked such kind of scenario and possibilities. Their requirement was to attach the images saved in PO and display along with PO Output Form. If there is any image files (mostly in “.jpg”) saved at Purchase Order, these should be merged with PO Output form and send it back to vendor. So I explored this with following use cases:

  • Convert a JPG file into PDF Raw Data.
  • Merge the Image raw data with existing Adobe form.

Let’s explore the technical set-up to achieve above requirements.

Step 1:

Create an Adobe form ZPB_TEST_CONVERT_JPG, Interface ZPB_TEST_CONVERT_JPG with an import parameter as GV_CONTENT of type XSTRING.

Step 2:

In context of form, create a Graphics Node as follow:

Step 3:

Set the graphics node properties as follow:

Step 4:

Create an Image field UI in form layout and bind it with GRAPHICS node created in the context.

We are done with creation of 1st Adobe form in which any image from driver program can be uploaded as PDF and then it needs to be merged with any existing Adobe form. I’ve already created another Adobe form ZPB_HIDE_TABLE_COLUMN with simple table display. I will use that in this tutorial.

Step 5:

Create a driver program with the following steps:

  • Start Form Processing Job as:
ie_outputparams-getpdf    = 'X'.
ie_outputparams-nodialog  = 'X'. " suppress printer dialog popup

CALL FUNCTION 'FP_JOB_OPEN'
  CHANGING
    ie_outputparams = ie_outputparams.
  • Generate FM for 1st Adobe form:
    CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
      EXPORTING
        i_name     = 'ZPB_TEST_CONVERT_JPG'
      IMPORTING
        e_funcname = i_funcname.
  • Upload an image file from local system.
  cl_gui_frontend_services=>file_open_dialog(
     CHANGING
       file_table              = file_table
       rc                      = rc
          ).
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

  READ TABLE file_table INTO DATA(ls_file) INDEX 1.
  IF sy-subrc = 0.
    filename = ls_file-filename.
  ENDIF.

  cl_gui_frontend_services=>gui_upload(
   EXPORTING
     filename                = filename
     filetype                = 'BIN'
   IMPORTING
     filelength              = filelength
    CHANGING
      data_tab                = data_tab
         ).
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
  • Convert Binary data into raw data :
  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = filelength
    IMPORTING
      buffer       = gv_content
    TABLES
      binary_tab   = data_tab.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
  • Call FM for 1st Adobe Form from step 1 and collect it’s raw data into an internal table GT_PDF:
DATA: gt_pdf     TYPE TABLE OF xstring.

CALL FUNCTION i_funcname
  EXPORTING
    /1bcdwb/docparams  = fp_docparams
    iv_content         = gv_content
  IMPORTING
    /1bcdwb/formoutput = fp_formoutput
  EXCEPTIONS
    usage_error        = 1
    system_error       = 2
    internal_error     = 3.

IF sy-subrc = 0.
  APPEND fp_formoutput-pdf TO gt_pdf.
ENDIF.
  • Call 2nd Adobe form in which we want to merge the image as pdf and collect it’s raw data in the internal table GT_PDF:
  CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
    EXPORTING
      i_name     = 'ZPB_HIDE_TABLE_COLUMN'
    IMPORTING
      e_funcname = gv_fm_name.

  CLEAR: fp_formoutput.
  CALL FUNCTION gv_fm_name
    EXPORTING
      /1bcdwb/docparams  = fp_docparams
      it_details         = gt_details
    IMPORTING
      /1bcdwb/formoutput = fp_formoutput
    EXCEPTIONS
      usage_error        = 1
      system_error       = 2
      internal_error     = 3.
  IF sy-subrc = 0.
    APPEND fp_formoutput-pdf TO gt_pdf.
  ENDIF.
  • Now, merge both the pdf into one PDF:
  CREATE OBJECT lo_cl_merge.

  LOOP AT gt_pdf INTO DATA(lv_pdf_data).
    lo_cl_merge->add_document( lv_pdf_data ).
  ENDLOOP.

  lo_cl_merge->merge_documents( IMPORTING merged_document = gv_merge ).

LO_CL_MERGE is an object of class cl_rspo_pdf_merge. For more details about this class and other functionalities, please refer the below link:

https://sapyard.com/how-to-merge-pdf-files-using-sap-absolutely-free/

  • Close the form processing as:
DATA ls_result TYPE sfpjoboutput.

CALL FUNCTION 'FP_JOB_CLOSE'
  IMPORTING
    e_result       = ls_result
  EXCEPTIONS
    usage_error    = 1
    system_error   = 2
    internal_error = 3
    OTHERS         = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
  • Now, you can generate the result merged PDF (GV_MERGE) from above step g with the following code:
DATA: path              TYPE string,
      fullpath          TYPE string,
      default_extension TYPE string VALUE 'PDF'.

cl_gui_frontend_services=>file_save_dialog(
EXPORTING
default_extension = default_extension
CHANGING
filename = filename
path = path
fullpath = fullpath ).

CHECK fullpath IS NOT INITIAL.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer     = gv_merge
  TABLES
    binary_tab = data_tab.
cl_gui_frontend_services=>gui_download(
EXPORTING
filename = filename
filetype = 'BIN'
CHANGING
data_tab = data_tab ).
cl_gui_frontend_services=>execute(
EXPORTING
document = filename ).

 

Let’s execute the driver program, select an image file from your local system ( I preferred to choose SAP image ) and save the merged pdf in your local drive:

First Page of PDF generated as SAP Image in PDF

Second page of the generated PDF as an existing Adobe Form ( Sample Table Data ):

Be the first to leave a comment
You must be Logged on to comment or reply to a post.