Introduction to Downloading a Report Output in SAP Without Using Spool Request
In SAP, we often need to call another report programmatically and download its output directly to the local system without sending it to the spool request. This is useful when:
We need to automate report execution and download without user interaction. We do not want to store data in the spool (e.g., for security or performance reasons). We need to directly save the report output as a file in a readable format (e.g., .txt, .csv, .xlsx).
SAP provides standard ABAP statements and function modules to execute another report and capture its output without using the spool.
Business Scenario.
To use material data i need download that report into pdf format without using spool request.
The report should be downloaded as a file on the userβs local machine. Avoid sending the report output to the spool (SP01) to reduce unnecessary spool entries.
Implementation:
Use SUBMIT to execute the report and store the output in memory.
Retrieve the output using LIST_FROM_MEMORY. Convert the output into an internal table using LIST_TO_ASCI. Download the file using GUI_DOWNLOAD. REPORT ZKOU_RP_MAIN_PROGRAM.
DATA : g_werks type marc-werks,
g_matnr TYPE mara-matnr.
SELECTION-SCREEN BEGIN OF BLOCK b WITH FRAME.
SELECT-OPTIONS : s_werks for g_werks,
s_matnr FOR g_matnr.
SELECTION-SCREEN end of BLOCK b.
START-OF-SELECTION.
select mara~matnr,
marc~werks,
makt~MAKTX
FROM mara INNER JOIN marc on mara~matnr = marc~matnr
INNER JOIN makt on mara~matnr = makt~matnr AND spras EQ -langu
INTO TABLE (gt_final)
WHERE mara~matnr in @s_matnr AND
marc~werks in @s_werks.
end-OF-SELECTION.
IF gt_final IS NOT INITIAL.
FORMAT COLOR OFF.
FORMAT COLOR COL_GROUP.
data(gv_header) = |Material No| && | | && |Plant| && | | && |Description|.
write : / gv_header.
FORMAT COLOR COL_TOTAL.
LOOP AT gt_final INTO DATA(gs_final).
data(gv_item) = gs_final-matnr && | | && gs_final-werks && | | && gs_final-maktx.
write : / gv_item.
ENDLOOP.
ENDIF.
*&βββββββββββββββββββββββ*
*& Report ZKOU_RP_SUB_PROGRAM
*&βββββββββββββββββββββββ*
*&
*&βββββββββββββββββββββββ*
REPORT ZKOU_RP_SUB_PROGRAM.
types : begin of ty_str1,
fieldname type char20, β Structure for declear fieldname and values of table
values type char20,
end of ty_str1.
types : begin of Material_Details,
Material_Details type string, β Structure using for Declear the table description
end of Material_Details.
data : g_werks type marc-werks,
g_matnr type mara-matnr,
lt_output type table of abaplist,
it_werks type range of marc-werks,
it_matnr type range of mara-matnr,
wa_werks like line of it_werks,
wa_matnr like line of it_matnr,
lt_string1 type table of char200,
gt_data type standard table of x255, βSDOK_SDATX(raw_data).
lt_fin type table of ty_str1 with header line,
lt_fin1 type table of ty_str1 with header line,
ls_fin type ty_str1,
lt_Material_Details type table of Material_Details,
ls_Material_Details type Material_Details,
curr_line type sy-tabix,
lv_file_path type string value βC:UsersuserDesktopOUTPUT17.PDFβ. βPath value for local system
select-options : s_werks for g_werks, β Selection screen for the particlur table
s_matnr for g_matnr.
wa_werks-sign = βIβ.
wa_werks-option = βEQβ.
wa_werks-low = s_werks-low. β fill the data for selction screen
wa_werks-high = s_Werks-high.
append wa_werks to it_werks.
wa_matnr-sign = βIβ.
wa_matnr-option = βEQβ.
wa_matnr-low = s_matnr-low.
wa_matnr-high = s_matnr-high.
append wa_matnr to it_matnr.
submit ZKOU_RP_MAIN_PROGRAM with s_werks in it_werks
with s_matnr in it_matnr exporting list to memory and return.
call function βLIST_FROM_MEMORYβ
tables
listobject = lt_output βfetching the data form memory
exceptions
not_found = 1
others = 2.
IF SY-SUBRC = 0.
call function βWRITE_LISTβ
* EXPORTING
* WRITE_ONLY = βXβ
tables
listobject = LT_OUTPUT
* EXCEPTIONS
* EMPTY_LIST = 1
* OTHERS = 2
.
if sy-subrc <> 0.
* Implement suitable error handling here
endif.
ENDIF.
call function βLIST_TO_ASCIβ
tables
listasci = lt_string1
listobject = lt_output β ascii value convert to string value
exceptions
empty_list = 1
list_index_invalid = 2
others = 3
.
if sy-subrc <> 0.
endif.
read table lt_string1 into data(lv_fields) index 3.
if sy-subrc = 0.
read table lt_string1 into data(lv_values) index 4.
ls_Material_Details-Material_Details = lv_fields.
append ls_Material_Details to lt_Material_Details. βMoved string data to internal table data
clear ls_Material_Details.
ls_Material_Details-Material_Details = lv_values.
append ls_Material_Details to lt_Material_Details.
endif.
data(lo_pdf) = new cl_apoc_pdf_generator( ).
lo_pdf->create_pdf(
exporting
input = lt_Material_Details
β using this class create pdf
receiving
pdf_binary_output = data(rv_bin)
).
call function βSCMS_XSTRING_TO_BINARYβ
exporting
buffer = rv_bin
tables βconvert the data form binary format
binary_tab = gt_data
.
call function βGUI_DOWNLOADβ
exporting
filename = lv_file_path
filetype = βBINβ βDownload in the pdf format
tables
data_tab = gt_data
*
.
if sy-subrc <> 0.
* Implement suitable error handling here
endif.
Output:-
β Introduction to Downloading a Report Output in SAP Without Using Spool Request In SAP, we often need to call another report programmatically and download its output directly to the local system without sending it to the spool request. This is useful when: We need to automate report execution and download without user interaction. We do not want to store data in the spool (e.g., for security or performance reasons). We need to directly save the report output as a file in a readable format (e.g., .txt, .csv, .xlsx). SAP provides standard ABAP statements and function modules to execute another report and capture its output without using the spool. Business Scenario. To use material data i need download that report into pdf format without using spool request. The report should be downloaded as a file on the userβs local machine. Avoid sending the report output to the spool (SP01) to reduce unnecessary spool entries. Implementation: Use SUBMIT to execute the report and store the output in memory. Retrieve the output using LIST_FROM_MEMORY. Convert the output into an internal table using LIST_TO_ASCI. Download the file using GUI_DOWNLOAD. REPORT ZKOU_RP_MAIN_PROGRAM.
DATA : g_werks type marc-werks,
g_matnr TYPE mara-matnr.
SELECTION-SCREEN BEGIN OF BLOCK b WITH FRAME.
SELECT-OPTIONS : s_werks for g_werks,
s_matnr FOR g_matnr.
SELECTION-SCREEN end of BLOCK b.
START-OF-SELECTION.
select mara~matnr,
marc~werks,
makt~MAKTX
FROM mara INNER JOIN marc on mara~matnr = marc~matnr
INNER JOIN makt on mara~matnr = makt~matnr AND spras EQ -langu
INTO TABLE (gt_final)
WHERE mara~matnr in @s_matnr AND
marc~werks in @s_werks.
end-OF-SELECTION.
IF gt_final IS NOT INITIAL.
FORMAT COLOR OFF.
FORMAT COLOR COL_GROUP.
data(gv_header) = |Material No| && | | && |Plant| && | | && |Description|.
write : / gv_header.
FORMAT COLOR COL_TOTAL.
LOOP AT gt_final INTO DATA(gs_final).
data(gv_item) = gs_final-matnr && | | && gs_final-werks && | | && gs_final-maktx.
write : / gv_item.
ENDLOOP.
ENDIF.
*&βββββββββββββββββββββββ*
*& Report ZKOU_RP_SUB_PROGRAM
*&βββββββββββββββββββββββ*
*&
*&βββββββββββββββββββββββ*
REPORT ZKOU_RP_SUB_PROGRAM.
types : begin of ty_str1,
fieldname type char20, β Structure for declear fieldname and values of table
values type char20,
end of ty_str1.
types : begin of Material_Details,
Material_Details type string, β Structure using for Declear the table description
end of Material_Details.
data : g_werks type marc-werks,
g_matnr type mara-matnr,
lt_output type table of abaplist,
it_werks type range of marc-werks,
it_matnr type range of mara-matnr,
wa_werks like line of it_werks,
wa_matnr like line of it_matnr,
lt_string1 type table of char200,
gt_data type standard table of x255, βSDOK_SDATX(raw_data).
lt_fin type table of ty_str1 with header line,
lt_fin1 type table of ty_str1 with header line,
ls_fin type ty_str1,
lt_Material_Details type table of Material_Details,
ls_Material_Details type Material_Details,
curr_line type sy-tabix,
lv_file_path type string value βC:UsersuserDesktopOUTPUT17.PDFβ. βPath value for local system
select-options : s_werks for g_werks, β Selection screen for the particlur table
s_matnr for g_matnr.
wa_werks-sign = βIβ.
wa_werks-option = βEQβ.
wa_werks-low = s_werks-low. β fill the data for selction screen
wa_werks-high = s_Werks-high.
append wa_werks to it_werks.
wa_matnr-sign = βIβ.
wa_matnr-option = βEQβ.
wa_matnr-low = s_matnr-low.
wa_matnr-high = s_matnr-high.
append wa_matnr to it_matnr.
submit ZKOU_RP_MAIN_PROGRAM with s_werks in it_werks
with s_matnr in it_matnr exporting list to memory and return.
call function βLIST_FROM_MEMORYβ
tables
listobject = lt_output βfetching the data form memory
exceptions
not_found = 1
others = 2.
IF SY-SUBRC = 0.
call function βWRITE_LISTβ
* EXPORTING
* WRITE_ONLY = βXβ
tables
listobject = LT_OUTPUT
* EXCEPTIONS
* EMPTY_LIST = 1
* OTHERS = 2
.
if sy-subrc <> 0.
* Implement suitable error handling here
endif.
ENDIF.
call function βLIST_TO_ASCIβ
tables
listasci = lt_string1
listobject = lt_output β ascii value convert to string value
exceptions
empty_list = 1
list_index_invalid = 2
others = 3
.
if sy-subrc <> 0.
endif.
read table lt_string1 into data(lv_fields) index 3.
if sy-subrc = 0.
read table lt_string1 into data(lv_values) index 4.
ls_Material_Details-Material_Details = lv_fields.
append ls_Material_Details to lt_Material_Details. βMoved string data to internal table data
clear ls_Material_Details.
ls_Material_Details-Material_Details = lv_values.
append ls_Material_Details to lt_Material_Details.
endif.
data(lo_pdf) = new cl_apoc_pdf_generator( ).
lo_pdf->create_pdf(
exporting
input = lt_Material_Details
β using this class create pdf
receiving
pdf_binary_output = data(rv_bin)
).
call function βSCMS_XSTRING_TO_BINARYβ
exporting
buffer = rv_bin
tables βconvert the data form binary format
binary_tab = gt_data
.
call function βGUI_DOWNLOADβ
exporting
filename = lv_file_path
filetype = βBINβ βDownload in the pdf format
tables
data_tab = gt_data
*
.
if sy-subrc <> 0.
* Implement suitable error handling here
endif.Output:- Read More Technology Blogs by Members articles
#SAP
#SAPTechnologyblog