How To Print PDF File Content From ABAP in Background

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

REPORT z_print_pdf.

TYPE-POOLS: abap, srmgs.

PARAMETERS: p_prnds LIKE tsp01-rqdest OBLIGATORY DEFAULT 'LOCL',


p_fname TYPE file_table-filename OBLIGATORY LOWER CASE,
p_ncopi TYPE rspocopies OBLIGATORY DEFAULT '1',
p_immed AS CHECKBOX.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

DATA: lv_rc TYPE i,


lv_filter TYPE string.

DATA: lt_files TYPE filetable.

FIELD-SYMBOLS: <fs_file> LIKE LINE OF lt_files.

CONCATENATE 'PDF (*.pdf)|*.pdf|'


cl_gui_frontend_services=>filetype_all INTO lv_filter.

CALL METHOD cl_gui_frontend_services=>file_open_dialog


EXPORTING
file_filter = lv_filter
CHANGING
file_table = lt_files
rc = lv_rc
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0 AND lv_rc EQ 0.
MESSAGE 'Error' TYPE 'E' DISPLAY LIKE 'S'.
ENDIF.
READ TABLE lt_files ASSIGNING <fs_file> INDEX 1.
IF sy-subrc EQ 0.
p_fname = <fs_file>-filename.
ENDIF.

AT SELECTION-SCREEN.

DATA: lv_name TYPE string,


lv_result TYPE boolean.

lv_name = p_fname.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = lv_name
RECEIVING
result = lv_result
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
MESSAGE 'Bad file!' TYPE 'E' DISPLAY LIKE 'S'.
ENDIF.

IF lv_result NE abap_true.
MESSAGE 'Bad file!' TYPE 'E' DISPLAY LIKE 'S'.
ENDIF.

START-OF-SELECTION.

END-OF-SELECTION.
PERFORM process.

FORM process.

DATA: lv_name TYPE string,


lv_size TYPE i,
lv_data TYPE xstring,
lv_retcode TYPE i.

DATA: lt_file TYPE srmgs_bin_content.

lv_name = p_fname.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_name
filetype = 'BIN'
IMPORTING
filelength = lv_size
CHANGING
data_tab = lt_file
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
MESSAGE 'Read file error!' TYPE 'E' DISPLAY LIKE 'S'.
ENDIF.

CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'


EXPORTING
input_length = lv_size
IMPORTING
buffer = lv_data
TABLES
binary_tab = lt_file
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc NE 0.
MESSAGE 'Binary conversion error!' TYPE 'E' DISPLAY LIKE 'S'.
ENDIF.

PERFORM print USING p_prnds lv_data CHANGING lv_retcode.


IF lv_retcode EQ 0.
WRITE: / 'Print OK' COLOR COL_POSITIVE.
ELSE.
WRITE: / 'Print ERROR' COLOR COL_NEGATIVE.
ENDIF.

ENDFORM. " PROCESS


FORM print USING iv_prndst TYPE rspopname
iv_content TYPE xstring
CHANGING ev_retcode TYPE i.

DATA: lv_handle TYPE sy-tabix,


lv_spoolid TYPE rspoid,
lv_partname TYPE adspart,
lv_globaldir TYPE text1024,
lv_dstfile TYPE text1024,
lv_filesize TYPE i,
lv_pages TYPE i.
CLEAR: ev_retcode.

CALL FUNCTION 'ADS_SR_OPEN'


EXPORTING
dest = iv_prndst
doctype = 'ADSP'
copies = p_ncopi
immediate_print = p_immed
auto_delete = 'X'
IMPORTING
handle = lv_handle
spoolid = lv_spoolid
partname = lv_partname
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
ev_retcode = 4.
RETURN.
ENDIF.

CALL FUNCTION 'ADS_GET_PATH'


IMPORTING
ads_path = lv_globaldir.

CONCATENATE lv_globaldir '/' lv_partname '.pdf' INTO lv_dstfile.

OPEN DATASET lv_dstfile FOR OUTPUT IN BINARY MODE.


IF sy-subrc NE 0.
ev_retcode = 4.
RETURN.
ENDIF.

TRANSFER iv_content TO lv_dstfile.


IF sy-subrc NE 0.
ev_retcode = 4.
RETURN.
ENDIF.

CLOSE DATASET lv_dstfile.


IF sy-subrc NE 0.
ev_retcode = 4.
RETURN.
ENDIF.

CALL FUNCTION 'ZBAP_RM_PDF_GET_PAGES'


EXPORTING
iv_content = iv_content
IMPORTING
ev_pages = lv_pages.

lv_filesize = XSTRLEN( iv_content ).


CALL FUNCTION 'ADS_SR_CONFIRM'
EXPORTING
handle = lv_handle
partname = lv_partname
size = lv_filesize
pages = lv_pages
no_pdf = ' '
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
ev_retcode = 4.
RETURN.
ENDIF.

CALL FUNCTION 'ADS_SR_CLOSE'


EXPORTING
handle = lv_handle
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
ev_retcode = 4.
RETURN.
ENDIF.

ENDFORM. " PRINT

UNCTION zbap_rm_pdf_get_pages.
*"--------------------------------------------------------------------
--
*"*"Lokální rozhraní:
*" IMPORTING
*" REFERENCE(IV_CONTENT) TYPE XSTRING
*" EXPORTING
*" REFERENCE(EV_PAGES) TYPE I
*"--------------------------------------------------------------------
--

DATA: lv_cont TYPE string,


lv_lines TYPE i,
lv_pages TYPE numc5,
lv_temp TYPE string.

DATA: lt_result TYPE match_result_tab.

FIELD-SYMBOLS: <fs_result> LIKE LINE OF lt_result,


<fs_subm> LIKE LINE OF <fs_result>-submatches.

ev_pages = 0.

CALL 'ICT_DISPATCH' ID 'did' FIELD 'append_xstring_to_string'


ID 'source' FIELD iv_content
ID 'dest' FIELD lv_cont.

FIND REGEX `/N (.{1,5})/` IN lv_cont IGNORING CASE RESULTS


lt_result.
IF sy-subrc NE 0.
FIND ALL OCCURRENCES OF REGEX `/count (.{1,4})/` IN lv_cont
IGNORING CASE RESULTS lt_result.
ENDIF.
lv_lines = LINES( lt_result ).
IF lv_lines IS NOT INITIAL.
READ TABLE lt_result ASSIGNING <fs_result> INDEX lv_lines.
IF sy-subrc EQ 0.
READ TABLE <fs_result>-submatches ASSIGNING <fs_subm> INDEX 1.
IF sy-subrc EQ 0.
lv_temp = lv_cont+<fs_subm>-offset(<fs_subm>-length).
CONDENSE lv_temp NO-GAPS.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = lv_temp
IMPORTING
output = lv_pages.
ev_pages = lv_pages.
ENDIF.
ENDIF.
ENDIF.

ENDFUNCTION.

You might also like