【ABAP随笔】smartforms 打印及PDF转化

2022-06-23 15:14:46 浏览数 (1)

—— BEGIN ——

report Z_smartforms

TABLES:mara.

TYPES:BEGIN OF ty_data,

checkbox TYPE c,

matnr TYPE mara-matnr,

mtart TYPE mara-mtart,

matkl TYPE mara-matkl,

maktx TYPE makt-maktx,

END OF ty_data.

DATA gt_data TYPE TABLE OF ty_data.

*INCLUDE zalv_oo_cls.

INCLUDE z_smartforms_cls.

DATA ok_code TYPE sy-ucomm.

DATA: gt_fieldcat TYPE lvc_t_fcat. "字段操作表

DATA: gs_layout TYPE lvc_s_layo.

SELECT-OPTIONS:

matnr FOR mara-matnr,

mtart FOR mara-mtart,

matkl FOR mara-matkl.

START-OF-SELECTION.

SELECT

a~matnr ,

a~mtart ,

a~matkl ,

b~maktx

INTO CORRESPONDING FIELDS OF TABLE @gt_data

FROM mara AS a

INNER JOIN makt AS b ON a~matnr = b~matnr

UP TO 30 ROWS.

END-OF-SELECTION.

CHECK gt_data[] IS NOT INITIAL.

CREATE OBJECT gr_alv.

PERFORM frm_set_fieldcat.

CALL METHOD gr_alv->standard_oo_alv

EXPORTING

iv_screen = '9000'

iv_default_ex = 'X'

is_layout = gs_layout

iv_self_ucomm = 'X'

iv_container_name = 'TC'

CHANGING

* CR_DOCK = GR_DOCK

* CR_GRID = GR_GRID

ct_fieldcat = gt_fieldcat

ct_data = gt_data

EXCEPTIONS

create_alv_failed = 1.

CALL SCREEN 9000.

FORM frm_set_fieldcat.

gs_layout-sel_mode = 'D' . "模式

gs_layout-zebra = 'X'. "ALV表格按斑马线条纹显示

gs_layout-cwidth_opt = 'X'. "将ALV字段宽度设置为最优化

gs_layout-no_merging = ''.

gs_layout-box_fname = 'CHECKBOX'.

DATA lv_pos TYPE i.

DATA ls_fieldcat LIKE LINE OF gt_fieldcat.

DEFINE %%append_fieldcat.

lv_pos = lv_pos 1.

ls_fieldcat-col_pos = lv_pos.

ls_fieldcat-fieldname = &1."字段名

ls_fieldcat-coltext = &2."文本

* ls_fieldcat-no_zero = &3."去掉前导零

ls_fieldcat-ref_table = &3.

ls_fieldcat-ref_field = &4.

APPEND ls_fieldcat TO gt_fieldcat.

CLEAR ls_fieldcat.

END-OF-DEFINITION.

%%append_fieldcat:

'CHECKBOX' '选择' '' '',

'MATNR' '' 'MARA' 'MATNR',

'MTART' '' 'MARA' 'MTART',

'MATKL' '' 'MARA' 'MATKL',

'MAKTX' '' 'MAKT' 'MAKTX'.

ENDFORM.

*&---------------------------------------------------------------------*

*& Module STATUS_9000 OUTPUT

*&---------------------------------------------------------------------*

*&

*&---------------------------------------------------------------------*

MODULE status_9000 OUTPUT.

SET PF-STATUS 'STATUS'.

SET TITLEBAR 'TITLE'.

ENDMODULE.

*&---------------------------------------------------------------------*

*& Module USER_COMMAND_9000 INPUT

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

MODULE user_command_9000 INPUT.

ok_code = sy-ucomm.

CASE ok_code.

WHEN 'BACK' OR 'EXIT'.

LEAVE TO SCREEN 0.

WHEN 'CANC'.

LEAVE PROGRAM.

WHEN OTHERS.

ENDCASE.

ENDMODULE.

*&---------------------------------------------------------------------*

*& Form FRM_PRINT_DATA

*&---------------------------------------------------------------------*

*& text

*&---------------------------------------------------------------------*

*& --> p1 text

*& <-- p2 text

*&---------------------------------------------------------------------*

FORM frm_print_data .

TYPES: lty_spopli TYPE TABLE OF spopli WITH EMPTY KEY.

DATA(lt_spopli) = VALUE lty_spopli(

( selflag = 'X' varoption = '打印')

( varoption = '仅预览')

( varoption = '输出PDF')

).

DATA: antwort TYPE c.

"选择打印功能

CALL FUNCTION 'POPUP_TO_DECIDE_LIST'

EXPORTING

cursorline = 1

mark_flag = 'X'

mark_max = 1

* START_COL = 0

* START_ROW = 0

textline1 = '请选择输出功能'

* TEXTLINE2 = ' '

* TEXTLINE3 = ' '

titel = '功能选择'

* DISPLAY_ONLY = ' '

IMPORTING

answer = antwort

TABLES

t_spopli = lt_spopli

EXCEPTIONS

not_enough_answers = 1

too_much_answers = 2

too_much_marks = 3

OTHERS = 4.

IF sy-subrc NE 0.

MESSAGE '请选择一个功能' TYPE 'I'.

RETURN.

ENDIF.

CHECK antwort IS NOT INITIAL.

DATA: fname(30) VALUE 'ZTEST_SMARTFORMS_01'.

DATA: fm_name TYPE rs38l_fnam.

DATA: ls_control_param TYPE ssfctrlop ."控制结构

DATA: ls_composer_param TYPE ssfcompop ."处理输出

DATA: outopt TYPE ssfcresop.

DATA: i_job_output_info TYPE ssfcrescl.

CASE antwort.

WHEN '1'.

ls_control_param-langu = '1'.

ls_control_param-no_open = 'X'."控制smartforms的调用

ls_control_param-no_close = 'X'."控制假脱机系统

ls_control_param-device = 'PRINTER'."

"最后一张票据需要关闭假脱机 ls_control_param-no_close = space

PERFORM frm_print_smf USING

ls_control_param

ls_composer_param

fname

CHANGING

i_job_output_info.

WHEN '2'."直接预览

ls_composer_param-tdnoprint = 'X'."预览界面无打印按钮

ls_control_param-langu = '1'.

ls_control_param-no_open = 'X'."控制smartforms的调用

ls_control_param-no_close = 'X'."控制假脱机系统

"最后一张票据需要关闭假脱机 ls_control_param-no_close = space

ls_control_param-no_dialog = 'X'."

ls_control_param-device = 'PRINTER'."

ls_control_param-preview = 'X'."预览

PERFORM frm_print_smf USING

ls_control_param

ls_composer_param

fname

CHANGING

i_job_output_info.

WHEN '3'.

DATA: lv_fullpath TYPE string.

DATA: l_filename TYPE string.

DATA: l_bin_filesize TYPE i.

DATA :l_bin_file TYPE xstring.

DATA: lt_lines TYPE STANDARD TABLE OF tline.

CALL METHOD cl_gui_frontend_services=>directory_browse

EXPORTING

window_title = '选择目录'

* INITIAL_FOLDER =

CHANGING

selected_folder = lv_fullpath.

* 根据SmartForm 名称获得Form的 Function Name

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'

EXPORTING

formname = fname

IMPORTING

fm_name = fm_name

EXCEPTIONS

no_form = 1

no_function_module = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE 'Call SSF_FUNCTION_MODULE_NAME Error' TYPE 'E'.

ENDIF.

ls_control_param-langu = '1'.

ls_control_param-no_dialog = 'X'."

ls_control_param-getotf = 'X'."

ls_composer_param-tddest = 'LP01'."打印机

* smartforms

CALL FUNCTION fm_name

EXPORTING

control_parameters = ls_control_param

output_options = ls_composer_param

IMPORTING

job_output_info = i_job_output_info

EXCEPTIONS

formatting_error = 1

internal_error = 2

send_error = 3

user_canceled = 4

OTHERS = 5.

IF sy-subrc <> 0.

MESSAGE 'Call SmartForms Error' TYPE 'E'.

ENDIF.

* Convert content to PDF

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

format = 'PDF'

IMPORTING

bin_filesize = l_bin_filesize

* BIN_FILE = L_BIN_FILE

TABLES

otf = i_job_output_info-otfdata[]

lines = lt_lines

EXCEPTIONS

err_max_linewidth = 1

err_format = 2

err_conv_not_possible = 3

OTHERS = 4.

CONCATENATE lv_fullpath '' sy-datum sy-uzeit

'.PDF' INTO l_filename.

* download pdf

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

bin_filesize = l_bin_filesize

filename = l_filename

filetype = 'BIN'

TABLES

data_tab = lt_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.

WHEN OTHERS.

ENDCASE.

ENDFORM.

*&---------------------------------------------------------------------*

*& Form FRM_PRINT_SMF

*&---------------------------------------------------------------------*

*& text

*&---------------------------------------------------------------------*

*& --> LS_CONTROL_PARAM

*& --> LS_COMPOSER_PARAM

*& --> FNAME

*& <-- I_JOB_OUTPUT_INFO

*&---------------------------------------------------------------------*

FORM frm_print_smf USING ls_control_param TYPE ssfctrlop

ls_composer_param TYPE ssfcompop

fname TYPE char30

CHANGING i_job_output_info TYPE ssfcrescl.

DATA: outopt TYPE ssfcresop,

fm_name TYPE rs38l_fnam.

CALL FUNCTION 'SSF_OPEN'

EXPORTING

control_parameters = ls_control_param

output_options = ls_composer_param

IMPORTING

job_output_options = outopt

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.

EXIT.

ENDIF.

* 根据SmartForm 名称获得Form的 Function Name

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'

EXPORTING

formname = fname

IMPORTING

fm_name = fm_name

EXCEPTIONS

no_form = 1

no_function_module = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE 'Call SSF_FUNCTION_MODULE_NAME Error' TYPE 'E'.

ENDIF.

CALL FUNCTION fm_name

EXPORTING

control_parameters = ls_control_param

output_options = ls_composer_param

EXCEPTIONS

formatting_error = 1

internal_error = 2

send_error = 3

user_canceled = 4

OTHERS = 5.

CALL FUNCTION 'SSF_CLOSE'

IMPORTING

job_output_info = i_job_output_info

EXCEPTIONS

formatting_error = 1

internal_error = 2

send_error = 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.

ENDIF.

ENDFORM.

此程序使用OOALV :GR_ALV是自己创建的OO ALV的类,打印的按钮代码如下

CLASS lcl_alv DEFINITION .

PUBLIC SECTION.

······

"自定义工具栏

METHODS handle_toolbar

FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING e_object

e_interactive.

" 响应用户命令

METHODS handle_user_command

FOR EVENT user_command OF cl_gui_alv_grid

IMPORTING e_ucomm.

······

ENDCLASS .

"实施

CLASS lcl_alv IMPLEMENTATION.

METHOD handle_toolbar.

DATA:

ls_toolbar TYPE stb_button.

CLEAR ls_toolbar.

MOVE 'PRINT' TO ls_toolbar-function.

MOVE TEXT-t01 TO ls_toolbar-text.

MOVE icon_print TO ls_toolbar-icon.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD.

METHOD handle_user_command.

······

CASE e_ucomm.

WHEN 'PRINT'.

PERFORM frm_print_data. "调用打印

WHEN OTHERS.

ENDCASE.

DATA ls_stbl TYPE lvc_s_stbl.

ls_stbl-row = 'X'.

ls_stbl-col = 'X'.

CALL METHOD cr_grid->refresh_table_display

EXPORTING

is_stable = ls_stbl.

ENDCLASS.

DATA gr_alv TYPE REF TO lcl_alv.

—— E N D ——

0 人点赞