*&---------------------------------------------------------------------*
*& Report ZQS50008. 将程式中write出来的语句down成pdf格式的一般用于排job
*& 如果是程式中是用write语句写的输出的数据,一般可以直接在界面上选择sap自带的下载成pdf的,
*& 但是如果要后台排job自动下载可以用如下方法来转换
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZQS50008.
TABLES ZT507.
DATA LS_PRI_PARAMS TYPE PRI_PARAMS.
DATA LT_SPOOL_REQUESTS TYPE STANDARD TABLE OF RSPORQ.
DATA LS_SPOOL_REQUESTS TYPE RSPORQ.
DATA LV_PARAM TYPE CHAR40.
DATA LV_SPOOL_ID TYPE RSPOID.
DATA LT_PDF TYPE STANDARD TABLE OF TLINE.
DATA LV_PDF_BYTECOUNT TYPE I.
DATA LV_FILETYPE TYPE CHAR10.
DATA LV_CODEPAGE TYPE ABAP_ENCODING.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
SELECT-OPTIONS: S_RETNO FOR ZT507-RETNO NO INTERVALS.
PARAMETERS: P_6 RADIOBUTTON GROUP GRP1,
P_4 RADIOBUTTON GROUP GRP1.
SELECTION-SCREEN END OF BLOCK B1.
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME.
PARAMETERS: C_1 AS CHECKBOX DEFAULT 'X'. "是否附加Invoice信息
PARAMETERS: C_2 AS CHECKBOX DEFAULT 'X'. "是否附加页码
SELECTION-SCREEN END OF BLOCK B2.
PARAMETERS P_PATH TYPE STRING.
START-OF-SELECTION.
* Import the current print parameters
CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
EXPIRATION = 1
IMMEDIATELY = SPACE
NO_DIALOG = 'X'
IMPORTING
OUT_PARAMETERS = LS_PRI_PARAMS
EXCEPTIONS
ARCHIVE_INFO_NOT_FOUND = 1
INVALID_PRINT_PARAMS = 2
INVALID_ARCHIVE_PARAMS = 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.
* Submit the report program into the spool
SUBMIT ZQB00095 TO SAP-SPOOL
SPOOL PARAMETERS LS_PRI_PARAMS
WITHOUT SPOOL DYNPRO
WITH S_RETNO IN S_RETNO
WITH P_6 = P_6
WITH P_4 = P_4
WITH C_1 = C_1
WITH C_2 = C_2
AND RETURN.
* Get a list of the current user's spool requests
CALL FUNCTION 'RSPO_FIND_SPOOL_REQUESTS'
EXPORTING
RQOWNER = SY-UNAME
TABLES
SPOOLREQUESTS = LT_SPOOL_REQUESTS
EXCEPTIONS
NO_PERMISSION = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Sort and read the first (newest) spool request
SORT LT_SPOOL_REQUESTS BY RQCRETIME DESCENDING.
READ TABLE LT_SPOOL_REQUESTS INTO LS_SPOOL_REQUESTS INDEX 1.
LV_SPOOL_ID = LS_SPOOL_REQUESTS-RQIDENT.
* Use that request to create a PDF
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
SRC_SPOOLID = LV_SPOOL_ID
IMPORTING
PDF_BYTECOUNT = LV_PDF_BYTECOUNT
TABLES
PDF = LT_PDF
EXCEPTIONS
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.
CONCATENATE P_PATH SY-DATUM SY-UZEIT'.pdf' INTO P_PATH.
LV_FILETYPE = 'BIN'.
LV_CODEPAGE = '8400'.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
BIN_FILESIZE = LV_PDF_BYTECOUNT
FILENAME = P_PATH
FILETYPE = LV_FILETYPE
* 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 = LV_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 = ' '
* WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE
* SHOW_TRANSFER_STATUS = ABAP_TRUE
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = LT_PDF
* 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.
* Implement suitable error handling here
ENDIF.
* Now the PDF binary data is in the variable lt_pdf
* and the length in variable lv_pdf_bytecount.
* Go ahead and attach that to an e-mail!