Skip to Content

How to print PDF file content from ABAP in background?

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

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

5 Answers

  • Feb 09, 2012 at 08:08 AM

    <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
    

    Add comment
    10|10000 characters needed characters exceeded

    • 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!

  • Feb 09, 2012 at 08:42 AM

    thank you for your coding,

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

    grx

    Andreas

    Add comment
    10|10000 characters needed characters exceeded

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

  • avatar image
    Former Member
    Jun 21, 2012 at 10:17 PM

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

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • avatar image
    Former Member
    Mar 20, 2014 at 11:54 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 24, 2014 at 08:35 AM

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

    Add comment
    10|10000 characters needed characters exceeded