Application Development 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: 

How to print PDF file content from ABAP in background?

tomas_wiedermann
Explorer
0 Kudos

Hi,

Is it possible to print PDF file content from ABAP in background?

I have some PDF content which I need to print it, these PDF files are generated outside the SAP.

Please have you any suggestions?

Thank you

Tomas

20 REPLIES 20

tomas_wiedermann
Explorer
0 Kudos

<b><u>Solution:</u></b><br>

<br>

The target output device must support PDF print, this is only one limitation.<br>

<br>


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

0 Kudos

Can you exactly specify what is meant by

The target output device must support PDF print, this is only one limitation

Because the report generates a spool file, but when i try to print it via SP01 then i get an error!

andreas_mann3
Active Contributor
0 Kudos

thank you for your coding,

but to use your report , we need the called fm 'ZBAP_RM_PDF_GET_PAGES' too!

grx

Andreas

0 Kudos

Hello,

the number of pages is not important, but FM attached bellow:


FUNCTION 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.

Former Member
0 Kudos

You asked for background...the spoon-fed code utilizes GUI_FRONTEND_SERVICES class...that's not going to work in background.

0 Kudos

Yes you are right, but this is only demo with code for easy upload PDF from frontend and print it via configured spool printer without GUI (saplpd/win printer, etc....).

If you want use it in background, just remove cl_gui_frontend_services usage and replace it with open dataset and read data set to read data from application server.

Regards

Tomas

0 Kudos

Hi Thomas,

Is this sort of PDF printing possible only with one of the new ECC EHPs(5)? coz, in ECC6.0, we dont have the FM: ADS_GET_PATH.

But now we are upgrading to ECC6 EHP5 & this FM is available.

Also, I'm not able to open the Spool that is generated in SP01, it says 'File /usr/sap/...xxx../sys/global/810ADSP/SPOOL0000400639_00

cannot be opened'. I'm trying to check with my security team also if this is any Auth issue.

Thanks alot!

0 Kudos

Hi Phani,

hope these notes will answer you questions:

1) FM is available in SP of your basis and kernel release:

Note 1327372 - Subdirectories for ADS spool requests

2) Authorization error:

Note 1516785 - Dump OPEN_DATASET_NO_AUTHORITY when displaying ADS request

Note 1686051 - S_DATASET authorization not applicable for ADS spool request

or check if the target path is owned by <sid>adm:sapsys

Kid regards

Tomas

Edit: In our system ECC 6.0 EhP4 on BASIS 7.01 SP10 is the FM available.

0 Kudos

How did you define you printer in SPAD?   I have the PDF in the output spool, and I can view it, but when it prints, it just prints the text of the PDF (like you opened in notepad) eg. %PDF-1.4 .....

I defined my printer as:

Device Type:      'PDF1 : PDF ISO Latin-1   4.6D+'

Device Class:     'Standard'

Access Method: 'G: Front End Printing with Control Tech

Host Printer:       __DEFAULT

Thanks,

-Tim

Former Member
0 Kudos

Hello Thomas,

your Coding helped me, I could generate a spool with the right pdf-File. But I also have problems with printing the spool, this point doesn't work. There was the advice, to install a pdf-printer, in order to print (in background as well as online).

But how? Can you give me an hint? By the way, the adobe document server is installed.

Thanks in advance

Andreas

0 Kudos

I think you need to use a printer which can process PDF-Files directly!

To check if the printer is able to print PDFs directly, go to the command line and send a command like

copy test.pdf \\printserver\printername

If the PDF comes out, everything should work!

If you have to use a printer which is not able to process PDFs, then another possibility is to send a postscriptfile to it. I use a above-like coding and write postscriptdata to the printfiles. It works!

The other problem is to generate a postscript in SAP!

0 Kudos

Hello Manfred,

thank's for your answer. If I install a PDF-Printer with help of Transaction SPAD, I can print the pfd via workprocess. But I still have problems via batch. The command "copy test.pdf \\printserver\printername" did not work.

How did you generate postscript? Is this solution working via spool including batch?

0 Kudos

The postscript is generated outside SAP in a Java Servlet.

I thought the copy command should work with PDF-File because in this way it is also possible to send postscript directly to a printer!

Sorry, i can´t give you any help with PDF-Printers because i don´t have any of them in use!

But as others told, above coding should work!

0 Kudos

Hi

As per my requirement I have read the PDF file which is there in the application server and give print.        I have implemented ADS Fucntional module logic and able to view and print the PDF file using spool (SP02). But when I directly print to printer it is printing all Raw data(Similar to application server file data).

Can you please suggest what would be the reason.

Regards

Raj.

0 Kudos

Hi

As per my requirement I have read the PDF file which is there in the application server and give print.        I have implemented ADS Fucntional module logic and able to view and print the PDF file using spool (SP02). But when I directly print to printer it is printing all Raw data(Similar to application server file data).

Can you please suggest what would be the reason if ADS soluction is working for you.

Regards

Raj.

0 Kudos

Maybe your printer does not support PDF-Printing directly

0 Kudos

Hi Manfred,

Thanks for your reply. When I open the spool it will open in PDF format and I could print that. It means that my printer will support PDF right.  How exactly we can check whether printer will support PDF or not?


0 Kudos

You have tried the code here in this thread provided by Thomas Wiedermann and it is not working??

As i wrote a PDF-Compatible printer shoult print out the PDF if you go to command line and write

copy test.pdf \\printserver\printername

timo_ehl3
Participant
0 Kudos

Hi, thank you for your post. Cause of the ADS FM's. Is it necessary mandatory to have ADS (which means also JAVA Stack)? 

2SAP
Discoverer
0 Kudos

I use function ADS_CREATE_PDF_SPOOLJOB : it's easier