Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Merge multiple files(any type) into one PDF.

anger007
Discoverer
0 Kudos
3,563

Hi Scholars,

I am trying to select multiple files(any type) and merge them into one PDF file.

As I've coded the IMAGES are converted and merged successfully but when I am trying to merge PDF files into PDF

they merged successfully but the content(data/only blank pages are visible) of the PDF files are not showing in merged PDF.

Here, is the my code:

DATA: pdf_merger TYPE REF TO cl_rspo_pdf_merge.

DATA: gt_display TYPE t_displayoption.

DATA: merged_document TYPE xstring.
DATA: rc TYPE i VALUE 0, lnum TYPE i VALUE 0, p_sel TYPE i VALUE 1.
DATA: docindex TYPE i VALUE 0, errordoc TYPE xstring.
DATA: answer TYPE c VALUE '1'.
DATA: gt_pdf TYPE TABLE OF xstring.

CREATE OBJECT pdf_merger.
***

PERFORM get_pdffiles CHANGING lnum.

PERFORM merge_pdfs USING gt_display CHANGING merged_document docindex errordoc rc.
IF rc = 0.

PERFORM put_merged_file USING merged_document.

ENDIF.

**

FORM get_pdffiles CHANGING p_lnum TYPE i.
DATA: ie_outputparams TYPE sfpoutputparams,
i_funcname TYPE funcname,
file_table TYPE filetable,
filename TYPE string,
filelength TYPE i,
data_tab TYPE STANDARD TABLE OF tabl1024,
gv_content TYPE xstring,
fp_docparams TYPE sfpdocparams,
fp_formoutput TYPE fpformoutput,
e_pdf_table TYPE tfpcontent.

ie_outputparams-getpdf = 'X'.
ie_outputparams-nodialog = 'X'.

CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
ie_outputparams = ie_outputparams.


CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
EXPORTING
i_name = 'ZPB_CONVERT_JPG'
IMPORTING
e_funcname = i_funcname.

cl_gui_frontend_services=>file_open_dialog(
EXPORTING
window_title = 'Select Files'
multiselection = abap_true
CHANGING
file_table = file_table
rc = rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5 ).

LOOP AT file_table INTO DATA(wa_file_table).

filename = wa_file_table-filename.
p_lnum = p_lnum + 1.

cl_gui_frontend_services=>gui_upload(
EXPORTING
filename = filename
filetype = 'BIN'
IMPORTING
filelength = filelength
CHANGING
data_tab = data_tab
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19 ).

CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = filelength
IMPORTING
buffer = gv_content
TABLES
binary_tab = data_tab.

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


IF sy-subrc = 0.
APPEND fp_formoutput-pdf TO gt_pdf.
ENDIF.

WRITE: / wa_file_table(50) COLOR COL_NORMAL, 'Len', filelength.

CLEAR: wa_file_table, gv_content, filename,
filelength, data_tab, fp_formoutput.
ENDLOOP.

ENDFORM.

**

FORM merge_pdfs USING p_gt_display TYPE t_displayoption
CHANGING p_merged_document
p_docindex
p_errordoc
p_rc.

LOOP AT gt_pdf INTO DATA(wa_gt_pdf).
pdf_merger->add_document( wa_gt_pdf ).
CLEAR: wa_gt_pdf.
ENDLOOP.

pdf_merger->merge_documents( IMPORTING merged_document = p_merged_document rc = p_rc ).

CLEAR pdf_merger.

ENDFORM.

**

FORM put_merged_file USING p_merged_document.
DATA: bin_tab TYPE STANDARD TABLE OF tabl1024.
DATA: lo_gui TYPE REF TO cl_gui_frontend_services.
DATA: path TYPE string, fullpath TYPE string.
DATA: length TYPE i.
DATA: filter TYPE string, uact TYPE i, name TYPE string.

CREATE OBJECT lo_gui.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = p_merged_document
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
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24 ).

ENDFORM.

So, what is the problem? Plz tell me.

Thanks & Regards.

1 REPLY 1

harishankar714
Participant
0 Kudos
2,849

It seems that the code you posted is using the class CL_RSPO_PDF_MERGE to merge multiple PDF files into one. This class provides methods to add individual PDF documents and then merge them into a single document.

However, There could be multiple reasons for this issue, such as:

  • Incorrect handling of the individual PDF files before merging them.
  • Incorrect handling of the merged document after merging the PDF files.
  • Error in the merging process, possibly due to the size of the PDF files being too large.