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: 

Error while sending spool as PDF attachment in background

Former Member
0 Kudos

Hi Expers,

I'm trying to use some Function Modules to download spool as an PDF attachment and send that attachment as a mail

I'm using the below mentioned logic.
But the PDF is not getting opened.

DATA : g_f_spool_nr TYPE tsp01-rqident,

        g_it_spool        TYPE TABLE OF soli WITH HEADER LINE,

        g_f_real_type     TYPE soodk-objtp.

DATA: BEGIN OF it_pdf_output OCCURS 0.

         INCLUDE STRUCTURE tline.

DATA: END OF it_pdf_output.

DATA : gd_buffer TYPE string.

  TYPES: BEGIN OF g_ty_objpack,

          rqnumber   TYPE rspotype-rqnumber,     " Spool request number

          head_start TYPE sopcklsti1-head_start, " Start line of obj. hdr

          head_num   TYPE sopcklsti1-head_num,   " Number of lines

          body_start TYPE sopcklsti1-body_start, " Start ln. of obj. cnts

          body_num   TYPE sopcklsti1-body_num,   " Number of lines

          no_lines   TYPE i,

         END OF g_ty_objpack.

   DATA:   g_f_no_lines         TYPE i,

           g_it_objbin          TYPE TABLE OF solisti1.

   DATA:   g_it_objpack   TYPE STANDARD TABLE OF g_ty_objpack,

           g_wa_objpack    TYPE g_ty_objpack,

           g_wa_document_data   TYPE sodocchgi1,

           g_wa_docsize         TYPE sy-index,

           g_it_message         TYPE TABLE OF solisti1 WITH HEADER LINE.

   DATA :  g_it_objtxt          TYPE TABLE OF solisti1 WITH HEADER LINE,

           g_wa_lines           TYPE sy-tabix,

           g_it_packing_list    TYPE TABLE OF sopcklsti1 WITH HEADER LINE.

     SELECT MAX( rqident ) INTO g_f_spool_nr FROM tsp01

             WHERE rqclient = sy-mandt

              AND   rqowner = sy-uname.

CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'

   EXPORTING

     SRC_SPOOLID                    = g_f_spool_nr

*   NO_DIALOG                      =

*   DST_DEVICE                     =

  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

           .

IF SY-SUBRC <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

LOOP AT it_pdf_output INTO it_pdf_output .

     TRANSLATE it_pdf_output USING ' ~'.

     CONCATENATE gd_buffer it_pdf_output INTO gd_buffer IN CHARACTER MODE.

   ENDLOOP.

TRANSLATE gd_buffer USING '~ ' .

    IF NOT it_pdf_output[] IS INITIAL.

     g_it_objbin[] = it_pdf_output[].

     DESCRIBE TABLE it_pdf_output LINES g_f_no_lines.

   ENDIF.

*Building table g_lt_objpack internal table for creating attachments

    g_wa_objpack-rqnumber   = g_f_spool_nr.

    g_wa_objpack-head_start = 1.

    g_wa_objpack-head_num   = 0.

    g_wa_objpack-body_start = 1.

    g_wa_objpack-no_lines   = g_f_no_lines.

    g_wa_objpack-body_num   = g_wa_objpack-no_lines.

   APPEND  g_wa_objpack TO  g_it_objpack.

*To format the text of the mail content

   g_wa_document_data-doc_size  =  g_wa_docsize.

   g_wa_document_data-obj_langu =  sy-langu.

   g_wa_document_data-obj_name  = 'Message'.

   MOVE 'LIABILITY REPORT' TO

                 g_wa_document_data-obj_descr.

   REFRESH  g_it_message.

    g_it_message  = 'Please find the attached list'.

   APPEND  g_it_message .

   " POPULATE_EMAIL_MESSAGE_BODY

   " descr_content_message .

     LOOP AT  g_it_message  .

     g_it_objtxt =  g_it_message-line.

     APPEND g_it_objtxt.

   ENDLOOP.

   DESCRIBE TABLE  g_it_objtxt LINES g_wa_lines.

   IF g_wa_lines GE 0.

     READ TABLE g_it_objtxt INTO g_it_objtxt INDEX g_wa_lines.

     g_wa_document_data-doc_size = ( g_wa_lines - 1 ) * 255 + STRLEN(

     g_it_objtxt ).

     g_wa_document_data-obj_langu = sy-langu.

     g_wa_document_data-sensitivty = 'F'.

   ELSE.

     g_wa_document_data = 0.

   ENDIF.

   g_it_packing_list-head_start = 1.

   g_it_packing_list-head_num = 1.

   g_it_packing_list-body_start = 0.

   g_it_packing_list-body_num = g_wa_lines.

   g_it_packing_list-doc_type = 'RAW'.

   g_it_packing_list-transf_bin = ' '.

   APPEND g_it_packing_list.

DESCRIBE TABLE it_pdf_output LINES g_wa_lines.

   IF NOT g_it_objpack[] IS INITIAL.

* Create attachment notification

     LOOP AT g_it_objpack INTO g_wa_objpack.

       g_it_packing_list-transf_bin = 'X'.

       g_it_packing_list-head_start = g_wa_objpack-head_start.

       g_it_packing_list-head_num   = g_wa_objpack-head_num.

       g_it_packing_list-body_start = g_wa_objpack-body_start.

       g_it_packing_list-doc_type   = 'PDF'.

       g_it_packing_list-body_num   = g_wa_objpack-body_num.

       g_it_packing_list-obj_name   =  'Attach'..

       g_it_packing_list-obj_descr  = 'LIABILITY Report'.

       g_it_packing_list-doc_size   = g_it_packing_list-body_num * 255.

       APPEND  g_it_packing_list.

     ENDLOOP.

   ENDIF.

*---Send attachment

   CALL FUNCTION 'SO_DOCUMENT_SEND_API1'

     EXPORTING

       DOCUMENT_DATA                   = g_wa_document_data

      PUT_IN_OUTBOX                    = 'X'

      SENDER_ADDRESS_TYPE              = 'B'

      COMMIT_WORK                      = 'X'

     TABLES

       PACKING_LIST                     = g_it_packing_list

      CONTENTS_BIN                     = g_it_objbin

      CONTENTS_TXT                     = g_it_objtxt

       RECEIVERS                        = t_reclist

    EXCEPTIONS

      TOO_MANY_RECEIVERS               = 1

      DOCUMENT_NOT_SENT                = 2

      DOCUMENT_TYPE_NOT_EXIST          = 3

      OPERATION_NO_AUTHORIZATION       = 4

      PARAMETER_ERROR                  = 5

      X_ERROR                          = 6

      ENQUEUE_ERROR                    = 7

      OTHERS                           = 8

             .

   IF SY-SUBRC <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

   ENDIF.

Any kind of help would be appreciated and rewarded.
Thanks in advance.

1 ACCEPTED SOLUTION

jeroen_verbrugge2
Active Participant
0 Kudos

Hi,

When using spool to PDF, program RSTXPDFT3 is useful to test if system generates correct PDF.

In this case, this would be ok so you know the problem arises when mailing it.

Also, when there is a request for mailing, please use CL_BCS class.

There are nice example programs, check BCS*

If you have had a look there, you might have found program BCS_EXAMPLE_8 which does exactly what you need: BCS: Send a Spool Request as PDF by E-Mail (Example).

Just FYI when using the 'old' SO_DOCUMENT_SEND_API1 way, a few things went wrong when filling the parameters for function.

1. Use the correct size

Use importing parameter PDF_BYTECOUNT of CONVERT_ABAPSPOOLJOB_2_PDF for this, this will give you the correct size instead of calculating by number of lines * 255. This will put garbage atfter the EOF of the PDF file.

Instead of g_it_packing_list-doc_size   = g_it_packing_list-body_num * 255.

use g_it_packing_list-doc_size   lv_length (importing parameter PDF_BYTECOUNT).

.

2. Use the correct number of lines for body

Instead of g_it_packing_list-body_num   = g_wa_objpack-body_num.

use  g_it_packing_list-body_num   = lines( g_it_objbin ).

3. You cannot assign it_pdf_output directly to g_it_obj_bin, the first table has a length of 134, second a width of 255. Conversion must be done:

CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'
   EXPORTING
     line_width_src              = 134
     line_width_dst              = 255
   TABLES
     content_in                  = it_pdf_output
     content_out                 = g_it_objbin
   EXCEPTIONS
     err_line_width_src_too_long = 1
     err_line_width_dst_too_long = 2
     err_conv_failed             = 3
     OTHERS                      = 4.

Rgds,

Jeroen

4 REPLIES 4

jeroen_verbrugge2
Active Participant
0 Kudos

Hi,

When using spool to PDF, program RSTXPDFT3 is useful to test if system generates correct PDF.

In this case, this would be ok so you know the problem arises when mailing it.

Also, when there is a request for mailing, please use CL_BCS class.

There are nice example programs, check BCS*

If you have had a look there, you might have found program BCS_EXAMPLE_8 which does exactly what you need: BCS: Send a Spool Request as PDF by E-Mail (Example).

Just FYI when using the 'old' SO_DOCUMENT_SEND_API1 way, a few things went wrong when filling the parameters for function.

1. Use the correct size

Use importing parameter PDF_BYTECOUNT of CONVERT_ABAPSPOOLJOB_2_PDF for this, this will give you the correct size instead of calculating by number of lines * 255. This will put garbage atfter the EOF of the PDF file.

Instead of g_it_packing_list-doc_size   = g_it_packing_list-body_num * 255.

use g_it_packing_list-doc_size   lv_length (importing parameter PDF_BYTECOUNT).

.

2. Use the correct number of lines for body

Instead of g_it_packing_list-body_num   = g_wa_objpack-body_num.

use  g_it_packing_list-body_num   = lines( g_it_objbin ).

3. You cannot assign it_pdf_output directly to g_it_obj_bin, the first table has a length of 134, second a width of 255. Conversion must be done:

CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'
   EXPORTING
     line_width_src              = 134
     line_width_dst              = 255
   TABLES
     content_in                  = it_pdf_output
     content_out                 = g_it_objbin
   EXCEPTIONS
     err_line_width_src_too_long = 1
     err_line_width_dst_too_long = 2
     err_conv_failed             = 3
     OTHERS                      = 4.

Rgds,

Jeroen

0 Kudos

Hi,

According to your name I think you're new to the  SAP world and also SCN.

Welcome to this community.

People are glad to help but it would help you if you changed your name to your real name.

<removed by moderator>

Kind Regards,

Jeroen

Message was edited by: Thomas Zloch

0 Kudos

Hi Jeroen,

Thanks a lot for the reply.


<removed by moderator>

Regards,
Sandeep C.

Message was edited by: Thomas Zloch

0 Kudos

Hi,

Great!

Enjoy SCN forums

Kind Regards,

Jeroen