Converting Single SAP Script output into PDF file.

Methodology The SAP Script output is internally represented in OTF format which is normally not visible to the user. This internal format is accessed in this program by setting the TDGETOTF field of the structure of type ITCPO and passing the structure to the OPTIONS parameter of the function module OPEN_FORM. To suppress the dialog box in SAP Script asking for Printer details, the DIALOG parameter of OPEN_FORM is assigned to SPACE. The SAP Script is generated using the Function modules START_FORM, WRITE_FORM and END_FORM. In the function module CLOSE_FORM, the OTF output for the SAPscript is obtained in the Tables parameter OTFDATA by assigning an internal table of structure type ITCOO. This internal table data is converted to PDF data using the function module CONVERT_OTF, which will also return the size of PDF data. This PDF data is generated in an internal table of Structure type TLINE. The PDF data is downloaded into a PDF file in the local system using the Function module GUI_DOWNLOAD. The path where the file will be downloaded is taken as user input using the method FILE_SAVE_DIALOG of the class CL_GUI_FRONTEND_SERVICES. Code Snippet
REPORT Z1SCRIPT_TO_PDF . ****************************DECLARATIONS******************************** DATA: BEGIN OF ITAB OCCURS 0, CARRID TYPE SFLIGHT-CARRID, CONNID TYPE SFLIGHT-CONNID, PRICE TYPE SFLIGHT-PRICE, END OF ITAB. DATA: struct TYPE ITCPO. DATA: PDFTAB TYPE TABLE OF TLINE WITH HEADER LINE, DATAB TYPE TABLE OF ITCOO WITH HEADER LINE, DATA: BINFILESIZE TYPE I, FILE_NAME TYPE STRING, FILE_PATH TYPE STRING, FULL_PATH TYPE STRING. ****************************END OF DECLARATIONS************************** *To specify Printer name* struct-tddest = 'LP01'. *To specify no Print Preview* struct-tdnoprev = 'X'. *To access the SAP Script output in OTF format* struct-tdgetotf = 'X'. ***************************SAPSCRIPT GENERATION******************************** CALL FUNCTION 'OPEN_FORM' EXPORTING * APPLICATION = 'TX' * ARCHIVE_INDEX = * ARCHIVE_PARAMS = DEVICE = 'PRINTER' DIALOG = space FORM = 'ZSCRIPT' * LANGUAGE = SY-LANGU OPTIONS = struct * MAIL_SENDER = * MAIL_RECIPIENT = * MAIL_APPL_OBJECT = * RAW_DATA_INTERFACE = '*' * SPONUMIV = * IMPORTING * LANGUAGE = * NEW_ARCHIVE_PARAMS =

* RESULT = EXCEPTIONS CANCELED = 1 DEVICE = 2 FORM = 3 OPTIONS = 4 UNCLOSED = 5 MAIL_OPTIONS = 6 ARCHIVE_ERROR = 7 INVALID_FAX_NUMBER = 8 MORE_PARAMS_NEEDED_IN_BATCH = 9 SPOOL_ERROR = 10 CODEPAGE = 11 OTHERS = 12 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. CALL FUNCTION 'START_FORM' EXPORTING * ARCHIVE_INDEX = FORM = 'ZSCRIPT' * LANGUAGE = ' ' * STARTPAGE = ' ' * PROGRAM = ' ' * MAIL_APPL_OBJECT = * IMPORTING * LANGUAGE = EXCEPTIONS FORM = 1 FORMAT = 2 UNENDED = 3 UNOPENED = 4 UNUSED = 5 SPOOL_ERROR = 6 CODEPAGE = 7 OTHERS = 8 IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. CALL FUNCTION 'WRITE_FORM' EXPORTING ELEMENT = 'ELEM1' FUNCTION = 'SET' TYPE = 'BODY' WINDOW = 'MAIN' * IMPORTING * PENDING_LINES = EXCEPTIONS ELEMENT = 1 FUNCTION = 2 TYPE = 3 UNOPENED = 4 UNSTARTED = 5 WINDOW = 6 BAD_PAGEFORMAT_FOR_PRINT = 7 SPOOL_ERROR = 8 CODEPAGE = 9

OTHERS = 10 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. SELECT CARRID CONNID PRICE FROM SFLIGHT INTO TABLE ITAB. LOOP AT ITAB. CALL FUNCTION 'WRITE_FORM' EXPORTING ELEMENT = 'ELEM2' FUNCTION = 'SET' TYPE = 'BODY' WINDOW = 'MAIN' * IMPORTING * PENDING_LINES = EXCEPTIONS ELEMENT = 1 FUNCTION = 2 TYPE = 3 UNOPENED = 4 UNSTARTED = 5 WINDOW = 6 BAD_PAGEFORMAT_FOR_PRINT = 7 SPOOL_ERROR = 8 CODEPAGE = 9 OTHERS = 10 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDLOOP. CALL FUNCTION 'END_FORM' * IMPORTING * RESULT = EXCEPTIONS UNOPENED = 1 BAD_PAGEFORMAT_FOR_PRINT = 2 SPOOL_ERROR = 3 CODEPAGE = 4 OTHERS = 5 IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. CALL FUNCTION 'CLOSE_FORM' * IMPORTING * RESULT = * RDI_RESULT = TABLES OTFDATA = DATAB[] EXCEPTIONS UNOPENED = 1 BAD_PAGEFORMAT_FOR_PRINT = 2 SEND_ERROR = 3 SPOOL_ERROR = 4 CODEPAGE = 5 OTHERS = 6 .

IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ***************************END OF SAPSCRIPT GENERATION********************* ***************************CONVERTING OTF DATA TO PDF DATA*************** CALL FUNCTION 'CONVERT_OTF' EXPORTING FORMAT = 'PDF' * MAX_LINEWIDTH = 132 * ARCHIVE_INDEX = ' ' * COPYNUMBER = 0 * ASCII_BIDI_VIS2LOG = ' ' * PDF_DELETE_OTFTAB = ' ' IMPORTING BIN_FILESIZE = BINFILESIZE * BIN_FILE = TABLES otf = DATAB[] lines = PDFTAB[] * EXCEPTIONS * ERR_MAX_LINEWIDTH = 1 * ERR_FORMAT = 2 * ERR_CONV_NOT_POSSIBLE = 3 * ERR_BAD_OTF = 4 * OTHERS = 5 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. *****************TAKING THE DOWNLOAD FILE PATH AS USER INPUT********** CALL METHOD cl_gui_frontend_services=>file_save_dialog * EXPORTING * WINDOW_TITLE = * DEFAULT_EXTENSION = * DEFAULT_FILE_NAME = * FILE_FILTER = * INITIAL_DIRECTORY = * WITH_ENCODING = * PROMPT_ON_OVERWRITE = 'X' CHANGING filename = FILE_NAME path = FILE_PATH fullpath = FULL_PATH * USER_ACTION = * FILE_ENCODING = * EXCEPTIONS * CNTL_ERROR = 1 * ERROR_NO_GUI = 2 * NOT_SUPPORTED_BY_GUI = 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. ****************************DOWNLOADING THE PDF DATA******************* CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING

BIN_FILESIZE = binfilesize FILENAME = 'D:\myfile.pdf' FILETYPE = 'BIN' * APPEND = ' ' * WRITE_FIELD_SEPARATOR = ' ' * HEADER = '00' * TRUNC_TRAILING_BLANKS = ' ' * WRITE_LF = 'X' * COL_SELECT = ' ' * COL_SELECT_MASK = ' ' * DAT_MODE = ' ' * CONFIRM_OVERWRITE = ' ' * NO_AUTH_CHECK = ' ' * CODEPAGE = ' ' * IGNORE_CERR = ABAP_TRUE * REPLACEMENT = '#' * WRITE_BOM = ' ' * TRUNC_TRAILING_BLANKS_EOL = 'X' * WK1_N_FORMAT = ' ' * WK1_N_SIZE = ' ' * WK1_T_FORMAT = ' ' * WK1_T_SIZE = ' ' * IMPORTING * FILELENGTH =

tables data_tab = PDFTAB[]
* FIELDNAMES = EXCEPTIONS FILE_WRITE_ERROR = 1 NO_BATCH = 2 GUI_REFUSE_FILETRANSFER = 3 INVALID_TYPE = 4 NO_AUTHORITY = 5 UNKNOWN_ERROR = 6 HEADER_NOT_ALLOWED = 7 SEPARATOR_NOT_ALLOWED = 8 FILESIZE_NOT_ALLOWED = 9 HEADER_TOO_LONG = 10 DP_ERROR_CREATE = 11 DP_ERROR_SEND = 12 DP_ERROR_WRITE = 13 UNKNOWN_DP_ERROR = 14 ACCESS_DENIED = 15 DP_OUT_OF_MEMORY = 16 DISK_FULL = 17 DP_TIMEOUT = 18 FILE_NOT_FOUND = 19 DATAPROVIDER_EXCEPTION = 20 CONTROL_FLUSH_ERROR = 21 OTHERS = 22 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.

Output Pop up asking user to specify location where the output PDF file is to be stored.

File stored in the path provided by the user.

Converting Multiple SAP Script outputs into single PDF file. Methodology The methodology adopted for this is similar to previous case except that multiple SAPScript outputs are merged into a single OTF data which is then converted to PDF file. Between function modules OPEN_FORM and CLOSE_FORM, for each SAP Script a set of 3 function modules START_FORM, WRITE_FORM and END_FORM is used. In the function module START_FORM the name of corresponding SAPScript is specified. The merged OTF data for multiple SAPScripts is obtained in the Tables parameter OTFDATA of the function module CLOSE_FORM. This is converted to PDF data using similar method as explained in the previous case. Code Snippet REPORT ZMULTIPLE_SCRIPT_TO_PDF .
****************************DECLARATIONS******************************** DATA: BEGIN OF ITAB OCCURS 0, CARRID TYPE SFLIGHT-CARRID, CONNID TYPE SFLIGHT-CONNID, PRICE TYPE SFLIGHT-PRICE, END OF ITAB. DATA: struct TYPE ITCPO. DATA: PDFTAB TYPE TABLE OF TLINE WITH HEADER LINE, DATAB TYPE TABLE OF ITCOO WITH HEADER LINE, DATA: BINFILESIZE TYPE I, FILE_NAME TYPE STRING, FILE_PATH TYPE STRING, FULL_PATH TYPE STRING. ****************************END OF DECLARATIONS*************************** *To specify Printer name* struct-tddest = 'LP01'. *To specify no Print Preview* struct-tdnoprev = 'X'. *To access the SAP Script output in OTF format* struct-tdgetotf = 'X'. ***************************SAPSCRIPTS GENERATION******************************** CALL FUNCTION 'OPEN_FORM' EXPORTING * APPLICATION = 'TX' * ARCHIVE_INDEX = * ARCHIVE_PARAMS = DEVICE = 'PRINTER' DIALOG = space * FORM = * LANGUAGE = SY-LANGU OPTIONS = struct * MAIL_SENDER = * MAIL_RECIPIENT = * MAIL_APPL_OBJECT = * RAW_DATA_INTERFACE = '*' * SPONUMIV = * IMPORTING * LANGUAGE = * NEW_ARCHIVE_PARAMS = * RESULT = EXCEPTIONS CANCELED = 1 DEVICE = 2 FORM = 3 OPTIONS = 4 UNCLOSED = 5 MAIL_OPTIONS = 6 ARCHIVE_ERROR = 7

INVALID_FAX_NUMBER = 8 MORE_PARAMS_NEEDED_IN_BATCH = 9 SPOOL_ERROR = 10 CODEPAGE = 11 OTHERS = 12 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ************************GENERATION OF FIRST SAP SCRIPT********** CALL FUNCTION 'START_FORM' EXPORTING * ARCHIVE_INDEX = FORM = 'ZSCRIPT1' * LANGUAGE = ' ' * STARTPAGE = ' ' * PROGRAM = ' ' * MAIL_APPL_OBJECT = * IMPORTING * LANGUAGE = EXCEPTIONS FORM = 1 FORMAT = 2 UNENDED = 3 UNOPENED = 4 UNUSED = 5 SPOOL_ERROR = 6 CODEPAGE = 7 OTHERS = 8 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. CALL FUNCTION 'WRITE_FORM' EXPORTING ELEMENT = 'ELEM1' FUNCTION = 'SET' TYPE = 'BODY' WINDOW = 'MAIN' * IMPORTING * PENDING_LINES = EXCEPTIONS ELEMENT = 1 FUNCTION = 2 TYPE = 3 UNOPENED = 4 UNSTARTED = 5 WINDOW = 6 BAD_PAGEFORMAT_FOR_PRINT = 7 SPOOL_ERROR = 8 CODEPAGE = 9 OTHERS = 10 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. SELECT CARRID CONNID PRICE FROM SFLIGHT INTO TABLE ITAB.

LOOP AT ITAB. CALL FUNCTION 'WRITE_FORM' EXPORTING ELEMENT = 'ELEM2' FUNCTION = 'SET' TYPE = 'BODY' WINDOW = 'MAIN' * IMPORTING * PENDING_LINES = EXCEPTIONS ELEMENT = 1 FUNCTION = 2 TYPE = 3 UNOPENED = 4 UNSTARTED = 5 WINDOW = 6 BAD_PAGEFORMAT_FOR_PRINT = 7 SPOOL_ERROR = 8 CODEPAGE = 9 OTHERS = 10 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDLOOP. CALL FUNCTION 'END_FORM' * IMPORTING * RESULT = EXCEPTIONS UNOPENED = 1 BAD_PAGEFORMAT_FOR_PRINT = 2 SPOOL_ERROR = 3 CODEPAGE = 4 OTHERS = 5 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ***********************END OF GENERATION OF FIRST SAPSCRIPT************************ ************************GENERATION OF SECOND SAP SCRIPT**************************** CALL FUNCTION 'START_FORM' EXPORTING * ARCHIVE_INDEX = FORM = 'ZSCRIPT2' * LANGUAGE = ' ' * STARTPAGE = ' ' * PROGRAM = ' ' * MAIL_APPL_OBJECT = * IMPORTING * LANGUAGE = EXCEPTIONS FORM = 1 FORMAT = 2 UNENDED = 3 UNOPENED = 4 UNUSED = 5 SPOOL_ERROR = 6 CODEPAGE = 7

OTHERS = 8 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. CALL FUNCTION 'WRITE_FORM' EXPORTING ELEMENT = 'ELEMENT1' FUNCTION = 'SET' TYPE = 'BODY' WINDOW = 'MAIN' * IMPORTING * PENDING_LINES = EXCEPTIONS ELEMENT = 1 FUNCTION = 2 TYPE = 3 UNOPENED = 4 UNSTARTED = 5 WINDOW = 6 BAD_PAGEFORMAT_FOR_PRINT = 7 SPOOL_ERROR = 8 CODEPAGE = 9 OTHERS = 10 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. CALL FUNCTION 'END_FORM' * IMPORTING * RESULT = * EXCEPTIONS * UNOPENED = 1 * BAD_PAGEFORMAT_FOR_PRINT = 2 * SPOOL_ERROR = 3 * CODEPAGE = 4 * OTHERS = 5 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ***********************END OF GENERATION OF SECOND SAPSCRIPT************************ CALL FUNCTION 'CLOSE_FORM' * IMPORTING * RESULT = * RDI_RESULT = TABLES OTFDATA = datab[] EXCEPTIONS UNOPENED = 1 BAD_PAGEFORMAT_FOR_PRINT = 2 SEND_ERROR = 3 SPOOL_ERROR = 4 CODEPAGE = 5 OTHERS = 6 . IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. *******************END OF SAPSCRIPTS GENERATION************************************* ***************************CONVERTING OTF DATA TO PDF DATA************************** CALL FUNCTION 'CONVERT_OTF' EXPORTING FORMAT = 'PDF' * MAX_LINEWIDTH = 132 * ARCHIVE_INDEX = ' ' * COPYNUMBER = 0 * ASCII_BIDI_VIS2LOG = ' ' * PDF_DELETE_OTFTAB = ' ' IMPORTING BIN_FILESIZE = BINFILESIZE * BIN_FILE = TABLES otf = DATAB[] lines = PDFTAB[] * EXCEPTIONS * ERR_MAX_LINEWIDTH = 1 * ERR_FORMAT = 2 * ERR_CONV_NOT_POSSIBLE = 3 * ERR_BAD_OTF = 4 * OTHERS = 5 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. *****************TAKING THE DOWNLOAD FILE PATH AS USER INPUT**********CALL METHOD cl_gui_frontend_services=>file_save_dialog * EXPORTING * WINDOW_TITLE = * DEFAULT_EXTENSION = * DEFAULT_FILE_NAME = * FILE_FILTER = * INITIAL_DIRECTORY = * WITH_ENCODING = * PROMPT_ON_OVERWRITE = 'X' CHANGING filename = FILE_NAME path = FILE_PATH fullpath = FULL_PATH * USER_ACTION = * FILE_ENCODING = * EXCEPTIONS * CNTL_ERROR = 1 * ERROR_NO_GUI = 2 * NOT_SUPPORTED_BY_GUI = 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. ****************************DOWNLOADING THE PDF DATA********************************** CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING BIN_FILESIZE = binfilesize

FILENAME = full_path FILETYPE = 'BIN' * APPEND = ' ' * WRITE_FIELD_SEPARATOR = ' ' * HEADER = '00' * TRUNC_TRAILING_BLANKS = ' ' * WRITE_LF = 'X' * COL_SELECT = ' ' * COL_SELECT_MASK = ' ' * DAT_MODE = ' ' * CONFIRM_OVERWRITE = ' ' * NO_AUTH_CHECK = ' ' * CODEPAGE = ' ' * IGNORE_CERR = ABAP_TRUE * REPLACEMENT = '#' * WRITE_BOM = ' ' * TRUNC_TRAILING_BLANKS_EOL = 'X' * WK1_N_FORMAT = ' ' * WK1_N_SIZE = ' ' * WK1_T_FORMAT = ' ' * WK1_T_SIZE = ' ' * IMPORTING * FILELENGTH =

tables data_tab = PDFTAB[]

* FIELDNAMES = EXCEPTIONS FILE_WRITE_ERROR = 1 NO_BATCH = 2 GUI_REFUSE_FILETRANSFER = 3 INVALID_TYPE = 4 NO_AUTHORITY = 5 UNKNOWN_ERROR = 6 HEADER_NOT_ALLOWED = 7 SEPARATOR_NOT_ALLOWED = 8 FILESIZE_NOT_ALLOWED = 9 HEADER_TOO_LONG = 10 DP_ERROR_CREATE = 11 DP_ERROR_SEND = 12 DP_ERROR_WRITE = 13 UNKNOWN_DP_ERROR = 14 ACCESS_DENIED = 15 DP_OUT_OF_MEMORY = 16 DISK_FULL = 17 DP_TIMEOUT = 18 FILE_NOT_FOUND = 19 DATAPROVIDER_EXCEPTION = 20 CONTROL_FLUSH_ERROR = 21 OTHERS = 22 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.

Output Pop up asking user to specify location where the output PDF file is to be stored. File stored in the path provided by the user.

File stored in the path provided by the user.

Multiple SAPScript outputs in PDF format.