Hi again! As we have seen in the last Blog 'Creating a Smartform and Access it via ABAP Program' we can simply create a Smartform and access it via ABAP with whatever data we want.
Now often customers require a PDF version of their Smartform. Of course, you can simply enter Transactioncode pdf!from the Print Preview but that is not what I wanted and also not what a customer wants. Hence, there is a very simple way how to avoid this 'ugly' way of creating a PDF, namely the method I will present to you in the next few lines.
As already mentioned I will only cover the steps to create the PDF out of your Smartform and will not explain the whole procedure of creating a Smartform again.
Feel free to ask questions whenever you want.
Regards Patrick.
Again the code is commented so there is no need for more lines. Just keep in mind again that this is just a snippet of code so the way of filling the structures with data is not covered in this blog. The code is not formatted in order to ease the copying of code.
**&---------------------------------------------------------------------*
**& Report ZLP_DYN_DB
**&
**&---------------------------------------------------------------------*
**&
*&
**&---------------------------------------------------------------------*
*
REPORT ZLP_DYN_DB.
DATA: "needed to store information
ls_cars TYPE ZCARS,
ls_customers TYPE ZCUSTOMERS,
ls_reservations TYPE ZRESERVATIONS,
days TYPE I,
costs TYPE I.
* name of generated Function module (for the Smartform)
DATA: fm_name TYPE rs38l_fnam.
DATA: "Data for pdf creation
w_cparam TYPE ssfctrlop,
w_outoptions TYPE ssfcompop,
W_bin_filesize TYPE i,
w_FILE_NAME type string,
w_File_path type string,
w_FULL_PATH type string,
* Internal table to hold the OTF 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.
START-OF-SELECTION.
"calculate the costs just for fun
"days of car rental
days = ls_reservations-date_to - ls_reservations-date_from.
IF ls_cars-category = 'A'.
costs = days * 300.
ELSEIF ls_cars-category = 'B'.
costs = days * 200.
ELSEIF ls_cars-category = 'C'.
costs = days * 115.
ENDIF.
"code for calling function module
"to give data to smartform && PDF Creation
* ----------------------------------------------------------------------
* 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 = 'ZSF_CAR_BOOKING'
* VARIANT = ' '
* DIRECT_CALL = ' '
IMPORTING
fm_name = fm_name
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'." Suppressing the dialog box for print preview
w_cparam-preview = space.
w_cparam-getotf = 'X'.
w_cparam-langu = sy-langu.
CALL FUNCTION fm_name
EXPORTING
mandt = sy-mandt
name = ls_customers-name
car = ls_cars-car
category = ls_cars-category
license_plate = ls_cars-license_plate
reservation_number = ls_reservations-reservation_id
date_from = ls_reservations-date_from
date_to = ls_reservations-date_to
costsrate = costs
days_rental = days
"parameters for converting to pdf
"-----------------------------------
control_parameters = w_cparam
output_options = w_outoptions
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
* PDF MAGIC STARTS HERE
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
you can choose the default_file_name as you like
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_extension = 'pdf'
default_file_name = 'Smartform'
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
* what you have to do here is give the transformed data, the filesize and the filename
* those parameters are gathered by calling the methods above
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = w_bin_filesize
filename = w_full_path
filetype = 'BIN'
TABLES
data_tab = t_pdf_tab
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.
ELSE.
MESSAGE |PDF successfully saved!| TYPE 'I' DISPLAY LIKE 'E'.
ENDIF.