Wednesday, April 12, 2017

SMARTFORMS: Output in PDF Format


Smart Forms does not support direct output in PDF format. Instead, the OTF output has to be converted to PDF. Fonts and code pages used (particularly with Asian languages) have to be correct in the OTF output and are dependent on the language in which the form is printed. Depending on the language, a different device type that supports the corresponding fonts has to be used for printing.

A device type for printing PDF or for creating a PDF file is therefore not adequate for a Smart Form that is used as a template for printing forms in a number of languages. The device type has to be set dynamically before the Smart Forms function module is called.
Activities
Another option is for Smart Forms to return OTF and for you to convert it to PDF yourself:
  1. Get the name of the generated function module using the function module SSF_FUNCTION_MODULE_NAME
  2. To get a device type suitable for the current language, call the function module SSF_GET_DEVICE_TYPE.
  3. Set the device type using the parameter TDPRINTER of the standard parameter OUTPUT_OPTIONS (see also: Output Options). Transfer this parameter, together with the parameters for the OTF output, to the generated Smart Forms function module, as described under Returning a Form as a Table to the Application Program.
  4. Get the OTF output from table OTFDATA of the standard parameter JOB_OUTPUT_INFO (see also: Querying the Output Result).
  5. To convert the OTF output to PDF, transfer the OTF table to the function module CONVERT_OTF. To do this, set the parameter FORMAT to 'PDF'. The output can be returned either as a binary string (parameter BIN_FILE) or as a character table (parameter LINES). SAP recommends the first variant.
You can then store the PDF output using the function module GUI_DOWNLOAD, for example, as a local file on your PC.



Example code:


REPORT zsuresh_test.

* Variable declarations
DATA:
  w_form_name  TYPE tdsfname VALUE 'ZSURESH_TEST',
  w_fmodule        TYPE rs38l_fnam,
  w_cparam        TYPE ssfctrlop,
  w_bin_filesize   TYPE i, " Binary File Size
  w_FILE_NAME type string,
  w_File_path      type string,
  w_FULL_PATH type string.

* Internal tables declaration

* Internal table to hold the OTF data 
DATA:
  t_otf TYPE itcoo OCCURS 0 WITH HEADER LINE,

* Internal table to hold OTF data recd from the SMARTFORM
  t_otf_from_fm TYPE ssfcrescl,

* Internal table to hold the data from the FM CONVERT_OTF
T_pdf_tab LIKE tline OCCURS 0 WITH HEADER LINE.


* This function module call is used to retrieve the name of the Function
* module generated when the SMARTFORM is activated


CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname = w_form_name
  IMPORTING
    fm_name  = w_fmodule
  EXCEPTIONS
    no_form    = 1
    no_function_module = 2
    others       = 3
  .
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

* Calling the SMARTFORM using the function module retrieved above
* GET_OTF parameter in the CONTROL_PARAMETERS is set to get the OTF
* format of the output
w_cparam-no_dialog = 'X'.
w_cparam-preview    = space. " Suppressing the dialog box
                                                    " for print preview
w_cparam-getotf       = 'X'.


CALL FUNCTION w_fmodule
  EXPORTING
    control_parameters  = w_cparam
    output_data              = .....
  IMPORTING
    job_output_info   = t_otf_from_fm
  EXCEPTIONS
    formatting_error = 1
    internal_error = 2
    send_error = 3
    user_canceled = 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.

t_otf[] = t_otf_from_fm-otfdata[].

* Function Module CONVERT_OTF is used to convert the OTF format to PDF
CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
    FORMAT = 'PDF'
    MAX_LINEWIDTH = 132
  IMPORTING
    BIN_FILESIZE = W_bin_filesize
  TABLES
    otf = T_OTF
    lines = T_pdf_tab
  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.

* To display File SAVE dialog window
CALL METHOD cl_gui_frontend_services=>file_save_dialog
  CHANGING
    filename = w_FILE_NAME
    path = w_FILE_PATH
    fullpath = w_FULL_PATH
  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.


* Use the FM GUI_DOWNLOAD to download the generated PDF file onto the
* presentation server

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    BIN_FILESIZE = W_bin_filesize
    filename = w_FULL_PATH
    FILETYPE = 'BIN'
  TABLES
    data_tab = T_pdf_tab
  .
 IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
 ENDIF.

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...