1.smartforms--->生成PDF如下:
 "smartforms 名称
     LSMNAME = 'ZFIS0010'.
     CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'   " 内定函数
        EXPORTING
           FORMNAME           = LSMNAME "'ZFIS0010S'
        IMPORTING
           FM_NAME            = L_NAME
        EXCEPTIONS
           NO_FORM            = 1
           NO_FUNCTION_MODULE = 2
        OTHERS             = 3.
   IF SY-SUBRC <> 0.
    " Call Smart Forms ERROR!
    MESSAGE S001 DISPLAY LIKE 'E' WITH TEXT-E02.  " 提示表单错误
  ELSE.
*--->---smrt forms參數設定---
    LS_CONTROL-NO_DIALOG = 'X'.   " No dialog window
    LS_CONTROL-GETOTF    = 'X'.   " Get OTF data
    LS_CONTROL-LANGU     = 'M'.
    LS_CONTROL-NO_OPEN   = 'X'.
    LS_CONTROL-PREVIEW   = 'X'.
    LS_COMPOSER-TDDEST   = 'FPW1'.
    LS_COMPOSER-TDIMMED  = 'X'.   " print immediately
*--->---設定印表機--- " OTF: 直接下載檔案 CALL FUNCTION 'SSF_OPEN' EXPORTING CONTROL_PARAMETERS = LS_CONTROL OUTPUT_OPTIONS = LS_COMPOSER USER_SETTINGS = '' EXCEPTIONS FORMATTING_ERROR = 1 INTERNAL_ERROR = 2 SEND_ERROR = 3 USER_CANCELED = 4 OTHERS = 5.
IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.
*--->---get OTF file--- CLEAR: CH_OTF.
" 动态调用新生成的函数 CALL FUNCTION L_NAME EXPORTING USER_SETTINGS = '' CONTROL_PARAMETERS = LS_CONTROL OUTPUT_OPTIONS = LS_COMPOSER ZUGDT = ANLA-ZUGDT G_TOTALLINES = G_TOTALLINES IMPORTING JOB_OUTPUT_INFO = CH_OTF TABLES GT_LYS = GT_LYS EXCEPTIONS FORMATTING_ERROR = 1 INTERNAL_ERROR = 2 SEND_ERROR = 3 USER_CANCELED = 4 OTHERS = 5.
IF SY-SUBRC <> 0. " GET OTF data ERROR! MESSAGE S001 DISPLAY LIKE 'E' WITH TEXT-E03. " 提示表单错误 ENDIF.
" 调用完整代码 PERFORM 并取消部分的注释,
PERFORM SEND_PDF TABLES CH_OTF-OTFDATA. ENDIF.

2.完整代码如下(不包含smartforms):

REPORT  ZMM_PDF_ZPI_MAIL NO STANDARD PAGE HEADING
          MESSAGE-ID 00 LINE-SIZE 177 LINE-COUNT 200.
DATA:g_val        TYPE c,
     w_pripar     TYPE pri_params,
     w_arcpar     TYPE arc_params,
     input_length TYPE i,
     content_x    TYPE xstring,
     content_s    TYPE string,
     spool_no     LIKE tsp01-rqident,
     it_pdf       TYPE TABLE OF tline WITH HEADER LINE,
     wa_pdf       LIKE LINE OF it_pdf,
     file_tab     TYPE STANDARD TABLE OF solisti1,
     bin_file     TYPE xstring,
     bytecount    TYPE i,
     zip          TYPE xstring,
     path_table   TYPE TABLE OF char1024.
DATA: BEGIN OF it_t001 OCCURS 0,
        bukrs     TYPE t001-bukrs,
        butxt     TYPE t001-butxt,
      END OF it_t001.
DATA:LT_TLINE TYPE STANDARD TABLE OF TLINE WITH HEADER LINE. DATA: G_MAIL_ADDRESS TYPE AD_SMTPADR. DATA:g_zipper TYPE REF TO cl_abap_zip. DATA:file_name TYPE string VALUE 'Report_output.pdf'. DATA:file_name2 TYPE string VALUE 'Report_output2.pdf'. DATA:file_path TYPE string VALUE 'D:\temp\pdf_report.ZIP'.
SELECTION-SCREEN BEGIN OF BLOCK A WITH FRAME TITLE TEXT-BO1.
SELECT-OPTIONS: s_addr FOR g_mail_address NO INTERVALS DEFAULT'qq.com'. SELECTION-SCREEN END OF BLOCK A.
START-OF-SELECTION. PERFORM get_data. PERFORM print_data CHANGING spool_no. PERFORM convert_spool_2_pdf. PERFORM convert_binary_2_xstring.
END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form get_data *&---------------------------------------------------------------------* FORM get_data .
SELECT * FROM t001 INTO CORRESPONDING FIELDS OF TABLE it_t001 UP TO 10 ROWS.
ENDFORM. " get_data *&---------------------------------------------------------------------* *& Form print_data *&---------------------------------------------------------------------* FORM print_data CHANGING spool_no. CALL FUNCTION 'GET_PRINT_PARAMETERS' EXPORTING in_archive_parameters = w_arcpar in_parameters = w_pripar layout = 'X_65_132' line_count = 65 line_size = 255 no_dialog = 'X' IMPORTING out_archive_parameters = w_arcpar out_parameters = w_pripar valid = g_val. IF g_val NE space AND sy-subrc = 0. w_pripar-prrel = space. w_pripar-primm = space. NEW-PAGE PRINT ON NEW-SECTION PARAMETERS w_pripar ARCHIVE PARAMETERS w_arcpar NO DIALOG. "Output which is gone to spool LOOP AT it_t001. WRITE:/ it_t001. ENDLOOP. NEW-PAGE PRINT OFF. CALL FUNCTION 'ABAP4_COMMIT_WORK'. ENDIF. IF NOT sy-spono IS INITIAL. spool_no = sy-spono. ENDIF. ENDFORM. " print_data *&---------------------------------------------------------------------* *& Form convert_spool_2_pdf *&---------------------------------------------------------------------* FORM convert_spool_2_pdf . CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF' EXPORTING src_spoolid = spool_no no_dialog = space IMPORTING pdf_bytecount = bytecount TABLES pdf = it_pdf.
*"---> pdf 内表格式转换固定格式 CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE' EXPORTING line_width_src = 134 line_width_dst = 255 TABLES content_in = it_pdf content_out = file_tab.
ENDFORM. " convert_spool_2_pdf
*&---------------------------------------------------------------------* *& Form convert_binary_2_xstring *&---------------------------------------------------------------------* FORM convert_binary_2_xstring . "create xstring from table CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = bytecount IMPORTING buffer = content_x TABLES binary_tab = file_tab.
PERFORM SEND_PDF ."TABLES CH_OTF-OTFDATA. "CH_OTF-OTFDATA 是 JOB_OUTPUT_INFO 返回的参数 * PERFORM SEND_PDF TABLES CH_OTF-OTFDATA. ENDFORM. " convert_binary_2_xstring
FORM SEND_PDF. "TABLES IT_OTF STRUCTURE ITCOO * smartforms 传过来值请启用一下注释代码 *FORM SEND_PDF TABLES IT_OTF STRUCTURE ITCOO .
DATA: "LT_PDF TYPE STANDARD TABLE OF TLINE, L_PDF_FSIZE TYPE I, " PDF XSTRING SIZE MSG1(50) TYPE C.
* ---CONVERT OTF TO PDF--- * CALL FUNCTION 'CONVERT_OTF' * EXPORTING * FORMAT = 'PDF' * MAX_LINEWIDTH = 132 * IMPORTING * BIN_FILESIZE = L_PDF_FSIZE * TABLES * OTF = IT_OTF * LINES = IT_PDF * EXCEPTIONS * ERR_MAX_LINEWIDTH = 1 * ERR_FORMAT = 2 * ERR_CONV_NOT_POSSIBLE = 3 * ERR_BAD_OTF = 4 * OTHERS = 5. * * IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE * SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. * ENDIF.
*-----------------pdf mail 压缩文件 --------------------- DATA:file_name TYPE string VALUE 'output.pdf', file_name2 TYPE string VALUE 'output2.pdf', zip TYPE xstring, l_obj_len2 TYPE i, file_path TYPE string VALUE 'D:\temp\pdf_out.ZIP', g_zipper TYPE REF TO cl_abap_zip.
"---> 转换 xstring CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = bytecount IMPORTING buffer = content_x TABLES binary_tab = file_tab.
DATA: l_send_request TYPE REF TO cl_bcs, l_zip TYPE REF TO cl_abap_zip, l_sender TYPE REF TO cl_sapuser_bcs, l_recipient TYPE REF TO cl_cam_address_bcs, l_body_text TYPE bcsy_text, l_obj_len TYPE so_obj_len, l_subject TYPE so_obj_des, l_exception TYPE REF TO cx_bcs, l_message TYPE string, l_content TYPE solix_tab, l_html_xstr TYPE xstring, lv_xstring TYPE xstring, l_zip_xstr TYPE xstring, l_len TYPE i, l_document TYPE REF TO cl_document_bcs.
TRY.
l_send_request
= cl_bcs=>create_persistent( ). l_subject = 'cl_abap_zip & cl_bcs example'. APPEND: '<HTML><BODY>' TO l_body_text, 'Hi,' TO l_body_text, '<p>Pls refer to the attached. Thanks.' TO l_body_text, '</BODY><HTML>' TO l_body_text. l_document = cl_document_bcs=>create_document( i_type = 'HTM' i_text = l_body_text i_subject = l_subject ).
CREATE OBJECT l_zip. " 第一个pdf文件 CALL METHOD l_zip->add EXPORTING name = 'cl_bcs example.PDF' content = content_x.
" 第二个pdf文件 CALL METHOD l_zip->add EXPORTING name = 'cl_bcs example2.PDF' content = content_x.
*创建一个压缩文件 CALL METHOD l_zip->save RECEIVING zip = l_zip_xstr.
CALL METHOD cl_bcs_convert=>xstring_to_solix EXPORTING iv_xstring = l_zip_xstr RECEIVING et_solix = l_content.
 *获取文件大小, ( XSTRLEN 计算文件大小 ) l_obj_len = XSTRLEN( l_zip_xstr ) .
CALL METHOD l_document->add_attachment EXPORTING i_attachment_size = l_obj_len i_attachment_type = 'ZIP' i_attachment_subject = l_subject i_att_content_hex = l_content. l_send_request->set_document( l_document ). l_sender = cl_sapuser_bcs=>create( sy-uname ). LOOP AT s_addr. l_recipient = cl_cam_address_bcs=>create_internet_address( s_addr-low ). CALL METHOD l_send_request->add_recipient EXPORTING i_recipient = l_recipient. ENDLOOP. CALL METHOD l_send_request->set_send_immediately( 'X' ). CALL METHOD l_send_request->send( EXPORTING i_with_error_screen = 'X' ). COMMIT WORK.
 MESSAGE S001 WITH '邮件发送成功!' DISPLAY LIKE 'S'. CATCH cx_bcs INTO l_exception. l_message = l_exception->get_text( ). MESSAGE l_message TYPE 'E'. ENDTRY.
 * IF SY-SUBRC = 0. * MESSAGE S001 WITH '邮件发送成功!' DISPLAY LIKE 'S'. * ENDIF. *----------------- end pdf mail 压缩文件 --------------------- 
*-----------------pdf 压缩文件 载到本端 ------------------- *---->下载压缩 pdf文件 "创建 g_zipper 压缩文件 CREATE OBJECT g_zipper.
"第一个 pdf(xstring类型 )add file to zip CALL METHOD g_zipper->add EXPORTING name = file_name content = content_x.
 * "第二个 pdf(xstring类型 )add file to zip CALL METHOD g_zipper->add EXPORTING name = file_name2 content = content_x.
 *创建一个压缩文件 CALL METHOD g_zipper->save RECEIVING zip = zip.
l_obj_len2
 = XSTRLEN( zip ) .
 *字码转换 (xstring --> 二进制) CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = zip IMPORTING output_length = l_obj_len2 TABLES binary_tab = file_tab.
 *下载压缩文件到本端 CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING bin_filesize = l_obj_len2 filename = file_path filetype = 'BIN' TABLES data_tab = file_tab. *----------------- end pdf 压缩文件 载到本端 ------------------- ENDFORM. 

效果如下:

打开效果内容如下:

 

原文参考:https://wiki.scn.sap.com/wiki/display/ABAP/CL_ABAP_ZIP+usage+-+Zipping+ABAP+report+output

posted on 2020-01-03 20:17  淡淡-祥  阅读(521)  评论(0编辑  收藏  举报