SAP ABAP 直接把内表转换成PDF格式(smartform的打印函数输出OTF格式数据)

直接上代码:

java 复制代码
REPORT zcycle055.

DATA: lt_tab TYPE TABLE OF zpps001.
DATA: ls_tab TYPE zpps001.

ls_tab-werks = '1001'.
ls_tab-gamng = '150.00'.
ls_tab-gstrp = '20201202'.
ls_tab-aufnr = '000010000246'.
ls_tab-auart = '标准生产'.
ls_tab-gltrp = '20201205'.
ls_tab-matnr = '04-r02a000-00005'.
ls_tab-maktx = 'Cycle is handsome!'.
ls_tab-ernam = 'lf002'.
ls_tab-erdat = '20201202'.
ls_tab-zcwb = 'MO20120003'.
ls_tab-zdddw = '盒'.
ls_tab-ftrmi = '20201210'.
ls_tab-zzczgsmc = 'Cycle有限责任公司'.

APPEND ls_tab TO lt_tab.
CLEAR ls_tab.

PERFORM export_pdf.

FORM export_pdf.
  DATA:zsmart_name TYPE rs38l_fnam.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZPP001'
    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   = 'lp01'.  "设备名称
  w_compop-xdf2outdev = 'lp01'.
  w_compop-tdarmod  = '1'.
  w_compop-tdnoprev = 'X'.        "隐藏报表预览功能,默认为空

*&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
      t_item             = lt_tab  "传所选行项目明细内表 传到sf
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

"CONVERT_OTF函数取不到lines的返回值
"但是可以直接获取到PDF数据的XSTRING数据,可以用于后台直接OPEN DATASET 生成文件
  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.

  "后台OPEN DATASET 直接生成PDF文件
  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.

  "CONVERT_OTF_2_PDF 可以生成PDF格式的内表
  CALL FUNCTION 'CONVERT_OTF_2_PDF'   "转换otf数据为二进制PDF文件,可以用于前台直接下载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.

  "前台下载PDF文件
  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

效果:

直接把文件下载到桌面

打开文件

更多细节可以参考大佬的文章:https://www.cnblogs.com/buduzhiren/p/10754617.html

相关推荐
岑梓铭2 分钟前
(CentOs系统虚拟机)Standalone模式下安装部署“基于Python编写”的Spark框架
linux·python·spark·centos
努力学习的小廉3 分钟前
深入了解Linux —— make和makefile自动化构建工具
linux·服务器·自动化
MZWeiei6 分钟前
Zookeeper基本命令解析
大数据·linux·运维·服务器·zookeeper
7yewh22 分钟前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
Arenaschi26 分钟前
在Tomcat中部署应用时,如何通过域名访问而不加端口号
运维·服务器
小张认为的测试26 分钟前
Linux性能监控命令_nmon 安装与使用以及生成分析Excel图表
linux·服务器·测试工具·自动化·php·excel·压力测试
waicsdn_haha33 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
打鱼又晒网34 分钟前
linux网络套接字 | 深度解析守护进程 | 实现tcp服务守护进程化
linux·网络协议·计算机网络·tcp
良许Linux1 小时前
0.96寸OLED显示屏详解
linux·服务器·后端·互联网
蜜獾云1 小时前
docker 安装雷池WAF防火墙 守护Web服务器
linux·运维·服务器·网络·网络安全·docker·容器