SAP 打印自动导出PDF本地文件(自己做的例子)

smartforms导出PDF:

常用函数有;

convert_otf
convert_otfspooljob_2_pdf
convert_abapspooljob_2_pdf
CONVERT_OTF_2_PDF;
SX_OBJECT_CONVER_OTF_PDF;
HRHAP_DOC_CONVERT_SMF_TO_PDF

实例:

FORM export_pdf.
  DATA:zsmart_name TYPE rs38l_fnam.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZLE_SHIP_BMEPR'
    IMPORTING
      fm_name            = zsmart_name
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.

  DATA: w_ctrlop TYPE ssfctrlop, "Smart Forms: 控制结构 "W_CONTROL
        w_compop TYPE ssfcompop, "SAP Smart Forms: 智能写作器 (传输) 选项"W_OUTPUT.
        w_return TYPE ssfcrescl. "Smart Forms: 表格打印结束时返回值
  DATA: len TYPE i,
        w_filesize TYPE i,
        g_pdf_xstring TYPE xstring,
        l_lines TYPE TABLE OF tline WITH HEADER LINE.
  DATA  doctab TYPE TABLE OF docs WITH HEADER LINE.
  DATA  itab LIKE tline OCCURS 0 WITH HEADER LINE  DATA: file_name TYPE string,

        fullpath  TYPE string,
        path      TYPE string,
        p_sefile LIKE rlgrap-filename VALUE 'D:\'.           "'/usr/sap/tmp/'.

  w_ctrlop-getotf    = 'X'."OTF 表的返回
  w_ctrlop-no_dialog = 'X'."关闭打印对话框
*  W_CTRLOP-NO_OPEN   = 'X'."no dialog box
*  W_CTRLOP-NO_CLOSE  = 'X'."no

*&W_COMPOP-TDNOPRINT = 'X'.
  w_compop-tdnoprev  = 'X'."没有打印预览

"W_COMPOP用来设置打印参数弹出对话框中的值,下面这几个参数不必要
*  W_COMPOP-TDNEWID  = 'X'.    "New Spool
  w_compop-tdimmed  = 'X'.
  w_compop-tddelete = ''.    "Delete Spool After Print
  w_compop-tdfinal  = 'X'.
  w_compop-tdiexit  = 'X'.    "Exit after printing in print preview
  w_compop-tddest   = 'LP02'.  "设备名称
  w_compop-xdf2outdev = 'LP02'.
  w_compop-tdarmod  = '1'.

*&smartforms打印 ,将打印的值输入到W_RETURN-OTFDATA[]中
  CALL FUNCTION zsmart_name
    EXPORTING
      control_parameters = w_ctrlop
      output_options     = w_compop
*      totalpages         = totalpages
*      zbukrs             = p_bukrs
*      zcb                = cb
      gth_header         = gth_alv_sel
    IMPORTING
      job_output_info    = w_return
    TABLES
      gtd_item           = gtd_item
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

*"CONVERT_OTF函数取不到lines的返回值,所以注释掉,用CONVERT_OTF_2_PDF
*  CALL FUNCTION 'CONVERT_OTF'
*    EXPORTING
*      format                = 'PDF'
*    IMPORTING
*      bin_filesize          = len
*      bin_file              = g_pdf_xstring
*    TABLES
*      otf                   = w_return-otfdata[]
*      lines                 = itab
*    EXCEPTIONS
*      err_max_linewidth     = 1
*      err_format            = 2
*      err_conv_not_possible = 3
*      err_bad_otf           = 4
*      OTHERS                = 5.

  CALL FUNCTION 'CONVERT_OTF_2_PDF'           "转换otf数据为二进制PDF文件
*    EXPORTING
*      use_otf_mc_cmd         = 'X'
*      archive_index          = archive_index
    IMPORTING
      bin_filesize           = len
    TABLES
      otf                    = w_return-otfdata[]
      doctab_archive         = doctab
      lines                  = l_lines[]
    EXCEPTIONS
      err_conv_not_possible  = 1
      err_otf_mc_noendmarker = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

*  CONCATENATE p_sefile sy-uname '-' sy-uzeit '.pdf' INTO file_name. "addrss
*  OPEN DATASET file_name FOR OUTPUT IN BINARY MODE." MESSAGE L_MESSAGE_DB.
*  IF sy-subrc EQ 0.
*    TRANSFER g_pdf_xstring TO file_name.
*    CLOSE DATASET file_name.
*  ELSE.
*    MESSAGE 'OPEN ERROR!!' TYPE 'E'.
*    STOP.
*  ENDIF.

  CONCATENATE 'D:\' sy-uname '-' sy-uzeit ' FFF.pdf' INTO  file_name.

  "定义文件名
  "这一段不屏蔽,  在打印的时候会有显示选择路径的对话框,屏蔽则直接保存在上面的文件名
  "file_name  指定的路径
  CALL METHOD cl_gui_frontend_services=>file_save_dialog    "保存文件窗口(选择路径)
    EXPORTING
      default_extension = 'PDF'
      file_filter = '*.PDF'
      default_file_name    = file_name
    CHANGING
      filename             = file_name
      path                 = path
      fullpath             = fullpath
*        user_action          = ua
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.

    CALL FUNCTION 'GUI_DOWNLOAD'   "保存文件在本地
      EXPORTING
        bin_filesize            = len
        filename                = file_name
        filetype                = 'BIN'
      IMPORTING
        filelength              = w_filesize
      TABLES
        data_tab                = l_lines[]
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        OTHERS                  = 22.
    IF sy-subrc <> 0.
      MESSAGE  'File not downloaded'  TYPE  'I' .
    ENDIF.
  ENDIF.

ENDFORM.                    "export_pdf

新浪博客兔兄博文里提到了动态的方式获取ScriptForm数据转PDF:http://blog.sina.com.cn/s/blog_55c871720102wevc.html

(转PDF应该还可以使用此函数:SX_OBJECT_CONVERT_OTF_PDF

data: gt_otfdata  like itcoo occurs 0 with header line.

 以动态的方式获取ScriptForm数据(没有试过,可能获取不到):

field-symbols:  type any.
    data: l_tab_otfdata(30) value '(SAPLSTXC)OTF[]'.
    assign (l_tab_otfdata) to .
    if sy-subrc = 0.
      gt_otfdata[] = .
    endif.

如果上面这种方式取不到Form表单数据,则可以在CLOSEForm时得到:

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

CLOSE_FORM 的otfdata  作用:当调用PRINT_TEXT 或者 OPEN_FORM时,如果参数OPTIONS(结构类型为ITCPO)中的字段TDGETOTF值为'X' 时,将会通过参数OTFDATA以OTF的格式输出,在这种情况下,打印输出、屏幕显示、传真将不会被执行。

    itcpo-tdnoprint  = ' '.No printing from print preview
    itcpo-tdnoprev   = 'X'.No print preview
    itcpo-tdgetotf = 'X'.Return of OTF table. No printing, display, or faxing

下面开始将ScriptForm数据转换成PDF格式的数据:

data: binfilesize type i.

data: i_tline type table of tline with header line,

* convert OTF to PDF
  call function 'CONVERT_OTF'
    exporting
      format                = 'PDF'
    importing
      bin_filesize          = binfilesize
    tables
      otf                   = gt_otfdata
      lines                 = i_tline
    exceptions
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      err_bad_otf           = 4
      others                = 5.

经过上面的PDF的转换,我们就可以将以上的PDF格式的数据直接存储到文本中(此时下载或写入文件时需要以二进制的方式进行传输),这个文本就是PDF。但如果还需要将这个PDF以附件的形式发送邮件,则还需要下一步转换(其实就是将两列的i_tline内表合并成一列的内表i_objbin):

 data:i_objbin  like solisti1 occurs 0 with header line.

 call function 'QCE1_CONVERT'
      tables
        t_source_tab         = i_tline
        t_target_tab         = i_objbin
      exceptions
        convert_not_possible = 1
        others               = 2.

最后以附件的形式将i_objbin发送出去

 

1、对于scriptform或者smartform已经ABAP list都可以通过SAP标准程序RSTXPDFT4进行本地保存。

使用本方法需要获得要打印内容的假脱机请求号,可以通过事务码SP02获得。

2、通过自己书写代码实现:

①对于scriptform:首先要设置 函数 OPEN_FORM 的参数 OPTIONS ,OPTIONS中的参数 tdgetotf 设置为'X'.

通过函数    

CALL FUNCTION 'CLOSE_FORM'
                IMPORTING
                  RESULT  = RESULT
                tables
                  otfdata = otfdata.

取得otfdata数据,用于下面的函数:

CALL FUNCTION 'CONVERT_OTF'
   EXPORTING
     format                      = 'PDF'
   IMPORTING
     bin_filesize                = bin_size
    TABLES
      otf                         = otfdata
    lines                       = pdf_lines .

调用文件保存对话框:

CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
    default_extension    = 'PDF'
      file_filter          = '*.PDF'
    CHANGING
      filename             = filename
      path                 = path
      fullpath             = fullpath .

将数据保存到本地:

CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
       bin_filesize                    = bin_size
       filename                        = fullpath
       filetype                        = 'BIN'
       codepage                        = '4110'
       IGNORE_CERR                     = ABAP_TRUE
    TABLES
       data_tab                        = pdf_lines.

②对于smartform:调用的时候需要设置参数control_parameters,getotf 值设置为'X'.

在打开smartform时候,通过importing参数job_output_info获得'CONVERT_OTF'函数的tables参数OTF的值。job_output_info-otfdata[],,可以通过SSFCOMP_PDF_PREVIEW函数预览smartform的内容:

call function ‘SSFCOMP_PDF_PREVIEW’
  
exporting
    i_otf                          = t_otf[].

或者通过下面代码下载PDF文件到本地:

CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
      format = 'PDF'
  IMPORTING
      bin_filesize = bin_size
  TABLES
      otf = otfdata
      lines = pdf_lines .

调用文件保存对话框:

CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
        default_extension = 'PDF'
        file_filter = '*.PDF'
    CHANGING
        filename = filename
        path = path
        fullpath = fullpath .

将数据保存到本地:

CALL FUNCTION 'GUI_DOWNLOAD'
   EXPORTING
      bin_filesize = bin_size
      filename = fullpath
      filetype = 'BIN'
      codepage = '4110'
      IGNORE_CERR = ABAP_TRUE
   TABLES
      data_tab = pdf_lines.

 

posted @ 2019-04-23 09:51  不渡之人  阅读(6921)  评论(0编辑  收藏  举报