How to generate PDF file and attach it to VF03

Usually, no need to generate a PDF file and attach it to Invoice VF03 cause standard output processing already cover this functionality including output generate/print/preview, output entry log, output reprocessing, etc.

What if the user wants to generate one customized PDF file which out of the scope of output processing and needs to attach it to the Invoice document at VF03?

If the PDF file already exists, then the below article can be used as a reference:

If the PFD file does not exist yet, its prerequisite steps are very similar to creating one form and sending it out with the PDF file as an attachment.

1, Getting PDF content from Form output

Fetch the PDF content in X-STRING format from return parameter /1BCDWB/FORMOUTPUT-PDF with setting Form Processing Output Parameter SFPOUTPUTPARAMS-GETPDF equal to ‘X’ when calling function module ‘FP_JOB_OPEN’. Refer to the below articles for this step.

2, Format conversion for PDF content

  • Convert PDF content from X-STRING format to BINARY using FM:‘SCMS_XSTRING_TO_BINARY’;
  • Convert Binary format from SOLIX_TAB type  to SOLI type using FM: ‘SO_SOLIXTAB_TO_SOLITAB’;

3, Create an attachment object and link to INVOICE

  • Get folder ID by FM: ‘SO_FOLDER_ROOT_ID_GET’;
  • Create SAP office object by FM:’SO_OBJECT_INSERT‘;
  • Create the relationship between the attachment object and the invoice by FM:’BINARY_RELATION_CREATE_COMMIT‘.
  • To display the attachment list as POPUP window can use FM:‘GOS_EXECUTE_SERVICE

The simple code is shown below:

FORM attach_pdf USING ip_pdf TYPE fpcontent. DATA: pdf_content TYPE solix_tab, ls_fol_id TYPE soodk, ls_obj_id TYPE soodk, ls_obj_data TYPE sood1, ls_folmem_k TYPE sofmk, ls_note TYPE borident, ls_object TYPE borident, lv_ep_note TYPE borident-objkey, lv_offset TYPE i, l_subrc TYPE subrc, it_objhead TYPE STANDARD TABLE OF soli, it_content LIKE STANDARD TABLE OF soli, wa_content LIKE soli. * convert attach document CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = ip_pdf TABLES binary_tab = pdf_content. CALL FUNCTION 'SO_SOLIXTAB_TO_SOLITAB' EXPORTING ip_solixtab = pdf_content[] IMPORTING ep_solitab = it_content[]. * CALL FUNCTION 'SO_CONVERT_CONTENTS_BIN'
* EXPORTING
* it_contents_bin = pdf_content[]
* IMPORTING
* et_contents_bin = it_content[]. ls_object-objkey = gs_header-vbeln. "invoice no. ls_object-objtype = 'VBRK'. CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET' EXPORTING region = 'B' IMPORTING folder_id = ls_fol_id EXCEPTIONS OTHERS = 1. * Attributes of Attached PDF ls_obj_data-objsns = 'O'. ls_obj_data-objla = sy-langu. ls_obj_data-file_ext = 'PDF'. ls_obj_data-objlen = lines( it_content ) * 255. * set File name ... ls_obj_data-objdes = 'customized_file_name.pdf'. CALL FUNCTION 'SO_OBJECT_INSERT' EXPORTING folder_id = ls_fol_id object_type = 'EXT' object_hd_change = ls_obj_data IMPORTING object_id = ls_obj_id TABLES objhead = it_objhead objcont = it_content EXCEPTIONS active_user_not_exist = 35 folder_not_exist = 6 object_type_not_exist = 17 owner_not_exist = 22 parameter_error = 23 OTHERS = 1000. IF sy-subrc = 0 AND ls_object-objkey IS NOT INITIAL. ls_folmem_k-foltp = ls_fol_id-objtp. ls_folmem_k-folyr = ls_fol_id-objyr. ls_folmem_k-folno = ls_fol_id-objno. ls_folmem_k-doctp = ls_obj_id-objtp. ls_folmem_k-docyr = ls_obj_id-objyr. ls_folmem_k-docno = ls_obj_id-objno. lv_ep_note = ls_folmem_k. ls_note-objtype = 'MESSAGE'. ls_note-objkey = lv_ep_note. CALL FUNCTION 'BINARY_RELATION_CREATE_COMMIT' EXPORTING obj_rolea = ls_object obj_roleb = ls_note relationtype = 'ATTA' EXCEPTIONS OTHERS = 1. IF sy-subrc = 0. ... ENDIF. ENDIF. ENDFORM. *To display attach list at Popup window clear ls_object. ls_object-objkey = vbrk-vbeln. ls_object-objtype = 'VBRK'. "display attachment list CALL FUNCTION 'GOS_EXECUTE_SERVICE' EXPORTING ip_service = 'VIEW_ATTA' is_object = ls_object ip_no_commit = 'X' ip_popup = 'X' IP_RWMOD = 'D'.

Please find below blog for more general-purpose: