Skip to Content
Author's profile photo playsuji s

Convert/Download Abap Report to PDF

Often the ABAP list report needs to viewed as PDF and attach email,….. and usually submit report to spool, then the spool is converted to PDF. Its like 3~4 steps regularly. I collected all the code and created a function to do  all the steps. Process is Submit the same report to spool and get the spool to convert to PDF / or Download the PDF to particular directory. This helped me learn and work on XSTRING.. I got problem in retriving the spool from the database for recently created,  then i come up with exact time it is being created to pick the spool no. Once spool number is ok, Then its convert to Xstring, PDF Tab or Download directly to file. Reason for returning XSTRING/PDF tab to be used for attaching email/other purposes..

Where to Use:

You can use it inside your program code itself. or AT USER-COMMAND .. for custom button click.

How to Use:

If Download eq ‘X’ is passed, then function will be expecting the filename to be given. so give the filename with full path and dont forget ‘.pdf’ to include in it. If  you dont want the output PDF File to be created just pass I_DOWNLOAD eq space . I_PAART is the page format, it can specified to your own format as well. Some times the linesize of the PDF needed to be changed often, in that case pass the size value to I_LINSZ . XSTRING & SOLIX tab always returned.

Importing:

Parameter
Type Optional
Possible/Example Values
I_FILENAME STRING True(if i_download eq space) C:\temp\test.pdf
I_PAART SYPAART False X_65_132
I_REPID SYREPID False SY-REPID
I_LINSZ SYLINSZ True 180
I_DOWNLOAD CHAR1 True ‘X’ OR SPACE

Example:

data l_spoolno type sy-spono.

data l_xstring type xstring.

data l_solix type solix_tab.

AT USER-COMMAND.

CASE SY-UCOMM.

WHEN ‘PDF’.

  CALL FUNCTION ‘Z_CONVERT_TO_PDF1’

    EXPORTING

      i_repid    = sy-repid

      i_linsz    = sy-linsz

      i_download = space

    IMPORTING

      e_spono    = l_spoolno

      e_xstring  = l_xstring

      e_solix    = l_solix.

ENDCASE.

  CALL FUNCTION ‘Z_CONVERT_TO_PDF1’

    EXPORTING

     i_filename = ‘C:\test.pdf’

      i_repid    = sy-repid

      i_linsz    = 300

      i_download = ‘X’

    IMPORTING

      e_spono    = l_spoolno

      e_xstring  = l_xstring

      e_solix    = l_solix.

Below is the Code for it..

FUNCTION z_convert_to_pdf1.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     REFERENCE(I_FILENAME) TYPE  STRING DEFAULT ‘C:\temp\test.pdf’
*”     REFERENCE(I_PAART) TYPE  SYPAART DEFAULT ‘X_65_132’
*”     REFERENCE(I_REPID) TYPE  SYREPID
*”     REFERENCE(I_LINSZ) TYPE  SYLINSZ
*”     REFERENCE(I_DOWNLOAD) TYPE  CHAR1 DEFAULT ‘X’
*”  EXPORTING
*”     REFERENCE(E_SPONO) TYPE  RSPOID
*”     REFERENCE(E_XSTRING) TYPE  XSTRING
*”     REFERENCE(E_SOLIX) TYPE  SOLIX_TAB
*———————————————————————-

  DATA:    l_params TYPE pri_params,
           l_valid TYPE string,
           w_spool_nr LIKE tsp01-rqident.
  DATA:    l_repid TYPE sy-repid.
  DATA:    l_acttime    TYPE rslgtime,
           l_timestamp  TYPE timestamp.
  DATA:    l_range_acttime TYPE RANGE OF rslgtime WITH HEADER LINE.

*Internal table for Selection Screen
  DATA: BEGIN OF it_rsparams OCCURS 0.
          INCLUDE STRUCTURE rsparams.
  DATA: END OF it_rsparams.

  DATA: lt_pdf TYPE TABLE OF tline,
        ls_pdf TYPE tline.
  DATA:
   pdf_bytecount                  TYPE i,
   pdf_spoolid                    TYPE tsp01-rqident,
   otf_pagecount                  TYPE i,
   btc_jobname                    TYPE tbtcjob-jobname,
   btc_jobcount                   TYPE tbtcjob-jobcount,
   bin_file                       TYPE xstring.

  l_repid = i_repid.

*TO GET PRINT PARAMETERS
  CALL FUNCTION ‘GET_PRINT_PARAMETERS’
    EXPORTING
      immediately            = space
      no_dialog              = ‘X’
    IMPORTING
      out_parameters         = l_params
      valid                  = l_valid
    EXCEPTIONS
      archive_info_not_found = 1
      invalid_print_params   = 2
      invalid_archive_params = 3
      OTHERS                 = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  IF l_valid NE ‘X’.
    STOP.
  ENDIF.

* Store the current selection screen details
  CALL FUNCTION ‘RS_REFRESH_FROM_SELECTOPTIONS’
    EXPORTING
      curr_report     = l_repid
    TABLES
      selection_table = it_rsparams.

  IF i_linsz IS NOT INITIAL.
    l_params-linsz = i_linsz.
  ENDIF.
  IF i_paart IS NOT INITIAL.
    l_params-paart = i_paart.
  ENDIF.

*Create System Log Time Before spool
  CLEAR l_acttime.
  CONVERT DATE sy-datum TIME sy-uzeit
  INTO TIME STAMP l_timestamp TIME ZONE sy-zonlo.
  l_acttime(14) = l_timestamp.
  CONCATENATE l_acttime ’00’ INTO l_acttime.
  l_range_acttime-sign   = ‘I’.
  l_range_acttime-option = ‘BT’.
  l_range_acttime-low    = l_acttime.

*Submit Report
  SUBMIT (l_repid) WITH SELECTION-TABLE it_rsparams
                    TO SAP-SPOOL
                    SPOOL PARAMETERS l_params
                    WITHOUT SPOOL DYNPRO
                    AND RETURN.

*Create System Log Time Right after spool
  CLEAR l_acttime.
  CONVERT DATE sy-datum TIME sy-uzeit
  INTO TIME STAMP l_timestamp TIME ZONE sy-zonlo.
  l_acttime(14) = l_timestamp.
  CONCATENATE l_acttime ’00’ INTO l_acttime.
  l_range_acttime-high    = l_acttime.
  APPEND l_range_acttime.

*SELECT THE RECENTLY CREATED SPOOL
  SELECT MAX( rqident ) INTO w_spool_nr FROM tsp01
         WHERE rqclient  = sy-mandt
         AND   rq0name   = l_params-prdsn
         AND   rq1name   = l_params-pdest
         AND   rq2name   = l_params-plist
         AND   rqowner   = l_params-prrec
         AND   rqcretime IN  l_range_acttime.

  e_spono = w_spool_nr.

*Get Xstring PDF
  CALL FUNCTION ‘CONVERT_ABAPSPOOLJOB_2_PDF’
    EXPORTING
      src_spoolid          = w_spool_nr
      no_dialog            = space
      pdf_destination      = ‘X’
      get_size_from_format = space
    IMPORTING
      pdf_bytecount        = pdf_bytecount
      pdf_spoolid          = pdf_spoolid
      btc_jobname          = btc_jobname
      btc_jobcount         = btc_jobcount
      bin_file             = e_xstring
    TABLES
      pdf                  = lt_pdf.
*Get tab PDF
  CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
    EXPORTING
      buffer     = e_xstring
    TABLES
      binary_tab = e_solix.

*Download Directly to the Location
  IF i_download EQ ‘X’.
    CALL FUNCTION ‘GUI_DOWNLOAD’
      EXPORTING
        bin_filesize = pdf_bytecount
        filename     = i_filename
        filetype     = ‘BIN’
      TABLES
        data_tab     = e_solix.
  ENDIF.

ENDFUNCTION.

*All the codings are collected and modified from Standard sap programs and refrences.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi,

      The Program you wrote is awesome but you would have mentioned the data declarations for the variables you used in this program.

      That would be more efficient and easily applicable for the users who use this code.

      Thanks,

      Pradeep.

      Author's profile photo playsuji s
      playsuji s
      Blog Post Author

      Hi Pradeep,

      Thanks for your comments,

      Actually the data type i used is exactly what mentioned in the function itself. Anyway i respect your request and i amended declarations. 🙂

      Author's profile photo Former Member
      Former Member

      Thanks for the update.

      Author's profile photo Former Member
      Former Member

      Reall y thanks for your interst for creating F.m i have a requirement of dowmlaoding a report in background  when we clicks on execute option for this Shall i use the functin module directly by copying

      Author's profile photo Naveen George
      Naveen George

      Hi,

      Good one.

      Thanks,

      Naveen