Skip to Content
Author's profile photo Hafizul Mollah

Sending Email with PDF Attachment

Hi All,This blog is with reference to one of the threads(question) in SCN (http://scn.sap.com/thread/3373098) wherein the requirement was to send email with PDF attachment.

As I have implemented the same for one similar requirement I replied to this question and wanted to share the steps/code if required.I have documented all the steps and code in a MS word document but as SCN does not allow to upload MS Word document ,I told to send one email to my personal email ID(mentioned in the reply) and I will send the MS word document.

 

Thereafter I kept on receiving a lot of email requests to share the word document.So thought of creating this blog with all those steps and code so that anyone needs those can get them here in SCN itself .And also they will not need to wait for my email reply instead they can get it immediately here in SCN itself.

 

I will maintain this blog link to that original thread/question.

 

Here are the steps and codes for the development of the class, method to send email with PDF attachment(content of internal table) and also one sample program to test the same.

 

Step1:  Go to tcode SE24-> Provide the class name

 

/wp-content/uploads/2014/05/class1_449494.png

 

Properties:

 

/wp-content/uploads/2014/05/2_449504.png

 

Step2 : Declare the PDF Method: SEND_PDF_FILE

 

PDF: Parameters:

 

/wp-content/uploads/2014/05/3_449505.png

 

Parameters list to copy and paste:

 

*———————————————————————————————————————————-*

IM_FILE_HEADER_TEXT        Importing    Type    STRING                 Header Text

IM_EMAIL_SUBJECT_LINE     Importing   Type    SO_OBJ_DES       Subject Line

IM_EMAIL_BODY_TEXT         Importing    Type    BCSY_TEXT          Body Text

IM_EMAIL_ADDRESS            Importing   Type    ZTP_SMTP_TAB    Email Address

IM_EMAIL_DIST_NAME          Importing   Type   SO_OBJ_NAM       Email Distribution Name

IM_EMAIL_DIST_ID                Importing   Type    SO_OBJ_ID           Email Distribution Id

IM_ATTACHMENT_NAME       Importing   Type    SOOD-OBJDES     Attachment Name

IM_FILE_DELIMETER             Importing   Type    C                          File Delimiter

IM_ITAB_1                             Importing   Type    ZTP_TEXT_TAB      Itab with data content

IM_ITAB_2                             Importing   Type    SOLI_TAB             Itab with column headers

EX_SEND_TO_ALL                Exporting   Type    OS_BOOLEAN      Send Email

IM_EMAIL_DO_NOT               Importing   Type    CHAR1                  Flag to determine whether mail needs to be sent or not

IM_LAYOUT                          Importing    Type    SYPAART             Layout mode Portrait or Landscape

EX_PDF_TABLE                    Exporting   Type    RCL_BAG_TLINE   Contains converted data in PDF format

 

*————————————————————————————————————————————————————-*

 

Custom types are highlighted , please find them below:

 

Step3 : Custom types needs to be created which is used above:

Email Table Type :

/wp-content/uploads/2014/05/4_449511.png

 

Corresponding structure:

 

/wp-content/uploads/2014/05/5_449512.png

 

Data Content table type:

 

/wp-content/uploads/2014/05/7_449516.png

 

Corresponding structure:

 

/wp-content/uploads/2014/05/8_449518.png

 

Exceptions:

 

/wp-content/uploads/2014/05/9_449519.png

 

Step4 : Paste the below code in the method body.

Sample code :

*———————————————————————————————————————————–*

Please find below the method code.

METHOD send_pdf_file.
*******************************************************************************
*    S-H-O-R-T-----D-E-S-C-R-I-P-T-I-O-N                                      *
*******************************************************************************
*Program Description : Method to send email with pdf attatchment.             *
*Developer :           Hafizul Mollah                                         *
*Brief Description : This method is used to send email with                   *
*                     a pdf attachment.  It can be used to send the email     *
*                     to multiple email addresses(To, CC,BCC) or to a         *
*                     distribution name or distribution id.                   *
*******************************************************************************


* CONSTANTS----------------------------------------------------------------*
  CONSTANTS:
    c_tab   TYPE c VALUE cl_bcs_convert=>gc_tab,
    c_crlf  TYPE c VALUE cl_bcs_convert=>gc_crlf,
    gc_tab  TYPE c VALUE cl_bcs_convert=>gc_tab,
    gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.

* TYPES----------------------------------------------------------------*
  TYPES: BEGIN OF ty_itab,
           line TYPE ztp_text_struct,
         END OF ty_itab,
         BEGIN OF ty_itab2,
           line TYPE soli,
         END OF ty_itab2,
         BEGIN OF ty_header,
           field TYPE char80,
         END OF ty_header,
         BEGIN OF ty_field,
           field TYPE char40,
         END OF ty_field,
         BEGIN OF ty_maxlen,
           maxlen TYPE i,
         END OF ty_maxlen.

* INTERNAL TABLES------------------------------------------------------*
  DATA:
    li_dlientries      TYPE STANDARD TABLE OF sodlienti1,
    li_binary_content  TYPE solix_tab,
    li_main_text       TYPE bcsy_text,
    li_recipient       TYPE ztp_smtp_tab,
    i_header           TYPE STANDARD TABLE OF ty_header,
    i_maxlen           TYPE STANDARD TABLE OF ty_maxlen,
    li_final           TYPE ztp_text_tab,
    i_field            TYPE STANDARD TABLE OF ty_field.

* STRUCTURES-----------------------------------------------------------*
  DATA:
    st_itab1       TYPE ty_itab,
    st_itab2       TYPE ty_itab2,
    st_email_addr  TYPE ztp_smtp_struct,
    st_dlientry    TYPE sodlienti1,
    st_body        TYPE soli.

* FIELD-SYMBOLS----------------------------------------------------*
  FIELD-SYMBOLS: <f_source>     TYPE any,
                 <f_wa_filetab> TYPE any,
                 <l_xline>      TYPE x,
                 <t_dyntable>   TYPE STANDARD TABLE ,  "Dynamic internal table name
                 <fs_dyntable>  TYPE any ,            "Field symbol to create work area
                 <fs_fldval>    TYPE any,               " Field symbol to assign value
                 <fs>           TYPE any.

* VARIABLES------------------------------------------------------------*
  DATA:
    wa_header        TYPE ty_header,
    send_request     TYPE REF TO cl_bcs,
    document         TYPE REF TO cl_document_bcs,
    recipient        TYPE REF TO if_recipient_bcs,
    bcs_exception    TYPE REF TO cx_bcs,
    l_size           TYPE so_obj_len,
    l_sent_to_all    TYPE os_boolean,
    l_dliname        TYPE so_obj_nam,
    l_dlidata        TYPE sodlidati1,
    l_string         TYPE string,
    l_cl_name        TYPE string,
    l_mailcc         TYPE c,
    l_mailbcc        TYPE c,
    l_recipient      TYPE ad_smtpadr,
    t_newtable       TYPE REF TO data,
    t_newline        TYPE REF TO data,
    t_fldcat         TYPE lvc_t_fcat,
    wa_it_fldcat     TYPE lvc_s_fcat,
    wa_colno(2)      TYPE n,
    wa_flname        TYPE string,
    t_attachment     TYPE solisti1 ,
    pdf_content      TYPE solix_tab,
    lp_pdf_size      TYPE so_obj_len,
    main_text        TYPE bcsy_text,
    binary_content   TYPE solix_tab,
    size             TYPE so_obj_len,
    sent_to_all      TYPE os_boolean,
    lv_string        TYPE string,
    wa_tab1          TYPE ztp_text_struct,
    wa_tab2          TYPE soli,
    pdf_xstring      TYPE xstring,
    it_pdf_table     TYPE  rcl_bag_tline,
    wa_pdfline       LIKE LINE OF it_pdf_table,
    l_column_index   TYPE  sy-index,
    l_rc             TYPE c,
    l_handle         TYPE sy-tabix,
    l_spoolid        TYPE tsp01-rqident,
    l_str(255)       TYPE c,
    params           TYPE pri_params,
    valid            TYPE char1,
    l_pdf_bytecount  TYPE i,
    wa_im_itab_1     TYPE ztp_text_struct,
    g_data           TYPE char25,
    l_output         TYPE char40,
    l_col_cnt        TYPE i,
    l_maxcnt         TYPE i,
    wa_field         TYPE ty_field,
    wa_maxlen        TYPE ty_maxlen,
    l_index          TYPE char2,
    lwa_final        TYPE ztp_text_struct,
    l_field          TYPE string,
    l_rec            TYPE i,
    wa_im_itab_2     TYPE soli,
    wa_im_itab_3     TYPE soli,
    l_field_conv     TYPE string,
    l_comp           TYPE string,
    l_pos            TYPE i VALUE 1,
    l_sp_id          TYPE tsp01_sp0r-rqid_char.



  l_field = ' '.

  CONCATENATE im_file_header_text
              gc_crlf gc_crlf
              INTO lv_string.

  SET LOCALE LANGUAGE 'E'."added
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      destination    = 'LOCL'
*LINE_SIZE = 100
      immediately    = ' '
      no_dialog      = 'X'
      layout         = im_layout
*LINE_COUNT = 500000
      line_size      = 200
    IMPORTING
      out_parameters = params
      valid          = valid.


  SPLIT im_file_header_text AT '#' INTO TABLE i_header.
  SET LOCALE LANGUAGE 'E'.

  NEW-PAGE PRINT ON PARAMETERS params NO DIALOG NO-TITLE NO-HEADING.
  LOOP AT i_header INTO wa_header.
    WRITE : wa_header-field.
  ENDLOOP.


  LOOP AT im_itab_2 INTO wa_im_itab_2.
    IF sy-tabix = 1.
      l_field = wa_im_itab_2.
    ELSE.
      CONCATENATE l_field  wa_im_itab_2 INTO l_field SEPARATED BY '|'.
    ENDIF.
  ENDLOOP.
  APPEND l_field TO li_final.

  LOOP AT  im_itab_1 INTO wa_im_itab_1.
    APPEND wa_im_itab_1 TO li_final.
  ENDLOOP.


  READ TABLE li_final INTO lwa_final INDEX 1.  "reading the columns name
  IF sy-subrc = 0.
    SPLIT lwa_final AT '|' INTO TABLE i_field.
  ENDIF.

  DESCRIBE TABLE i_field LINES l_rec.

  LOOP AT i_field INTO wa_field.
    l_index = sy-tabix.
    CONCATENATE 'COL' l_index INTO wa_flname.
    wa_it_fldcat-fieldname = wa_flname.
    wa_it_fldcat-outputlen = 40.
    wa_it_fldcat-datatype = 'CHAR'.
    APPEND wa_it_fldcat TO t_fldcat.

    CLEAR wa_it_fldcat.

  ENDLOOP.

* Create dynamic internal table and assign to FS

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = t_fldcat
    IMPORTING
      ep_table        = t_newtable.

  ASSIGN t_newtable->* TO <t_dyntable>.

  REFRESH <t_dyntable>.

* Create dynamic work area and assign to FS
  CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
  ASSIGN t_newline->* TO <fs_dyntable>.
  CLEAR <fs_dyntable>.

*****************reading the data value********************************************
  REFRESH i_field.
  LOOP AT li_final  INTO lwa_final.
    SPLIT lwa_final AT '|' INTO TABLE i_field.
    SET LOCALE LANGUAGE 'E'.
    LOOP AT i_field INTO wa_field.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
        EXPORTING
          input  = wa_field-field
        IMPORTING
          output = l_output.

      CONCATENATE l_field_conv l_output INTO l_field_conv RESPECTING BLANKS.

    ENDLOOP.
    <fs_dyntable> = l_field_conv.
    APPEND  <fs_dyntable> TO <t_dyntable>.
    CLEAR:l_field_conv.
  ENDLOOP.

  DO l_rec TIMES.
    REFRESH: i_field.
    l_col_cnt = l_col_cnt + 1.
    LOOP AT <t_dyntable> INTO <fs_dyntable>.
      IF <fs> IS ASSIGNED.
        UNASSIGN <fs>.
      ENDIF.
      DO.
        ASSIGN COMPONENT l_col_cnt OF STRUCTURE <fs_dyntable> TO <fs>.
        IF sy-subrc <> 0.
          EXIT.
        ENDIF.
        wa_field-field = <fs>.
*condense wa_field-field.
        APPEND wa_field TO i_field.
        CLEAR: wa_field.
        EXIT.
      ENDDO.

    ENDLOOP.
*****************calculating tha maximum length of domain values********************************************
    LOOP AT i_field INTO wa_field.
      l_maxcnt = strlen( wa_field-field ).
      IF wa_maxlen-maxlen <  l_maxcnt .
        wa_maxlen-maxlen = l_maxcnt.
      ENDIF.
      CLEAR:l_maxcnt.
    ENDLOOP.
    APPEND wa_maxlen TO i_maxlen .
    CLEAR: wa_maxlen,l_maxcnt.
  ENDDO.

  SKIP 2.
*****************writing the column names and data value********************************************
  LOOP AT <t_dyntable> INTO <fs_dyntable>.
    IF <fs> IS ASSIGNED.
      UNASSIGN <fs>.
    ENDIF.
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <fs_dyntable> TO <fs>.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      l_comp = <fs>.
      IF sy-index = 1.
        WRITE:/ l_comp.
      ELSE.
        l_rec = sy-index - 1.
        READ TABLE i_maxlen INTO wa_maxlen  INDEX l_rec.
        IF sy-subrc = 0.
          l_pos = l_pos + wa_maxlen-maxlen + 1.
          WRITE AT l_pos l_comp.
        ENDIF.
      ENDIF.
      CLEAR:l_rec,l_comp.
    ENDDO.
    l_pos = 1.
  ENDLOOP.

  NEW-PAGE PRINT OFF.
  l_spoolid = sy-spono.
  SET LOCALE LANGUAGE 'E'.
  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
    EXPORTING
      src_spoolid              = l_spoolid                  "'30221'
*     NO_DIALOG                = 'X'
      dst_device               = 'LOCL'
*     PDF_DESTINATION          =
*     NO_BACKGROUND            =
*     GET_SIZE_FROM_FORMAT     =
    IMPORTING
      pdf_bytecount            = l_pdf_bytecount
*     PDF_SPOOLID              =
*     LIST_PAGECOUNT           =
*     BTC_JOBNAME              =
*     BTC_JOBCOUNT             =
*     BIN_FILE                 =
    TABLES
      pdf                      = it_pdf_table
    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.


  MOVE l_spoolid TO l_sp_id.

  CALL FUNCTION 'RSPO_R_RDELETE_SPOOLREQ'
    EXPORTING
      spoolid = l_sp_id.


* Map PDF table into 'flat' table acording to Unicode flag
  ex_pdf_table = it_pdf_table .

  IF im_email_do_not IS INITIAL.
    LOOP AT it_pdf_table INTO wa_pdfline.
      ASSIGN wa_pdfline TO <l_xline> CASTING.
      CONCATENATE pdf_xstring <l_xline> INTO pdf_xstring IN BYTE MODE.
    ENDLOOP.

*   get PDF xstring and convert it to BCS format
    lp_pdf_size = xstrlen( pdf_xstring ).
    pdf_content = cl_document_bcs=>xstring_to_solix(
        ip_xstring = pdf_xstring ).
    TRY.

*-------- create persistent send request ------------------------
        send_request = cl_bcs=>create_persistent( ).

*-------- create and set document with attachment ---------------
*create document object from internal table with text
        LOOP AT im_email_body_text INTO st_body.
          APPEND st_body TO li_main_text.
        ENDLOOP.
        document = cl_document_bcs=>create_document(
          i_type    = 'RAW'
          i_text    = li_main_text
          i_subject = im_email_subject_line ).

*add the spread sheet as attachment to document object
        document->add_attachment(
         i_attachment_type    = 'PDF'                       "#EC NOTEXT
         i_attachment_subject = im_attachment_name          "#EC NOTEXT
         i_attachment_size    = lp_pdf_size                    "Size
         i_att_content_hex    = pdf_content ).
*add document object to send request
        send_request->set_document( document ).

*get email addresses from table
        LOOP AT im_email_address INTO st_email_addr.
          APPEND st_email_addr TO li_recipient.
        ENDLOOP.

*Get email addresses from Distribution list
        CALL FUNCTION 'SO_DLI_READ_API1'
          EXPORTING
            shared_dli                 = 'X'
            dli_id                     = space
            dli_name                   = im_email_dist_name
          IMPORTING
            dli_data                   = l_dlidata
          TABLES
            dli_entries                = li_dlientries
          EXCEPTIONS
            dli_not_exist              = 9001
            operation_no_authorization = 9002
            parameter_error            = 9003
            x_error                    = 9004
            OTHERS                     = 01.

        CLEAR st_email_addr.
        LOOP AT li_dlientries INTO st_dlientry.
          st_email_addr-smtpadr = st_dlientry-member_adr.
          st_email_addr-mail_to = 'X'.
          APPEND st_email_addr TO li_recipient.
        ENDLOOP.

*Get email addresses from Distribution Id
        CALL FUNCTION 'SO_DLI_READ_API1'
          EXPORTING
            shared_dli                 = 'X'
            dli_id                     = im_email_dist_id
            dli_name                   = space
          IMPORTING
            dli_data                   = l_dlidata
          TABLES
            dli_entries                = li_dlientries
          EXCEPTIONS
            dli_not_exist              = 9001
            operation_no_authorization = 9002
            parameter_error            = 9003
            x_error                    = 9004
            OTHERS                     = 01.

        LOOP AT li_dlientries INTO st_dlientry.
          st_email_addr-smtpadr = st_dlientry-member_adr.
          st_email_addr-mail_to = 'X'.
          APPEND st_email_addr TO li_recipient.
        ENDLOOP.


*--------- add recipient (e-mail address)-----------------------
        LOOP AT li_recipient INTO st_email_addr.
          l_recipient = st_email_addr-smtpadr.
          recipient = cl_cam_address_bcs=>create_internet_address( l_recipient ).
          IF st_email_addr-mail_to = 'X'.
            CLEAR l_mailcc.
            CLEAR l_mailbcc.
          ELSE.
            IF st_email_addr-mail_cc = 'X'.
              l_mailcc = 'X'.
              CLEAR l_mailbcc.
            ELSE.
              IF st_email_addr-mail_bcc = 'X'.
                l_mailbcc = 'X'.
                CLEAR l_mailcc.
              ENDIF.
            ENDIF.
          ENDIF.
          TRY.
            CALL METHOD send_request->add_recipient
              EXPORTING
                i_recipient  = recipient
                i_express    = 'X'
                i_copy       = l_mailcc
                i_blind_copy = l_mailbcc.
          ENDTRY .

        ENDLOOP.

*add recipient object to send request
*send_request->add_recipient( recipient ).

*---------- send document ---------------------------------------
        send_request->send_request->set_link_to_outbox( 'X' ).

        l_sent_to_all = send_request->send( i_with_error_screen = 'X' ).

        COMMIT WORK.

        IF l_sent_to_all IS INITIAL.
          ex_send_to_all = '1'.
        ELSE.
          ex_send_to_all = '0'.
        ENDIF.

        SUBMIT rsconn01 WITH mode = 'INT'
                    WITH output = 'X'
                    AND RETURN.

*   ------------ exception handling ----------------------------------
      CATCH cx_bcs INTO bcs_exception.
*      message i865(so) with bcs_exception->error_type.
    ENDTRY.

  ENDIF.


ENDMETHOD.

*———————————————————————————————————————————-*

 

 

Step5:Activate the method and class and write the below small executable test program to test it.Go to SE38 create the below program to test:

 

/wp-content/uploads/2014/05/10_449520.png

 

Sample Code :

*——————————————————————————————————————————-*

Please find below the sample code for the test program.

*&---------------------------------------------------------------------*
*& Report  ZTP_SEND_EMAIL
*&
*&-----------------------------------------------------------------------------*
*&Short Description : This is a Test Program which calls the method            *
*&                    'SEND_PDF_FILE' of the Global Class 'ZCL_SEND_ATTACHMENT'*
*&                    and will send the email with a PDF file as an attachment.*
*&-----------------------------------------------------------------------------*

REPORT  ztp_send_email.

PARAMETERS : p_email TYPE char100 DEFAULT 'hafizul.mollah@test.com'."Email ID in the format test@xxxx.com

TYPES: BEGIN OF ty_outfield,
            outdata TYPE soli,
       END OF ty_outfield,
       BEGIN OF ty_data,
           outdata TYPE ztp_text_struct,
       END OF ty_data.

DATA:ls_body      TYPE soli,
     lt_body      TYPE bcsy_text,
     lt_headings  TYPE soli_tab,
     ls_headings  TYPE ty_outfield,
     ls_data      TYPE ty_data,
     lt_data      TYPE ztp_text_tab,
     ls_email     TYPE ztp_smtp_struct,
     lt_email     TYPE ztp_smtp_tab,
     l_dist_list  TYPE so_obj_nam,
     l_dist_list1 TYPE so_obj_id,
     pdf_file     TYPE rcl_bag_tline,
     li_email     TYPE ztp_smtp_tab,
     lst_email    TYPE ztp_smtp_struct,
     l_return     TYPE os_boolean,
     li_body       TYPE bcsy_text,
     lst_body      TYPE soli.


*----Populate Column names in the PDF file--------*

ls_headings-outdata = 'PERNR'.  APPEND ls_headings TO lt_headings.
ls_headings-outdata = 'LNAME'.  APPEND ls_headings TO lt_headings.
ls_headings-outdata = 'FNAME'.  APPEND ls_headings TO lt_headings.

*----Populate Actual data names in the PDF file(Use Internal Table content here)--------*
ls_data-outdata = '10000001|Romo|Tony'.  APPEND ls_data TO lt_data.
ls_data-outdata = '10000002|Favre|Brett'.  APPEND ls_data TO lt_data.

*----Populate Email IDs here(To ,CC , Bcc etc.)--------*

ls_email-smtpadr = p_email.
ls_email-mail_to = 'X'.
APPEND ls_email TO li_email.
CLEAR : ls_email.

*ls_email-smtpadr = 'souvik.bhattacharya@test.com'.
*ls_email-mail_to = ' '.
*ls_email-mail_cc = 'X'.
*APPEND ls_email TO li_email.    "


*----Populate Body of the email here--------*

lst_body = 'Hello User,'.
APPEND lst_body TO li_body.  "Body of the email
CLEAR : lst_body.
lst_body = 'Please find attached the PDF file.'.
APPEND lst_body TO li_body.  "Body of the email

CALL METHOD zcl_send_attachment=>send_pdf_file
  EXPORTING
    im_file_header_text   = 'Employee Name Details'
    im_email_subject_line = 'Employee Details in attached PDF file '
    im_email_body_text    = li_body
    im_email_address      = li_email
    im_email_dist_name    = 'DISTRIBU_LIS'
    im_email_dist_id      = 'DISTRIBU_LIS'
    im_attachment_name    = 'Employee_data.pdf'
    im_file_delimeter     = '|'
    im_itab_1             = lt_data
    im_itab_2             = lt_headings
    im_email_do_not       = space
  IMPORTING
    ex_send_to_all        = l_return
  EXCEPTIONS
    error_transferring    = 1.

IF l_return <> 0.

ELSE.
  SET LOCALE LANGUAGE 'E'.
  COMMIT WORK.
  SUBMIT rsconn01 WITH mode = 'INT'
              WITH output = 'X'
              AND RETURN.

  WRITE : 'Email successfully Sent'.
ENDIF.

*——————————————————————————————————————————*

 

Step6: Activate the program and execute it.Please change the email ID to yours one to receive the email to Lotus Notes / Outlook /Other email configured.

 

Execute the program and test whether the email is sent successfully or not (Check in SOST tcode):

 

/wp-content/uploads/2014/05/12_449521.png

 

Open the PDF file attached in the email and make sure the content is correct:

 

/wp-content/uploads/2014/05/13_449537.png

 

Please let me know if anyone face any issue while developing or testing it.

 

Thanks & Regrads,

Hafizul Mollah,

SAP ABAP Consultant @Capgemini India Pvt. Ltd.

Assigned Tags

      18 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      This was very useful for me. Great Content

      Author's profile photo Hafizul Mollah
      Hafizul Mollah
      Blog Post Author

      Thanks Radhika....glad to hear that it helped you.....

      Author's profile photo Former Member
      Former Member

      Hi Hafizul,

       

      This cool i want to use this too! but for my situation i am using ALV report so i already have an internal table for the details and i have to convert it to pdf frst then send it.

       

      1. how can i use this using my table?

      the code in the details is like this:

       

      ls_data-outdata = '10000001|Romo|Tony'

       

      how could i use my internal table with out looping and using concatenate?

       

      2. i also have a traffic light status icon in my internal table, if ever the alv worked will the icon display correctly?

       

       

      i'm just a noob here and also in SAP ABAP, if my question was already answered, could you kindly give me a link for this?

       

      thank you very much!

      Author's profile photo Hafizul Mollah
      Hafizul Mollah
      Blog Post Author

      Hello Edsel,

      You might have noticed that in the method code , it is splitting the character content string by the separator give as input , so you have to prepare this special content separated by the separator and then pass that internal table as the input while calling the method and to prepare this you will have to loop in the itab and then use CONCATENATE statement.

       

      Example : Lets say your itab(i_final , workarea : wa_final) has 3fields (matnr,maktx,werks) and separator is '|'. Content string table is 'i_content' , workarea : wa_content

       

      Loop at i_final into wa_final.

      CONCATENATE wa_final-matnr wa_final-maktx wa_final-werks into lv_string separated by '|'.

      wa_content-content = lv_string.

      append wa_content to i_content.

      Endloop.

       

      Now the content has been prepared and put into 'i_content' and you need to use this as content table while calling the method in your program.

       

      Regarding traffic signal icon , as the method is treating all content as character , the special character icon should also be printed but it may not reflect the Green/Red color as it is writing the characters in spool using 'write' statement and pulling it from the spool.Please follow the same procedure described above and let me know if you face any issue.

       

      Thanks & Regards,

      Hafizul

      Author's profile photo Jānis B
      Jānis B

      I mean no offense, but if the code in Source code _method.txt.zip really is the kind of stuff Capgemini India ships to customers... then I'm kind of lost for words

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Global variables prefixed with "L" in program.txt.

      Field-symbols that have <f_ something. Hey! If it's in a < > already, we know it's a field symbol, what does the "F" add?

      Itab1, itab2... very meaningful.

      No modularisation... well, that can be forgiven, it makes it easier to share the code

      Commented out exception handling...

       

      Did I miss anything?

      Author's profile photo Jānis B
      Jānis B

      In addition:

       

      First executable statement of method: l_field = ' '.

       

      Commented parameters left in FM call.

       

      Conditionless SET LOCALE LANGUAGE 'E' twice within 16 code lines.

       

      LOOP AT  im_itab_1 INTO wa_im_itab_1.

          APPEND wa_im_itab_1 TO li_final.

      ENDLOOP.

       

      Yet another SET LOCALE LANGUAGE 'E', within a loop.

       

      Yet another SET LOCALE LANGUAGE 'E'. - I mean, why on earth not..?

      Author's profile photo Matthew Billingham
      Matthew Billingham

      "SET LOCALE LANGUAGE E'

       

      It's the only way to be sure.

      Author's profile photo Former Member
      Former Member

      Pretty soon when someone mentions JB in coffee corner, people will confuse between you and Julius.

      Author's profile photo Hafizul Mollah
      Hafizul Mollah
      Blog Post Author

      Hello all,

      All these codes are from test programs and it was developed as a PoC , so both in the program and class methods no specific naming convention has been followed...I am aware all these but in the customer deliverable program the fixed naming convention is being followed stictly.

       

      My request to all of you , please ignore the naming convention issues and also use this as basic template and enhance this as per your specific requirement and optimize it before deliver the same to the client.This is just a test program which I created to debug and test the whole thing , please do not use this blindly and it needs to be modified as per the concerned client's requirement and company's ABAP development naming convention.

       

       

      Thanks & Regards,

      Hafizul

      Author's profile photo Former Member
      Former Member

      Hi, first be thanking for your work.

      I have a prolem, I can´t open the pdf attachtment. it sais that the format is invalid.

      I have tried to modify the language to S = Spanish but I don´t think that is the problem.

       

      thank´s so much

      Author's profile photo Hafizul Mollah
      Hafizul Mollah
      Blog Post Author

      Hello Javier,

      I have not faced this issue even during R&D phase-----hope u have copied the same code and tried---and Adobe is also installed in ur system----as u know we are writing to the spool and then reading from spool, so please check if the spool settings are correctly done in your system----also can u please debug the whole thing----starting from program, internal table data, separator then in the method where reading from spool etc. and see if where it is actually failing/giving issues/exception.

      Author's profile photo Fernando Iruela
      Fernando Iruela

      Hi,



         Hafizul, thanks so much for explanation.



         Javier, i face through the same problem as you, and debugging found that it is the parameter of impression "destination = 'LOCL'", that does not exist, but leaving it blank he takes the first valid:

       

      CALL FUNCTION 'GET_PRINT_PARAMETERS'

           EXPORTING

             immediately    = ' '

             no_dialog      = 'X'

             layout         = im_layout

           IMPORTING

             out_parameters = params

             valid          = valid.



      Thanks!

      Author's profile photo Florin Wach
      Florin Wach

      You could also use a generic smartForm to hold your print-out (WRITE's) to retrieve your PDF.

      With that, you don't need to go the long way round the spooler. It's a bit faster in processing, as no file is being generated intermediatly.

      Author's profile photo Former Member
      Former Member

      Hello Hafizul,

      I don't see the attachment. Could you please post it again? on a side note, there will always be naysayers for anything you do. This is a step in right direction.

       

      Thanks,

      John

      Author's profile photo Hafizul Mollah
      Hafizul Mollah
      Blog Post Author

      Hi John,

      I think those attachments have been deleted by admin during the migration from SCN to "sap.com". The sample codes have now been pasted directly in the blog.

       

      Thanks & Regards,

      Hafizul

      Author's profile photo Avish Mittal
      Avish Mittal

      Hi Hafizul,

      Thanks for this wonderful blog. It is very helpful.

      I am also working on same kind of requirement. I am calling a smartform and converting output into PDF (using FM "CONVERT_OTF") and then sending as attachment in email. Now the issue is when I am running the report in foreground everything is working fine, but when running in background PDF in email is a corrupt file. Then I have debug the code and found when I am running the code in background FM "CONVERT_OTF" is giving SY-SUBRC = 4. I have also tried FM "CONVERT_OTF_2_PDF", but no luck. Please help.

      Thanks in advance.

      Regards,

      Avish

       

      Author's profile photo Harris Veziris
      Harris Veziris

      Hi Hafizul,

       

      I was struggling to convert text lines with binary content to raw format of SOLIX type, but I used your code and worked fine.

          LOOP AT it_pdf_table INTO wa_pdfline.
            ASSIGN wa_pdfline TO <l_xline> CASTING.
            CONCATENATE pdf_xstring <l_xline> INTO pdf_xstring IN BYTE MODE.
          ENDLOOP.
      
      *   get PDF xstring and convert it to BCS format
          lp_pdf_size = xstrlen( pdf_xstring ).
          pdf_content = cl_document_bcs=>xstring_to_solix(
              ip_xstring = pdf_xstring ).

      Thanks a lot!

      Harris