Wednesday, September 1, 2010

Convert the Spool into PDF and send to External Email ID

Convert the Spool into PDF and send to
External Email ID

Applies To:

SAP 4.7/Above

Article Summary

This report program will execute the given program with the specified variant in background and
convert the spool request into PDF and send it as attachment to the specified Email ID.

By: Elini.P
Title: ABAP
Date: 29 Apr 2005


This is the code:

report zpp430_report_in_pdf no standard page heading line-size 255.


*&---------------------------------------------------------------------*
*& Report ZPP430_REPORT_IN_PDF
*
*& *
*&---------------------------------------------------------------------*
*& Converts spool request into PDF document and emails it to *
*& recipicant. *
*& *
*& Execution *
*& ---------*
*& This program must be run as a background job in-order for the write *
*& commands to create a Spool request rather than be displayed on *
*& screen *
*&---------------------------------------------------------------------*


tables: tsp01.
parameter: p_email1 like somlreci1-receiver
default 'me.report@domain.co.in',
p_sender like somlreci1-receiver


default 'you.report@domain.co.in',
p_repid like sy-repid, " Report to execute
p_linsz like sy-linsz default 132, " Line size
p_paart like sy-paart default 'X_65_132', " Paper Format
p_slset like sy-slset, "Variant name
p_odescr like sodocchgi1-obj_descr,
p_adescr type so_obj_nam,
p_delspl as checkbox.


*DATA DECLARATION
data: gd_recsize type i.


* Spool IDs
types: begin of t_tbtcp.
include structure tbtcp.


types: end of t_tbtcp.


data: it_tbtcp type standard table of t_tbtcp initial size 0,
wa_tbtcp type t_tbtcp.


* Job Runtime Parameters
data: gd_eventid like tbtcm-eventid,
gd_eventparm like tbtcm-eventparm,
gd_external_program_active like tbtcm-xpgactive,
gd_jobcount like tbtcm-jobcount,
gd_jobname like tbtcm-jobname,
gd_stepcount like tbtcm-stepcount,
gd_error type sy-subrc,
gd_reciever type sy-subrc.


data: w_recsize type i,
mc_valid(1) type c.


data: gd_subject like sodocchgi1-obj_descr,
it_mess_bod like solisti1 occurs 0 with header line,
it_mess_att like solisti1 occurs 0 with header line,
gd_sender_type like soextreci1-adr_typ,
gd_attachment_desc type so_obj_nam,
gd_attachment_name type so_obj_des,
mi_rqident like tsp01-rqident.


* Spool to PDF conversions
data: gd_spool_nr like tsp01-rqident,
w_spool_nr like tsp01-rqident,
gd_destination like rlgrap-filename,
gd_bytecount like tst01-dsize,
gd_buffer type string.


data:
mstr_print_parms like pri_params.


* Binary store for PDF
data: begin of it_pdf_output occurs 0.
include structure tline.
data: end of it_pdf_output.


constants: c_dev like sy-sysid value 'DEV',
c_no(1) type c value ' ',
c_device(4) type c value 'LOCL'.


************************************************************************
*START-OF-SELECTION.
start-of-selection.


* Write statement to represent report output. Spool request is created
* if write statement is executed in background. This could also be an
* ALV grid which would be converted to PDF without any extra effort
* WRITE 'Hello World'.
* NEW-PAGE.
* COMMIT WORK.
* NEW-PAGE PRINT OFF.
* IF SY-BATCH EQ 'X'.
* PERFORM GET_JOB_DETAILS.
* PERFORM OBTAIN_SPOOL_ID.
************************************
*** Alternative way could be to submit another program and store spool
*** id into memory.
*submit ZSPOOLTOPDF2


* to sap-spool
* spool parameters %_print
* archive parameters %_print
* without spool dynpro
* and return.
************************************
call function 'GET_PRINT_PARAMETERS'


exporting
authority
copies
= space
= '1'
*
*
cover_page
data_set
department
destination
expiration
immediately
in_archive_parameters
in_parameters
layout
mode
new_list_id
= space
= space
= space
= space
= '1'
= space
= space
= space
= space
= space
= 'X'
no_dialog = 'X'
user
importing
out_parameters
valid
= sy-uname
= mstr_print_parms
= mc_valid
exceptions

archive_info_not_found = 1
invalid_print_params = 2
invalid_archive_params = 3
others = 4.


if mstr_print_parms-pdest = space.
mstr_print_parms-pdest = 'LOCL'.


endif.


mstr_print_parms-linsz = p_linsz.
mstr_print_parms-paart = p_paart.


submit (p_repid) to sap-spool without spool dynpro
spool parameters mstr_print_parms
using selection-set p_slset
and return.


* Get spool id from program called above
perform get_spool_number using sy-repid sy-uname changing mi_rqident.


* IMPORT w_spool_nr FROM MEMORY ID SY-REPID.
perform convert_spool_to_pdf.
perform process_email.


if p_delspl eq 'X'.
perform delete_spool.
endif.


if sy-sysid = c_dev.
wait up to 5 seconds.
submit rsconn01 with mode = 'INT'


with output = 'X'
and return.
endif.


* ELSE.
* SKIP.
* WRITE:/ 'Program must be executed in background in-order for spool'
*,
* 'request to be created.'.
* ENDIF.
*---------------------------------------------------------------------*
* FORM obtain_spool_id *
*---------------------------------------------------------------------*


form obtain_spool_id.
check not ( gd_jobname is initial ).
check not ( gd_jobcount is initial ).


select * from
tbtcp
into table it_tbtcp
where jobname = gd_jobname
and jobcount = gd_jobcount
and stepcount = gd_stepcount
and listident <> '0000000000'
order by jobname


jobcount
stepcount.


read table it_tbtcp into wa_tbtcp index 1.


if sy-subrc = 0.
message s004(zdd) with gd_spool_nr.
gd_spool_nr = wa_tbtcp-listident.
message s004(zdd) with gd_spool_nr.


else.
message s005(zdd).
endif.
endform. "OBTAIN_SPOOL_ID


*---------------------------------------------------------------------*
* FORM get_job_details *
*---------------------------------------------------------------------*


form get_job_details.


* Get current job details
call function 'GET_JOB_RUNTIME_INFO'


importing
eventid = gd_eventid
eventparm = gd_eventparm
external_program_active = gd_external_program_active
jobcount = gd_jobcount
jobname = gd_jobname
stepcount = gd_stepcount


exceptions
no_runtime_info = 1
others = 2.


endform. "GET_JOB_DETAILS


*---------------------------------------------------------------------*
* FORM convert_spool_to_pdf *
*---------------------------------------------------------------------*


form convert_spool_to_pdf.


call function 'CONVERT_ABAPSPOOLJOB_2_PDF'


exporting
src_spoolid = mi_rqident
no_dialog = c_no
dst_device = c_device


importing
pdf_bytecount = gd_bytecount
tables
pdf = it_pdf_output


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.


check sy-subrc = 0.


* Transfer the 132-long strings to 255-long strings
loop at it_pdf_output.
translate it_pdf_output using ' ~'.
concatenate gd_buffer it_pdf_output into gd_buffer.


endloop.


translate gd_buffer using '~ '.


do.
it_mess_att = gd_buffer.
append it_mess_att.
shift gd_buffer left by 255 places.
if gd_buffer is initial.


exit.
endif.
enddo.
endform. "CONVERT_SPOOL_TO_PDF


*---------------------------------------------------------------------*
* FORM process_email *
*---------------------------------------------------------------------*
form process_email.


describe table it_mess_att lines gd_recsize.
check gd_recsize > 0.
perform send_email using p_email1.


* perform send_email using p_email2.
endform. "PROCESS_EMAIL
*---------------------------------------------------------------------*
* FORM send_email *


*---------------------------------------------------------------------*
* --> p_email *
*---------------------------------------------------------------------*


form send_email using p_email.
check not ( p_email is initial ).


refresh it_mess_bod.


* Default subject matter
gd_subject = p_odescr.
gd_attachment_desc = p_adescr.


* CONCATENATE 'attach_name' ' ' INTO gd_attachment_name.
it_mess_bod = 'This is an automated report from SAP.'.
append it_mess_bod.
it_mess_bod = 'Please do not reply to this mail id.'.
append it_mess_bod.


*IT_MESS_BOD = 'For any clarification on the details of this report'
*.


* APPEND IT_MESS_BOD.
* IT_MESS_BOD = 'please contact Business Planning. Thank you'.
* APPEND IT_MESS_BOD.
* If no sender specified - default blank
if p_sender eq space.
gd_sender_type = space.
else.
gd_sender_type = 'INT'.
endif.


* Send file by email as .xls speadsheet
perform send_file_as_email_attachment
tables it_mess_bod
it_mess_att


using p_email
p_odescr
'PDF'
gd_attachment_name
gd_attachment_desc
p_sender
gd_sender_type


changing gd_error
gd_reciever.
endform. "SEND_EMAIL


*---------------------------------------------------------------------*
* FORM delete_spool *
*---------------------------------------------------------------------*
form delete_spool.


data: ld_spool_nr type tsp01_sp0r-rqid_char.


ld_spool_nr = gd_spool_nr.


check p_delspl <> c_no.
call function 'RSPO_R_RDELETE_SPOOLREQ'
exporting
spoolid = ld_spool_nr.
endform. "DELETE_SPOOL


*&---------------------------------------------------------------------*
*& Form SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*


* Send email
*----------------------------------------------------------------------*
form send_file_as_email_attachment tables it_message
it_attach


using p_email
p_mtitle
p_format
p_filename
p_attdescription
p_sender_address
p_sender_addres_type


changing p_error
p_reciever.


data: ld_error type sy-subrc,
ld_reciever type sy-subrc,
ld_mtitle like sodocchgi1-obj_descr,
ld_email like somlreci1-receiver,
ld_format type so_obj_tp ,
ld_attdescription type so_obj_nam ,
ld_attfilename type so_obj_des ,
ld_sender_address like soextreci1-receiver,
ld_sender_address_type like soextreci1-adr_typ,
ld_receiver like sy-subrc.


data:
t_packing_list like sopcklsti1 occurs 0 with header line,
t_contents like solisti1 occurs 0 with header line,
t_receivers like somlreci1 occurs 0 with header line,
t_attachment like solisti1 occurs 0 with header line,
t_object_header like solisti1 occurs 0 with header line,
w_cnt type i,
w_sent_all(1) type c,
w_doc_data like sodocchgi1.


ld_email = p_email.
ld_mtitle = p_mtitle.
ld_format = p_format.
ld_attdescription = p_attdescription.
ld_attfilename = p_filename.
ld_sender_address = p_sender_address.
ld_sender_address_type = p_sender_addres_type.


* Fill the document data.
w_doc_data-doc_size = 1.
* Populate the subject/generic message attributes
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle .
w_doc_data-sensitivty = 'F'.
* Fill the document data and get size of attachment
clear w_doc_data.
read table it_attach index w_cnt.
w_doc_data-doc_size =


( w_cnt - 1 ) * 255 + strlen( it_attach ).
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle.
w_doc_data-sensitivty = 'F'.
clear t_attachment.
refresh t_attachment.
t_attachment[] = it_attach[].


* Describe the body of the message
clear t_packing_list.
refresh t_packing_list.
t_packing_list-transf_bin = space.
t_packing_list-head_start = 1.
t_packing_list-head_num = 0.
t_packing_list-body_start = 1.
describe table it_message lines t_packing_list-body_num.
t_packing_list-doc_type = 'RAW'.
append t_packing_list.
* Create attachment notification
t_packing_list-transf_bin = 'X'.
t_packing_list-head_start = 1.
t_packing_list-head_num = 1.
t_packing_list-body_start = 1.
describe table t_attachment lines t_packing_list-body_num.


t_packing_list-doc_type
t_packing_list-obj_descr
t_packing_list-obj_name
t_packing_list-doc_size
append t_packing_list.


= ld_format.
= ld_attdescription.
= ld_attfilename.
= t_packing_list-body_num * 255.


* Add the recipients email address
clear t_receivers.
refresh t_receivers.
t_receivers-receiver = ld_email.
t_receivers-rec_type = 'U'.
t_receivers-com_type = 'INT'.
t_receivers-notif_del = 'X'.
t_receivers-notif_ndel = 'X'.
append t_receivers.
call function 'SO_DOCUMENT_SEND_API1'


exporting
document_data
put_in_outbox
sender_address
sender_address_type
commit_work


importing
sent_to_all


tables
packing_list
contents_bin
contents_txt
receivers


exceptions
too_many_receivers
document_not_sent
document_type_not_exist


= w_doc_data
= 'X'
= ld_sender_address
= ld_sender_address_type
= 'X'


= w_sent_all


= t_packing_list
= t_attachment
= it_message
= t_receivers


= 1
= 2
= 3


operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
others = 8.


* Populate zerror return code
ld_error = sy-subrc.
* Populate zreceiver return code
loop at t_receivers.
ld_receiver = t_receivers-retrn_code.
endloop.
endform. "SEND_FILE_AS_EMAIL_ATTACHMENT


*&---------------------------------------------------------------------*
*& Form GET_SPOOL_NUMBER
*&---------------------------------------------------------------------*


* text
*----------------------------------------------------------------------*
* -->P_SY_REPID text
* -->P_SY_UNAME text
* <--P_MI_RQIDENT text
*----------------------------------------------------------------------*
form get_spool_number using f_repid
f_uname
changing f_rqident.
data:
lc_rq2name like tsp01-rq2name.
concatenate f_repid+0(9)
f_uname+0(3)


into lc_rq2name.
select * from tsp01 where rq2name = lc_rq2name
order by rqcretime descending.


f_rqident = tsp01-rqident.


exit.
endselect.
if sy-subrc ne 0.


clear f_rqident.
endif.


endform. " GET_SPOOL_NUMBER


Disclaimer & Liability Notice

This document may discuss sample coding, which does not include official interfaces and
therefore is not supported. Changes made based on this information are not supported and can
be overwritten during an upgrade.

SAP will not be held liable for any damages caused by using or misusing of the code and methods
suggested here, and anyone using these methods, is doing it under his/her own responsibility.

SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the
content of the technial article, including any liability resulting from incompatibility between the
content of the technical article and the materials and services offered by SAP. You agree that you
will not hold SAP responsible or liable with respect to the content of the Technical Article or seek
to do so.

Copyright © 2004 SAP AG, Inc. All Rights Reserved. SAP, mySAP, mySAP.com, xApps,
xApp, and other SAP products and services mentioned herein as well as their respective
logos are trademarks or registered trademarks of SAP AG in Germany and in several other
countries all over the world. All other product, service names, trademarks and registered
trademarks mentioned are the trademarks of their respective owners.

No comments:

Latest Gadget

Search