Wednesday, September 14, 2011

Converting a SAPSCRIPT to PDF

Below are some of the things that you need to do in order for the sapscript to be converted into a pdf file. (We need to convert the spool instead of otf)

1. Make sure to move values to the ff. fields of OPTIONS parameter of FM OPEN_FORM:


                               TDCOPIES     = 1.
                            tdpreview   = ''.
                            TDNOPRINT = ''.
                            tdnewid     = X.
                           TDIMMED = ''.
                            TDPREVIEW  = ''. "to avoid showing the sapscript

  CALL FUNCTION 'OPEN_FORM'
       EXPORTING FORM = XFORMULAR
                 LANGUAGE = EKKO-SPRAS
                 OPTIONS = ITCPO
                 ARCHIVE_INDEX  = TOA_DARA
                 ARCHIVE_PARAMS = ARC_PARAMS
                 DEVICE = XDEVICE
                 DIALOG = XDIALOG
            MAIL_SENDER        = LVS_SENDER
            MAIL_RECIPIENT     = LVS_RECIPIENT
       EXCEPTIONS CANCELED = 01

2. Get the RESULT-TDSPOOLID of CLOSE_FORM and move to FM "CONVERT_OTFSPOOLJOB_2_PDF" to convert to pdf as shown below:

      CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
        EXPORTING
          src_spoolid              =
RESULT-TDSPOOLID
        TABLES
          pdf                      = it_pdf_lines
        EXCEPTIONS
          err_no_otf_spooljob      = 1
          err_no_spooljob          = 2
          err_no_permission        = 3
          err_conv_not_possible    = 4
          err_bad_dstdevice        = 5
          user_cancelled           = 6
          err_spoolerror           = 7
          err_temseerror           = 8
          err_btcjob_open_failed   = 9
          err_btcjob_submit_failed = 10
          err_btcjob_close_failed  = 11
          OTHERS                   = 12.  

3. You may then transfer the pdf file to your local directory directly

        CALL METHOD cl_gui_frontend_services=>gui_download
        EXPORTING
              filename          = l_AFILENAME
            filetype             = 'BIN'
           append                = 'X'
           CHANGING
            data_tab             = it_pdf_lines[]
        EXCEPTIONS
            OTHERS               = 1.  

 OR you may opt to save in a different location

          CALL FUNCTION 'DOWNLOAD'
               EXPORTING
                    FILENAME     = l_filename_d
                    FILETYPE     = 'BIN'
               IMPORTING
                    ACT_FILENAME = AFILENAME
               TABLES
                    DATA_TAB     = it_pdf_lines[].  

4. Make sure to get the file in the location were you saved the pdf file for the FM below will function properly:


** this will open the pdf file in PDF reader like ADOBE ACROBAT READER, etc.

            CALL FUNCTION 'WS_EXECUTE'
                  EXPORTING
                    document           = c_x
                    program            = l_AFILENAME
                  EXCEPTIONS
                    frontend_error     = 1
                    no_batch           = 2
                    prog_not_found     = 3
                    illegal_option     = 4
                    gui_refuse_execute = 5
                    OTHERS             = 6.


Then you are done!! 

No comments:

Post a Comment