Sie sind auf Seite 1von 7

7/5/2018 PDF Download By Creating Spool Request - ABAP Development - SCN Wiki

Getting Started Store

Community WIKI SAP Community Welcome, Guest Login Register Search the Community

ABAP Development / ABAP Development

PDF Download By Creating Spool Request


Created by Former Member, last modified by Cristiano Silveira on Aug 06, 2012

This document will help you all to create a Pdf download of classical report by generating a Spool
Request .
Basic Steps .
1) Generate the classical Report output .
2) Create a Spool Request .
3) Search the Spool Request .
4) Submit the Spool Request for Pdf Creation .
5) Download the Pdf Output in Presentation server or Application Server .

Detail Steps

1) Generate the classical Report output .

2) Create a Spool Request .


By Submitting the report itself to SAP-SPOOL you can create a spool request .
SUBMIT (p_repid) TO SAP-SPOOL
SPOOL PARAMETERS mstr_print_parms
WITHOUT SPOOL DYNPRO
AND RETURN.

but it will create infinite loop , so use abap memory and export a flag variable with a value to restricted the excution of the above submit only once and all the internal table and other
data which you want in your program . below is the example .
* importing variable value set at first time to restrict the infinite loop .
IMPORT var FROM MEMORY ID 'abc' .

var = var + 1 .
*checking variable to restricted scecond time exction of this block of code
* *IF var = 1 .
v_memid = 1 .
EXPORT var TO MEMORY ID 'abc' . (flag variable to restrict the excution only once )
*EXPORT it_pernr it_pernr_quali v_memid TO MEMORY ID 'bcd' . (internal table and other data)*
* submitting the spool request
SUBMIT (p_repid) TO SAP-SPOOL
SPOOL PARAMETERS mstr_print_parms
WITHOUT SPOOL DYNPRO
AND RETURN.
ENDIF.

3) Search the Spool Request .


Table which store the spool Request is TSP01 . To find the latest spool Request of that program we use a database table(TSP01) field ( rq2name ) . it is of 12 char length and contains first
9 character of report name and user name like (p3d , m5l) .
the below logic will create a vriable with value
DATA:
lc_rq2name LIKE tsp01-rq2name.
*consutrucing the database variable rq2name to search the spool request
IF v_len >= 9 .
CONCATENATE p_repid+0(9)
p_uname+0(3) INTO lc_rq2name .
ELSE.
v_len1 = 9 - v_len .
DO v_len1 TIMES .
CONCATENATE v_temp '_' INTO v_temp .
ENDDO.
CONCATENATE p_repid v_temp
p_uname INTO lc_rq2name .
ENDIF.
it will help us to find the spool request from TSP01 .
*selecting the spool request using the above consructed varibale
SELECT * FROM tsp01 INTO{} TABLE it_tsp01
* *WHERE rq2name = lc_rq2name .

now sort the internal table by descending order of time .


*sorting the interbla table
* *SORT it_tsp01 BY rqcretime DESCENDING .
read the first index of internal table which contains the latest spool request .
READ TABLE it_tsp01 INDEX 1.

https://wiki.scn.sap.com/wiki/display/ABAP/PDF+Download+By+Creating+Spool+Request 1/7
7/5/2018 PDF Download By Creating Spool Request - ABAP Development - SCN Wiki
4) Submit the Spool Request for Pdf Creation .
set the printer configuration
DATA:
mstr_print_parms LIKE pri_params,
mc_valid(1) TYPE c,
mi_bytecount TYPE i,
mi_length TYPE i,
mi_rqident LIKE tsp01-rqident.
*-- INTERNAL TABLES
DATA:
mtab_pdf LIKE tline OCCURS 0 WITH HEADER LINE,
mc_filename LIKE rlgrap-filename.
*-- SELECTION SCREEN
DATA:
p_linsz LIKE sy-linsz VALUE 132, " Line size
p_paart LIKE sy-paart VALUE 'X_65_132'. " Paper Format

*-- Setup the Print Parmaters


CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
authority = space
copies = '1'
cover_page = space
data_set = space
department = space
destination = space
expiration = '1'
immediately = space
new_list_id = 'X'
no_dialog = 'X'
user = sy-uname
IMPORTING
out_parameters = mstr_print_parms
valid = mc_valid
EXCEPTIONS
archive_info_not_found = 1
invalid_print_params = 2
invalid_archive_params = 3
OTHERS = 4.

*-- Make sure that a printer destination has been set up


*-- If this is not done the PDF function module ABENDS
IF mstr_print_parms-pdest = space.
mstr_print_parms-pdest = 'LP01'.
ENDIF.
*-- Explicitly set line width, and output format so that
*-- the PDF conversion comes out OK
mstr_print_parms-linsz = p_linsz.
mstr_print_parms-paart = p_paart.

call the function module CONVERT_ABAPSPOOLJOB_2_PDF . and pass spool rquest no .


*converting the spool request into pdf
* *CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
src_spoolid = it_tsp01-rqident
dst_device = 'LP01'
TABLES
pdf = it_pdf
EXCEPTIONS
err_no_abap_spooljob =1
err_no_spooljob =2
err_no_permission =3
err_conv_not_possible =4
err_bad_destdevice =5
user_cancelled =6
err_spoolerror =7
err_temseerror =8
err_btcjob_open_failed = 9
err_btcjob_submit_failed = 10
err_btcjob_close_failed = 11
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*.*
5) Download the Pdf Output in Presentation server or Application Server .
Presentation server
*download the pdf file in presentation server

https://wiki.scn.sap.com/wiki/display/ABAP/PDF+Download+By+Creating+Spool+Request 2/7
7/5/2018 PDF Download By Creating Spool Request - ABAP Development - SCN Wiki
* *DATA: l_file TYPE string .
l_file = p_file .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = l_file
filetype = 'BIN'
TABLES
data_tab = it_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.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

or Application Server
*downloading the pdf file in applecation server
OPEN DATASET p_file FOR OUTPUT IN BINARY MODE .
IF sy-subrc = 0 .
LOOP AT it_pdf .
TRANSFER it_pdf TO p_file .
ENDLOOP.
CLOSE DATASET p_file .
ELSE.
WRITE : / 'operating system could not open file' .
ENDIF.

Below is the part sample code which generating pdf .

1) * importing v_memid flag form memory id to getting the internal tables


* filled at first time .

* *IMPORT v_memid FROM MEMORY ID 'bcd' .


IF v_memid = 1 .
*importing the internal table filled at first time .
* *IMPORT it_pernr it_pernr_quali p_file FROM MEMORY ID 'bcd' .

*setting check box value second time


px_pdf = 'X'. (Selection Screen check Box for PDF Downlaod)
ENDIF.
2) genrete the classical output .

3) now use this code . (Pdf Genretion )


DATA: spoolno LIKE tsp01-rqident.
DATA : p_repid LIKE sy-repid .
DATA: var TYPE i VALUE 0 .
DATA: v_len TYPE i , v_len1 TYPE i .
DATA: v_temp(8) TYPE c .
DATA: p_uname LIKE sy-uname .
DATA: it_tsp01 TYPE STANDARD TABLE OF tsp01 WITH HEADER LINE .
DATA: it_pdf LIKE tline OCCURS 0 WITH HEADER LINE .
TABLES: tsp01.

*-- STRUCTURES
DATA:
lc_rq2name LIKE tsp01-rq2name.

https://wiki.scn.sap.com/wiki/display/ABAP/PDF+Download+By+Creating+Spool+Request 3/7
7/5/2018 PDF Download By Creating Spool Request - ABAP Development - SCN Wiki
DATA:
mstr_print_parms LIKE pri_params,
mc_valid(1) TYPE c,
mi_bytecount TYPE i,
mi_length TYPE i,
mi_rqident LIKE tsp01-rqident.
*-- INTERNAL TABLES
DATA:
mtab_pdf LIKE tline OCCURS 0 WITH HEADER LINE,
mc_filename LIKE rlgrap-filename.
*-- SELECTION SCREEN
DATA:
p_linsz LIKE sy-linsz VALUE 132, " Line size
p_paart LIKE sy-paart VALUE 'X_65_132'. " Paper Format

*-- Setup the Print Parmaters


CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
authority = space
copies = '1'
cover_page = space
data_set = space
department = space
destination = space
expiration = '1'
immediately = space
new_list_id = 'X'
no_dialog = 'X'
user = sy-uname
IMPORTING
out_parameters = mstr_print_parms
valid = mc_valid
EXCEPTIONS
archive_info_not_found = 1
invalid_print_params = 2
invalid_archive_params = 3
OTHERS = 4.

*-- Make sure that a printer destination has been set up


*-- If this is not done the PDF function module ABENDS
IF mstr_print_parms-pdest = space.
mstr_print_parms-pdest = 'LP01'.
ENDIF.
*-- Explicitly set line width, and output format so that
*-- the PDF conversion comes out OK
mstr_print_parms-linsz = p_linsz.
mstr_print_parms-paart = p_paart.

* importing variable value set at first time to restrict the infinite loop .
IMPORT var FROM MEMORY ID 'abc' .

var = var + 1 .
p_uname = sy-uname .
p_repid = sy-repid .
*checking variable to restricted scecond time exction of this block of code
IF var = 1 .
v_memid = 1 .
EXPORT var TO MEMORY ID 'abc' .
EXPORT it_pernr it_pernr_quali v_memid p_file TO MEMORY ID 'bcd' .
* submitting the spool request
SUBMIT (p_repid) TO SAP-SPOOL
SPOOL PARAMETERS mstr_print_parms
WITHOUT SPOOL DYNPRO
AND RETURN.
ENDIF.

FREE MEMORY ID 'abc'.


FREE MEMORY ID 'bcd'.
*Calculating the lenth of report name
v_len = STRLEN( p_repid ) .
*consutrucing the database variable rq2name to search the spool request
IF v_len >= 9 .
CONCATENATE p_repid+0(9)
p_uname+0(3) INTO lc_rq2name .
ELSE.
v_len1 = 9 - v_len .
DO v_len1 TIMES .
CONCATENATE v_temp '_' INTO v_temp .
ENDDO.

https://wiki.scn.sap.com/wiki/display/ABAP/PDF+Download+By+Creating+Spool+Request 4/7
7/5/2018 PDF Download By Creating Spool Request - ABAP Development - SCN Wiki
CONCATENATE p_repid v_temp
p_uname INTO lc_rq2name .
ENDIF.

*selecting the spool request using the above consructed varibale


SELECT {}FROM{} tsp01 {}INTO{} {}TABLE{} it_tsp01*
WHERE rq2name = lc_rq2name .
*sorting the interbla table
SORT it_tsp01 BY rqcretime DESCENDING .
*reading the first spool request
READ TABLE it_tsp01 INDEX 1.

*converting the spool request into pdf


CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
src_spoolid = it_tsp01-rqident
dst_device = 'LP01'
TABLES
pdf = it_pdf
EXCEPTIONS
err_no_abap_spooljob =1
err_no_spooljob =2
err_no_permission =3
err_conv_not_possible =4
err_bad_destdevice =5
user_cancelled =6
err_spoolerror =7
err_temseerror =8
err_btcjob_open_failed = 9
err_btcjob_submit_failed = 10
err_btcjob_close_failed = 11
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.

*download the pdf file in presentation server


DATA: l_file TYPE string .
l_file = p_file .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = l_file
filetype = 'BIN'
TABLES
data_tab = it_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.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

https://wiki.scn.sap.com/wiki/display/ABAP/PDF+Download+By+Creating+Spool+Request 5/7
7/5/2018 PDF Download By Creating Spool Request - ABAP Development - SCN Wiki
*downloading the pdf file in applecation server
OPEN DATASET p_file FOR OUTPUT IN BINARY MODE .
IF sy-subrc = 0 .
LOOP AT it_pdf .
TRANSFER it_pdf TO p_file .
ENDLOOP.
CLOSE DATASET p_file .
ELSE.
WRITE : / 'operating system could not open file' .
ENDIF.

PDF outupt

tutorial abap spool print pdf request

3 Comments
Former Member
HI Priyanka,

Thanks for good article. But there is an issue while copy this program in variable declaration.

Regards,

Madhu.

Former Member
Good.There are some good wikis on this topic.Is there any addition things you added.

Regards,

Madhu.

Former Member
Good.There are some good wikis on this topic.Is there any addition things you added.

Regards,

Madhu.

https://wiki.scn.sap.com/wiki/display/ABAP/PDF+Download+By+Creating+Spool+Request 6/7
7/5/2018 PDF Download By Creating Spool Request - ABAP Development - SCN Wiki

Contact Us SAP Help Portal


Privacy Terms of Use Legal Disclosure Copyright Follow SCN

https://wiki.scn.sap.com/wiki/display/ABAP/PDF+Download+By+Creating+Spool+Request 7/7

Das könnte Ihnen auch gefallen