Monday, October 29, 2007

Convert Smartforms to PDF and save as local file

*&---------------------------------------------------------------------*
*& Report Z_SF_PDF
*&---------------------------------------------------------------------*
*& + This program demonstrates how easy it is to convert Smartforms to
*& and save as local file
*& + This example also demostrates how to skip the PRINT PREVIEW screen
*& where we give the default printer name. The name is set in the program.
*&
*& NOTE: The smartform called here is an example with no relation to the
*& standard forms. You will have to do the Smartform data population
*& befoer you use the code for PDF Conversion. Data can be sent to the
*& smartform through the Function Module G_FM_NAME's EXPORTING or
*& TABLES section
*&---------------------------------------------------------------------*
*& Written by:
*& URL: http://allaboutsap.blogspot.com
*&---------------------------------------------------------------------*
REPORT Z_SF_PDF.

*--- Internal tables, Structures and Variables used for PDF conversion
DATA: IT_OTF TYPE STANDARD TABLE OF ITCOO,
IT_DOCS TYPE STANDARD TABLE OF DOCS,
IT_LINES TYPE STANDARD TABLE OF TLINE,
ST_JOB_OUTPUT_INFO TYPE SSFCRESCL,
ST_DOCUMENT_OUTPUT_INFO TYPE SSFCRESPD,
ST_JOB_OUTPUT_OPTIONS TYPE SSFCRESOP,
ST_OUTPUT_OPTIONS TYPE SSFCOMPOP,
ST_CONTROL_PARAMETERS TYPE SSFCTRLOP,
V_LEN_IN TYPE SO_OBJ_LEN,
V_LANGUAGE TYPE SFLANGU VALUE 'E',
V_E_DEVTYPE TYPE RSPOPTYPE,
V_BIN_FILESIZE TYPE I,
V_NAME TYPE STRING,
V_PATH TYPE STRING,
V_FULLPATH TYPE STRING,
V_FILTER TYPE STRING,
V_UACT TYPE I,
V_GUIOBJ TYPE REF TO CL_GUI_FRONTEND_SERVICES,
V_FILENAME TYPE STRING.


*--- Perform to download the smartform as PDF
*--- This can be placed after the Form data population routines
PERFORM DL_SMARTFORM.

*&---------------------------------------------------------------------*
*& Form DL_SMARTFORM
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text *----------------------------------------------------------------------*
FORM DL_SMARTFORM .
*--- Data Declarations
data: C_FORMNAME TYPE TDSFNAME.
DATA: G_FM_NAME TYPE RS38L_FNAM.
c_formname = 'Z_SF2PDF_EXMPL'.

*--- To get output device type
CALL FUNCTION 'SSF_GET_DEVICE_TYPE'
EXPORTING
I_LANGUAGE = V_LANGUAGE
I_APPLICATION = 'SAPDEFAULT'
IMPORTING
E_DEVTYPE = V_E_DEVTYPE.
ST_OUTPUT_OPTIONS-TDPRINTER = V_E_DEVTYPE.
ST_CONTROL_PARAMETERS-NO_DIALOG = 'X'.
ST_CONTROL_PARAMETERS-GETOTF = 'X'.

*--- Call Function to get the Function Module name for the smart form
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
FORMNAME = C_FORMNAME
IMPORTING
FM_NAME = G_FM_NAME.
IF SY-SUBRC <> 0.
*--- Check for Errors
ENDIF.

*--- Call Smartform function module without hard coding
CALL FUNCTION G_FM_NAME
EXPORTING
CONTROL_PARAMETERS = ST_CONTROL_PARAMETERS
OUTPUT_OPTIONS = ST_OUTPUT_OPTIONS
* USER_SETTINGS = 'X'
* ADD YOUR DATA STRUCTURES HERE
IMPORTING
DOCUMENT_OUTPUT_INFO = ST_DOCUMENT_OUTPUT_INFO
JOB_OUTPUT_INFO = ST_JOB_OUTPUT_INFO
JOB_OUTPUT_OPTIONS = ST_JOB_OUTPUT_OPTIONS
* TABLES
* ADD YOUR DATA TABLES
EXCEPTIONS
FORMATTING_ERROR = 1
INTERNAL_ERROR = 2
SEND_ERROR = 3
USER_CANCELED = 4
OTHERS = 5
.
IF SY-SUBRC <> 0.
*--- On Failure display standard Message
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
else.

*--- Convert OTF to PDF
CALL FUNCTION 'CONVERT_OTF_2_PDF'
IMPORTING
BIN_FILESIZE = V_BIN_FILESIZE
TABLES
OTF = ST_JOB_OUTPUT_INFO-OTFDATA
DOCTAB_ARCHIVE = IT_DOCS
LINES = IT_LINES
EXCEPTIONS
ERR_CONV_NOT_POSSIBLE = 1
ERR_OTF_MC_NOENDMARKER = 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.

*--- Get filename to store
CONCATENATE 'Converted_Smartform' '.pdf' INTO V_NAME.
CREATE OBJECT V_GUIOBJ.
CALL METHOD V_GUIOBJ->FILE_SAVE_DIALOG
EXPORTING
DEFAULT_EXTENSION = 'pdf'
DEFAULT_FILE_NAME = V_NAME
FILE_FILTER = V_FILTER
CHANGING
FILENAME = V_NAME
PATH = V_PATH
FULLPATH = V_FULLPATH
USER_ACTION = V_UACT.
IF V_UACT = V_GUIOBJ->ACTION_CANCEL.
EXIT.
ENDIF.

*--- Download PDF to local PC
MOVE V_FULLPATH TO V_FILENAME.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
BIN_FILESIZE = V_BIN_FILESIZE
FILENAME = V_FILENAME
FILETYPE = 'BIN'
TABLES
DATA_TAB = IT_LINES
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.
ENDIF.

ENDFORM. " DL_SMARTFORM

1 comment:

Anonymous said...

I can't wait for more articles! When will you update your interesting site?