Get SAPScript output as PDF and launch Acrobat inside SAPGUI itself
A very common requirement for programmers writing ABAP code for printing using SAPScript Forms is to redirect output to a PDF (Adobe Acrobat) file.
A very common requirement for programmers writing ABAP code for printing using SAPScript Forms is to redirect output...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
to a PDF (Adobe Acrobat) file. I struggled for a few days trying to find a solution to this. Though I found quite a few examples on the web, it was difficult figuring out which technique was optimal. This tip is borne out of these struggles.
This is an example of how to use ABAP code to save output of a print routine using a SAPScript form into PDF format and also display it within the SAP frontend itself. This routine proves extremely useful to provide users the ability to save local copies of output and preview it within the user-friendly Acrobat Reader control, all without leaving the SAP frontend or your program. Since function modules are used, the code is portable and this technique can be used in any other ABAP program as well.
Two function modules, Z_DS_CREATE_LOCAL_PDF_FILE and Z_DS_CALL_PDF_VIEWER need to be created. I have used a function group called Z5_DS_PDF for this purpose. The function group contains the ABAP objects code for declaration and implementation of a class that encapsulates the Acrobat application functionality. The function group also contains a screen '0901', that epresents our PDF viewer and one PBO and one PAI block for the same screen.
Note: The following example has been stripped of essential error-handling for the sake of simplicity and the programmer is assumed to possess knowledge of creation of function groups, function modules, screens and SAPScript forms. ABAP objects or custom controls knowledge is not mandatory. Be patient when trying this out and follow all instructions thoroughly. The results will be worth the effort.
Steps to follow to get this example running:
1) Create a function group (Example : Z5_DS_PDF)
2) Define the top include and place the code listed below into it (LZ5_DS_PDFTOP)
3) Create screen '0901' in function group with three elements:
a) Pushbutton CLOSE at the top with function code 'CLO' (this is to exit preview screen)
b) Custom control container (Large- spanning entire screen) named MY_CONTAINER
c) The customary OK code field called OK_CODE
Note: The names of the elements should be exactly as described above
4) Create one output and one input module in the flow logic of screen '0901' for which the code is provided below
5) Define two function modules with the following signatures:
a) FUNCTION Z_DS_CREATE_LOCAL_PDF_FILE
EXPORTING
REFERENCE(AFILENAME) LIKE RLGRAP-FILENAME
TABLES
OTF_LINES STRUCTURE ITCOO
b) FUNCTION Z_DS_CALL_PDF_VIEWER
IMPORTING
VALUE(FILENAME) TYPE STRING
Code is provided below.
6) Compile and activate the function group
7) Create a simple SAPScript form with one page and one window
8) Define one element in the text for the main window called 'HELLO' and some static text in it
9) Check and activate the form
10) Create the example program (Example : Z5_DS_SCRIPT2PDF) with the below code
11) Run the example
NOTES: I tested this code in R/3 version 4.6C but it should work in all 4.6 setups. I'm pretty sure some of the ABAP objects code I have used may not work with R/3 4.0 versions and earlier. Also, it works perfectly only when Acrobat Reader is installed on the presentation server. I have checked it with Acrobat versions 4 and 5 but I haven't had the opportunity to check it with Acrobat Reader 6.
*____________________________________________________________________________________ **** **** Code inside top include LZ5_DS_PDFTOP of function group Z5_DS_PDF **** FUNCTION-POOL Z5_DS_PDF. "MESSAGE-ID .. *---------------------------------------------------------------------* * CLASS CL_GUI_PDF DEFINITION * *---------------------------------------------------------------------* * ........ * *---------------------------------------------------------------------* CLASS CL_GUI_PDF DEFINITION INHERITING FROM CL_GUI_CONTROL. PUBLIC SECTION. TYPES: COL_TYPE TYPE INT4. METHODS: CONSTRUCTOR IMPORTING !PARENT TYPE REF TO CL_GUI_CONTAINER VALUE(SHELLSTYLE) TYPE I OPTIONAL VALUE(DISP_MODE) TYPE I OPTIONAL VALUE(LIFE_TIME) TYPE I OPTIONAL VALUE(NAME) TYPE STRING OPTIONAL EXCEPTIONS CNTL_ERROR CNTL_INSTALL_ERROR. METHODS: LOADFILE IMPORTING VALUE(FILENAME) TYPE STRING EXCEPTIONS FILE_NOT_FOUND. METHODS: REFRESH. METHODS: DISPATCH REDEFINITION. ENDCLASS. DATA: MY_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER. DATA: MY_PDF TYPE REF TO CL_GUI_PDF. data: ok_code like sy-ucomm. data: file_name type string. *********************************************************************** * custom control class implementation *********************************************************************** CLASS CL_GUI_PDF IMPLEMENTATION. METHOD CONSTRUCTOR. DATA: CTRL_NAME(80) TYPE C. IF NOT CL_GUI_OBJECT=>ACTIVEX IS INITIAL. CTRL_NAME = '{CA8A9780-280D-11CF-A24D-444553540000}'. ELSE. RAISE CNTL_ERROR. ENDIF. CALL METHOD SUPER->CONSTRUCTOR EXPORTING CLSID = CTRL_NAME SHELLSTYLE = SHELLSTYLE PARENT = PARENT LIFETIME = LIFE_TIME NAME = NAME EXCEPTIONS CNTL_SYSTEM_ERROR = 1 OTHERS = 2. CASE SY-SUBRC. WHEN 1. RAISE CNTL_INSTALL_ERROR. WHEN 2. RAISE CNTL_ERROR. ENDCASE. CALL METHOD CL_GUI_CFW=>SUBSCRIBE EXPORTING REF = ME SHELLID = ME->H_CONTROL-SHELLID EXCEPTIONS OTHERS = 1. IF SY-SUBRC NE 0. RAISE CNTL_ERROR. ENDIF. ENDMETHOD. METHOD LOADFILE. CALL METHOD ME->CALL_METHOD EXPORTING METHOD = 'LoadFile' P_COUNT = 1 P1 = FILENAME. ENDMETHOD. METHOD REFRESH. CALL METHOD ME->CALL_METHOD EXPORTING METHOD = 'Refresh' P_COUNT = 0. ENDMETHOD. METHOD DISPATCH. CALL METHOD CL_GUI_CFW=>FLUSH. IF SY-SUBRC NE 0. RAISE CNTL_ERROR. ENDIF. ENDMETHOD. ENDCLASS. **** **** End of code for LZ5_DS_PDFTOP **** *____________________________________________________________________________________ **** **** Code for Function Module Z_DS_CREATE_LOCAL_PDF_FILE **** FUNCTION Z_DS_CREATE_LOCAL_PDF_FILE . *"---------------------------------------------------------------------- *"*"Local interface: *" EXPORTING *" REFERENCE(AFILENAME) LIKE RLGRAP-FILENAME *" TABLES *" OTF_LINES STRUCTURE ITCOO *"---------------------------------------------------------------------- DATA: PDF_LINES LIKE TLINE OCCURS 1000 WITH HEADER LINE, ARCH LIKE TOA_DARA, NO_LINES TYPE I. CALL FUNCTION 'CONVERT_OTF' EXPORTING FORMAT = 'PDF' IMPORTING BIN_FILESIZE = NO_LINES TABLES OTF = OTF_LINES LINES = PDF_LINES. CALL FUNCTION 'DOWNLOAD' EXPORTING BIN_FILESIZE = NO_LINES FILENAME = 'c:test.pdf' FILETYPE = 'BIN' IMPORTING ACT_FILENAME = AFILENAME TABLES DATA_TAB = PDF_LINES. ENDFUNCTION. **** **** End of Code for Z_DS_CREATE_LOCAL_PDF_FILE **** *____________________________________________________________________________________ **** **** Code for Function Module Z_DS_CALL_PDF_VIEWER **** FUNCTION Z_DS_CALL_PDF_VIEWER . *"---------------------------------------------------------------------- *"*"Local interface: *" IMPORTING *" VALUE(FILENAME) TYPE STRING *"---------------------------------------------------------------------- FILE_NAME = FILENAME. IF MY_CONTAINER IS INITIAL. CREATE OBJECT MY_CONTAINER EXPORTING CONTAINER_NAME = 'MY_CONTAINER'. CREATE OBJECT MY_PDF EXPORTING NAME = 'MY_PDF' PARENT = MY_CONTAINER. ENDIF. CALL SCREEN 901. " Ensure screen is created as per instructions ENDFUNCTION. **** **** End of Code for Z_DS_CALL_PDF_VIEWER **** *____________________________________________________________________________________ **** **** Flow Logic for screen '0901' **** PROCESS BEFORE OUTPUT. MODULE INIT. PROCESS AFTER INPUT. MODULE USER_COMMAND_0901. **** **** End of Flow Logic for screen '0901' **** *____________________________________________________________________________________ **** **** PBO module INIT for screen '0901' **** MODULE init OUTPUT. call method my_pdf->loadfile exporting filename = file_name. ENDMODULE. " init OUTPUT **** **** End of PBO module INIT for screen '0901' **** *____________________________________________________________________________________ **** **** PAI module USER_COMMAND_901 for screen '0901' **** MODULE USER_COMMAND_0901 INPUT. case ok_code. when 'CLO'. set screen 0. endcase. ENDMODULE. " USER_COMMAND_0901 INPUT **** **** End of PAI module USER_COMMAND_901 for screen '0901' **** *____________________________________________________________________________________ **** **** Example program Z5_DS_SCRIPT2PDF **** *&---------------------------------------------------------------------* *& Report Z5_DS_SCRIPT2PDF * *& * *&---------------------------------------------------------------------* *& This report works only if the function modules * *& Z_DS_CREATE_LOCAL_PDF_FILE and Z_DS_CALL_PDF_VIEWER already exist * *& Also use an already existing simple SAPScript Form that contains a * *& window "MAIN" and rework printing code if necessary, remember to * *& change the output device name in OPTIONS-TDDEST * *& * *&---------------------------------------------------------------------* REPORT Z5_DS_SCRIPT2PDF. *----------------------------------------------------------------------* PARAMETERS: FORM LIKE RSSCF-TDFORM DEFAULT 'Z5_DS_HELLO2'. "your form DATA: OTF_LINES LIKE ITCOO OCCURS 1000 WITH HEADER LINE, OPTIONS TYPE ITCPO, FILENAME LIKE RLGRAP-FILENAME, FILENAME_S TYPE STRING. START-OF-SELECTION. OPTIONS-TDDEST = 'LP01'. * Replace 'LP01' above with your default output device OPTIONS-TDCOPIES = 1. OPTIONS-TDGETOTF = 'X'. " the key to returning OTF data ************************************************************************ * Open the SapScript Form with the name "form" * ************************************************************************ CALL FUNCTION 'OPEN_FORM' EXPORTING FORM = FORM " name of form (SE71) OPTIONS = OPTIONS DIALOG = ' '. ************************************************************************ * Execute the element "HELLO" in window MAIN * - Nothing happens if /E HELLO is not declared in MAIN ************************************************************************ CALL FUNCTION 'WRITE_FORM' EXPORTING ELEMENT = 'HELLO' "execute element /E HELLO TYPE = 'BODY'. "normal output ************************************************************************ * Close the current SapScript Form ************************************************************************ CALL FUNCTION 'CLOSE_FORM' TABLES OTFDATA = OTF_LINES. " Retrieve all the OTF so far ************************************************************************ * Code for PDF Formatting and creation of local File ************************************************************************ CALL FUNCTION 'Z_DS_CREATE_LOCAL_PDF_FILE' IMPORTING AFILENAME = FILENAME TABLES OTF_LINES = OTF_LINES. FILENAME_S = FILENAME. ************************************************************************ * Code to launch Adobe Acrobat inplace in SAPGUI ************************************************************************ CALL FUNCTION 'Z_DS_CALL_PDF_VIEWER' EXPORTING FILENAME = FILENAME_S. **** **** End of example program Z5_DS_SCRIPT2PDF **** *____________________________________________________________________________________