Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Generating a PDF from a OTF (imported from CLOSE_FORM) and send it by mail.

bettinasilveira
Participant
0 Kudos

Hello!

I have a problem.

I'll try to explain it toy you.

I have a SapScript that the user has the option to send it to the printer or have a Print Preview. I need to send it by mail in PDF format.

If the user select PRINT, a spool id is created, I can get the OTF, generate the PDF and send it by mail. Everything OK with that.

The problem is if the user select PRINT PREVIEW I don't get a spool id. So, what I did is to get the OTF from the CLOSE_FORM MF and generate de PDF from there. I tried several FM to generate the PDF but for sure I'm doing something wrong, because or the PDF is corrupted or I have a problem with the parameters in the SO_NEW_DOCUMENT_ATT_SEND_API1 FM.

So, what I need it: Generate a PDF from a OTF imported from CLOSE_FORM FM and send it by mail.

Examples or the FM I can use are welcome.

I tried to do search here, but I couldn't find anything that may help me..

So, please... Show me the light!!!

Thanks!!!

Bettina

10 REPLIES 10

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is an example.

data: maildata   like sodocchgi1.
data: mailpack   like sopcklsti1 occurs 2 with header line.
data: mailhead   like solisti1 occurs 1 with header line.
data: mailbin    like solisti1 occurs 10 with header line.
data: mailtxt    like solisti1 occurs 10 with header line.
data: mailrec    like somlrec90 occurs 0  with header line.
data: solisti1   like solisti1 occurs 0 with header line.

data: lv_email_address type string.
data: tab_lines like sy-tabix.

data: begin of otf occurs 0.
        include structure itcoo .
data: end of otf.

Regards,

Rich Heilman

Edited by: Rich Heilman on Dec 9, 2009 2:58 PM

Splitting source code.... geezzz

0 Kudos

Part 2

call function 'CLOSE_FORM'
         importing
              result  = itcpp
         tables
              otfdata = otf
         exceptions
              others  = 1.

* Move OTF code to structure SOLI for email
    clear solisti1. refresh solisti1.
    loop at otf.
      solisti1-line = otf.
      append solisti1.
    endloop.

Regards,

Rich Heilman

0 Kudos

Part 3

* Send Report

  clear:    maildata, mailtxt, mailbin, mailpack, mailhead, mailrec.
  refresh:  mailtxt, mailbin, mailpack, mailhead, mailrec.

* Creation of the document to be sent File Name
  maildata-obj_name = 'REPORT'.

* Mail Subject
  maildata-obj_descr = 'Your Report'.

  describe table mailtxt lines tab_lines.
  read table mailtxt index tab_lines.
  maildata-doc_size = ( tab_lines - 1 ) * 255 + strlen( mailtxt ).

* Creation of the entry for the compressed document
  clear mailpack-transf_bin.
  mailpack-head_start = 1.
  mailpack-head_num = 0.
  mailpack-body_start = 1.
  mailpack-body_num = tab_lines.
  mailpack-doc_type = 'RAW'.
  append mailpack.

* Creation of the document attachment
  loop at solisti1.
    move-corresponding solisti1 to mailbin.
    append mailbin.
  endloop.

  describe table mailbin lines tab_lines.
  mailhead = 'REPORT.OTF'.
  append mailhead.

** Creation of the entry for the compressed attachment
  mailpack-transf_bin = 'X'.
  mailpack-head_start = 1.
  mailpack-head_num = 1.
  mailpack-body_start = 1.
  mailpack-body_num = tab_lines.
  mailpack-doc_type = 'OTF'.
  mailpack-obj_name = 'ATTACHMENT'.
  mailpack-obj_descr = 'Some Report'.
  mailpack-doc_size = tab_lines * 255.
  append mailpack.

  mailrec-receiver = lv_email_address.  "<-- a valid email address
  mailrec-rec_type  = 'U'.
  append mailrec.

* Sending the document
  call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
       exporting
            document_data              = maildata
            put_in_outbox              = 'X'
       tables
            packing_list               = mailpack
            object_header              = mailhead
            contents_bin               = mailbin
            contents_txt               = mailtxt
            receivers                  = mailrec
       exceptions
            too_many_receivers         = 1
            document_not_sent          = 2
            operation_no_authorization = 4
            others                     = 99.

Regards,

Rich Heilman

0 Kudos

Rich, I need to send the OTF converted to PDF...

0 Kudos

Yes, I understand that. This code will do that. There is an internal function which will be triggered creating the email, it will convert the OTF to PDF on the fly, and send the PDF attachment in the email.

REgards,

Rich Heilman

0 Kudos

So more specifically, in the framework itself, I believe it calls FM SX_OBJECT_CONVERT_OTF_PDF to convert the OTF to PDF when mail is actually generated.

Regards,

Rich Heilman

0 Kudos

Rich, I copy pasted your code and I replaced:

mailpack-doc_type = 'OTF'.

for

mailpack-doc_type = 'PDF'

and

mailhead = 'REPORT.OTF'.

for

mailhead = 'REPORT.PDF'.

But no luck yet...

When I open the attachment I get a message error:

"Adobe Reader could not open "nameofthefile.pdf" because it is either not a upported file or because the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded)."

With your original code, I receive a sap document that shows text.

0 Kudos

What release are you on?

Check notes:

698124

171698

Regards,.

Rich Heilman

0 Kudos

Rich.

Sap ECC 6.0.

I'll check those notes later...

Thanks!

0 Kudos

I solved this doing a 2nd run, generating a spool order, then to pdf and after that, deleteing that spool order.

Thanks for your time Rich!