*& Copia pirata de Report RSPO_TEST_MERGE_PDF_FILES
report z_xx_prg_merge_pdf_files.
types: begin of t_fileinfo,
filename type string,
data type xstring,
len type i,
end of t_fileinfo.
types: begin of t_displayoption,
page_mode type i,
page_layout type i,
hide_toolbar type i,
hide_menubar type i,
end of t_displayoption.
types t_fileinfotab type standard table of t_fileinfo with key filename.
data: pdf_merger type ref to cl_rspo_pdf_merge.
data: ex type ref to cx_rspo_pdf_merge, lv_ex_txt type string.
data: gt_pdffiles type t_fileinfotab.
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: text1 type string, error_text type string.
data: answer type c value '1'.
selection-screen begin of block b01 WITH FRAME.
PARAMETERS: p_dir type text255 OBLIGATORY DEFAULT '/SAPINTERFACES/NASA/Enviados',
p_mask type text50 OBLIGATORY default '1EG*.*'.
SELECTION-SCREEN end of block b01.
selection-screen begin of block opts with frame title title4.
parameters: dir radiobutton group sele default 'X',
file radiobutton group sele.
selection-screen skip.
selection-screen end of block opts.
selection-screen begin of block params with frame title title1.
selection-screen begin of block pagemode with frame title title2.
parameters: none radiobutton group pmod default 'X',
outline radiobutton group pmod,
thumbs radiobutton group pmod,
fullscr radiobutton group pmod,
oc radiobutton group pmod,
attachm radiobutton group pmod.
selection-screen skip.
selection-screen end of block pagemode.
selection-screen begin of block layout with frame title title3.
parameters: single radiobutton group plyt default 'X',
column radiobutton group plyt,
tcoll radiobutton group plyt,
tcolr radiobutton group plyt,
tpagl radiobutton group plyt,
tpagr radiobutton group plyt.
selection-screen skip.
selection-screen end of block layout.
parameters: p_hd_tb as checkbox default ' ',
p_hd_mb as checkbox default ' ',
p_hd_pc as checkbox default 'X'.
selection-screen skip.
selection-screen end of block params.
at SELECTION-SCREEN OUTPUT.
loop at SCREEN.
check screen-name np '*P_DIR*' and
screen-name np '*P_MASK*'.
screen-input = 0.
screen-invisible = 1.
modify screen.
endloop.
initialization.
title1 = 'Acrobat Reader display options for merged PDF'(001). "#EC TEXT_DIFF
title2 = 'Page Mode'(002). "#EC TEXT_DIFF
title3 = 'Page Layout'(003). "#EC TEXT_DIFF
title4 = 'Select option of PDF files'(005). "#EC TEXT_DIFF
end-of-selection.
text1 = 'Select input directory'(004). "#EC TEXT_DIFF
* Exception should be catched. Kernel must support PDF merge.
try.
create object pdf_merger.
catch cx_rspo_pdf_merge into ex.
lv_ex_txt = ex->get_text( ).
write: / lv_ex_txt color col_negative.
rc = 1.
return.
endtry.
* Get selection option for PDF files
if dir = 'X'.
p_sel = 1.
endif.
if file = 'X'.
p_sel = 0.
endif.
perform get_pdffiles using p_sel changing gt_pdffiles lnum.
if lnum = 0.
write: / 'No files specified, leaving program.'(006) color col_total.
exit.
endif.
describe table gt_pdffiles lines sy-tabix.
write: / 'Number of PDF Files:'(007), sy-tabix color col_total.
* Get display options of merged PDF: page mode
if none = 'X'.
gt_display-page_mode = 0.
endif.
if outline = 'X'.
gt_display-page_mode = 1.
endif.
if thumbs = 'X'.
gt_display-page_mode = 2.
endif.
if fullscr = 'X'.
gt_display-page_mode = 3.
endif.
if oc = 'X'.
gt_display-page_mode = 4.
endif.
if tpagl = 'X'.
gt_display-page_mode = 5.
endif.
if attachm = 'X'.
gt_display-page_mode = 5.
endif.
* Get display options of merged PDF: page layout
if single = 'X'.
gt_display-page_layout = 0.
endif.
if column = 'X'.
gt_display-page_layout = 1.
endif.
if tcoll = 'X'.
gt_display-page_layout = 2.
endif.
if tcolr = 'X'.
gt_display-page_layout = 3.
endif.
if tpagl = 'X'.
gt_display-page_layout = 4.
endif.
if tpagr = 'X'.
gt_display-page_layout = 5.
endif.
* Get display options of merged PDF: hide toolbar
if p_hd_tb = 'X'.
gt_display-hide_toolbar = 1.
else.
gt_display-hide_toolbar = 0.
endif.
* Get display options of merged PDF: hide menubar
if p_hd_mb = 'X'.
gt_display-hide_menubar = 1.
else.
gt_display-hide_menubar = 0.
endif.
* Return code should be checked: Only 0 means success
perform merge_pdfs using gt_pdffiles gt_display changing pdf_merger merged_document error_text docindex errordoc rc.
if rc = 0.
perform put_merged_file using merged_document.
else.
write: / 'Error: merge_pdfs returns '(008), rc, ' : ', error_text color col_negative.
write: / 'Failed document index '(009), docindex.
* Download failing document to check
call function 'POPUP_TO_CONFIRM'
exporting
titlebar = 'Ungültige PDF-Datei'(010)
text_question = 'Soll die PDF-Datei, die den Fehler verursacht, gespeichert werden?'(011)
text_button_1 = 'Ja'(014)
text_button_2 = 'Nein'(015)
default_button = '1'
display_cancel_button = ' '
importing
answer = answer
exceptions
others = 0.
if answer = '1'.
perform put_failed_file using errordoc.
endif.
endif.
form put_merged_file using merged_document type xstring.
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 = 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 ).
endform.
form put_failed_file using document type xstring.
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 = document
importing
output_length = length
tables
binary_tab = bin_tab.
call method lo_gui->file_save_dialog
exporting
default_extension = 'pdf'
default_file_name = 'failure.pdf'
file_filter = filter
changing
filename = name
path = path
fullpath = fullpath
user_action = uact.
if sy-subrc <> 0.
if sy-msgid is initial or sy-msgty is initial.
message e022(po) with sy-subrc.
else.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endif.
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 ).
if sy-subrc <> 0.
if sy-msgid is initial or sy-msgty is initial.
message e022(po) with sy-subrc.
else.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endif.
endform.
form get_pdffiles using p_sel changing pt_files type t_fileinfotab lnum type i.
data lo_gui type ref to cl_gui_frontend_services.
data lv_file_path type string.
data lt_files type filetable.
data l_data type xstring.
data wa_fil type t_fileinfo.
data lt_data type standard table of rspolpbi.
data lv_browse_rc type i.
create object lo_gui.
*
* if p_sel = 1.
*
* lo_gui->directory_browse( exporting window_title = text1
* changing selected_folder = lv_file_path ).
* if sy-subrc <> 0.
* if sy-msgid is initial or sy-msgty is initial.
* message e022(po) with sy-subrc.
* else.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
* endif.
*
* return.
* endif.
*
* lo_gui->directory_list_files( exporting directory = lv_file_path
* filter = '*.pdf'
* files_only = abap_true
* changing file_table = lt_files
* count = lnum
* exceptions cntl_error = 1
* directory_list_files_failed = 2
* wrong_parameter = 3
* error_no_gui = 4
* not_supported_by_gui = 5 ).
*
* if sy-subrc <> 0.
* if sy-msgid is initial or sy-msgty is initial.
* message e022(po) with sy-subrc.
* else.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
* endif.
*
* return.
* endif.
*
* else.
*
* lo_gui->file_open_dialog( exporting
* window_title = text1
* multiselection = abap_true
* changing
* file_table = lt_files
* rc = lv_browse_rc
* exceptions
* file_open_dialog_failed = 1
* cntl_error = 2
* error_no_gui = 3
* not_supported_by_gui = 4
* others = 5 ).
*
* endif.
*
* if sy-subrc <> 0.
* if sy-msgid is initial or sy-msgty is initial.
* message e022(po) with sy-subrc.
* else.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
* endif.
*
* return.
* endif.
data: l_name type EPS2FILNAM,
t_tab_01 TYPE STANDARD TABLE OF EPS2FILI.
l_name = p_dir.
CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING'
EXPORTING
iv_dir_name = l_name
FILE_MASK = '*.*'
* IMPORTING
* DIR_NAME =
* FILE_COUNTER =
* ERROR_COUNTER =
tables
dir_list = t_tab_01
EXCEPTIONS
INVALID_EPS_SUBDIR = 1
SAPGPARAM_FAILED = 2
BUILD_DIRECTORY_FAILED = 3
NO_AUTHORIZATION = 4
READ_DIRECTORY_FAILED = 5
TOO_MANY_READ_ERRORS = 6
EMPTY_DIRECTORY_LIST = 7
OTHERS = 8.
delete t_tab_01 where name np p_mask.
* loop at lt_files into data(wa).
loop at t_tab_01 into data(wa).
refresh lt_data.
clear wa_fil.
if p_sel = 1.
concatenate p_dir '/' wa-name into wa_fil-filename.
else.
move wa-name to wa_fil-filename.
lnum = lnum + 1.
endif.
wa_fil-len = wa-SIZE.
* lo_gui->gui_upload( exporting filename = wa_fil-filename
* filetype = 'BIN'
* importing filelength = wa_fil-len
* changing data_tab = lt_data
* 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 ).
data: lv_buffer type rspolpbi.
OPEN DATASET wa_fil-filename FOR INPUT IN binary MODE.
* ENCODING DEFAULT WITH SMART LINEFEED IGNORING CONVERSION ERRORS.
IF sy-subrc <> 0.
message e022(po) with sy-subrc.
CONTINUE.
ENDIF.
DO.
READ DATASET wa_fil-filename INTO lv_buffer-data.
IF sy-subrc <> 0.
EXIT.
ENDIF.
APPEND lv_buffer TO lt_data.
ENDDO.
* if sy-subrc <> 0.
* if sy-msgid is initial or sy-msgty is initial.
* message e022(po) with sy-subrc.
* else.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
* endif.
*
* return.
* endif.
perform table2xstring tables lt_data
using wa_fil-data
wa_fil-len.
write: / wa(50) color col_normal, 'Len', wa_fil-len.
append wa_fil to pt_files.
endloop.
lnum = lines( pt_files ).
write: / 'Number of PDF files:'(007), lnum.
endform.
form table2xstring tables pt_data structure rspolpbi
using p_data type xstring
p_len type i.
data l_rest type i.
data l_chunk type i.
l_rest = p_len.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = p_len
* FIRST_LINE = 0
* LAST_LINE = 0
IMPORTING
BUFFER = p_data
tables
binary_tab = pt_data
EXCEPTIONS
FAILED = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* clear p_data.
* loop at pt_data into data(wa).
* if l_rest < 128.
* l_chunk = l_rest.
* else.
* l_chunk = 128.
* endif.
*
* concatenate p_data wa-data(l_chunk) into p_data in byte mode.
* subtract l_chunk from l_rest.
* endloop.
endform.
form merge_pdfs using pt_files type t_fileinfotab
pt_display_options type t_displayoption
changing pdf_merger type ref to cl_rspo_pdf_merge
merged_document
error_text
docindex
errordoc
rc.
data: wa type line of t_fileinfotab.
data: i type i value 1.
* Add documents to attribut table of PDF merger
loop at pt_files into wa.
rc = 0.
if p_hd_pc = 'X'.
pdf_merger->check_document( exporting document = wa-data importing error_text = error_text rc = rc ).
endif.
if rc = 0.
pdf_merger->add_document( wa-data ).
else.
write: / 'Error for document number '(012), i, ' : ', error_text color col_negative.
write: / 'Document will not be merged'(013).
endif.
i = i + 1.
endloop.
rc = 0.
* Display options for merged PDF are optional. A built-in default is used if not specified.
if pt_display_options-page_mode <> 0.
pdf_merger->set_page_mode( pt_display_options-page_mode ).
endif.
if pt_display_options-page_layout <> 0.
pdf_merger->set_page_layout( pt_display_options-page_layout ).
endif.
if pt_display_options-hide_menubar <> 0.
pdf_merger->set_hide_menubar( pt_display_options-hide_menubar ).
endif.
if pt_display_options-hide_toolbar <> 0.
pdf_merger->set_hide_toolbar( pt_display_options-hide_toolbar ).
endif.
* Call kernel method to do the merge of the specified files.
pdf_merger->merge_documents( importing merged_document = merged_document rc = rc ).
* Get index of failed document
if rc <> 0.
pdf_merger->get_err_doc_index( importing index = docindex ).
pdf_merger->get_document( exporting index = docindex importing document = errordoc ).
pdf_merger->get_last_error_text( changing error_text = error_text ).
endif.
clear pdf_merger.
endform.