2019.12.24 【ABAP随笔】smartforms 打印及PDF转化
冬至已过,又临平安夜和圣诞,又是一年的末尾,闲暇时间需要静下心来温故而知新。
许久未碰打印,知识于脑子又有几分糊涂,遂整理些许知识,记录。
数据随便取于物料表
1 report Z_smartforms 2 3 TABLES:mara. 4 5 TYPES:BEGIN OF ty_data, 6 checkbox TYPE c, 7 matnr TYPE mara-matnr, 8 mtart TYPE mara-mtart, 9 matkl TYPE mara-matkl, 10 maktx TYPE makt-maktx, 11 END OF ty_data. 12 DATA gt_data TYPE TABLE OF ty_data. 13 14 15 *INCLUDE zalv_oo_cls. 16 INCLUDE z_smartforms_cls. 17 DATA ok_code TYPE sy-ucomm. 18 19 DATA: gt_fieldcat TYPE lvc_t_fcat. "字段操作表 20 DATA: gs_layout TYPE lvc_s_layo. 21 22 SELECT-OPTIONS: 23 matnr FOR mara-matnr, 24 mtart FOR mara-mtart, 25 matkl FOR mara-matkl. 26 27 START-OF-SELECTION. 28 29 SELECT 30 a~matnr , 31 a~mtart , 32 a~matkl , 33 b~maktx 34 INTO CORRESPONDING FIELDS OF TABLE @gt_data 35 FROM mara AS a 36 INNER JOIN makt AS b ON a~matnr = b~matnr 37 UP TO 30 ROWS. 38 39 END-OF-SELECTION. 40 CHECK gt_data[] IS NOT INITIAL. 41 42 CREATE OBJECT gr_alv. 43 44 PERFORM frm_set_fieldcat. 45 46 CALL METHOD gr_alv->standard_oo_alv 47 EXPORTING 48 iv_screen = '9000' 49 iv_default_ex = 'X' 50 is_layout = gs_layout 51 iv_self_ucomm = 'X' 52 iv_container_name = 'TC' 53 CHANGING 54 * CR_DOCK = GR_DOCK 55 * CR_GRID = GR_GRID 56 ct_fieldcat = gt_fieldcat 57 ct_data = gt_data 58 EXCEPTIONS 59 create_alv_failed = 1. 60 61 CALL SCREEN 9000. 62 63 FORM frm_set_fieldcat. 64 65 gs_layout-sel_mode = 'D' . "模式 66 gs_layout-zebra = 'X'. "ALV表格按斑马线条纹显示 67 gs_layout-cwidth_opt = 'X'. "将ALV字段宽度设置为最优化 68 gs_layout-no_merging = ''. 69 gs_layout-box_fname = 'CHECKBOX'. 70 71 DATA lv_pos TYPE i. 72 DATA ls_fieldcat LIKE LINE OF gt_fieldcat. 73 DEFINE %%append_fieldcat. 74 lv_pos = lv_pos + 1. 75 ls_fieldcat-col_pos = lv_pos. 76 ls_fieldcat-fieldname = &1."字段名 77 ls_fieldcat-coltext = &2."文本 78 * ls_fieldcat-no_zero = &3."去掉前导零 79 ls_fieldcat-ref_table = &3. 80 ls_fieldcat-ref_field = &4. 81 APPEND ls_fieldcat TO gt_fieldcat. 82 CLEAR ls_fieldcat. 83 END-OF-DEFINITION. 84 85 %%append_fieldcat: 86 'CHECKBOX' '选择' '' '', 87 'MATNR' '' 'MARA' 'MATNR', 88 'MTART' '' 'MARA' 'MTART', 89 'MATKL' '' 'MARA' 'MATKL', 90 'MAKTX' '' 'MAKT' 'MAKTX'. 91 ENDFORM. 92 93 *&---------------------------------------------------------------------* 94 *& Module STATUS_9000 OUTPUT 95 *&---------------------------------------------------------------------* 96 *& 97 *&---------------------------------------------------------------------* 98 MODULE status_9000 OUTPUT. 99 SET PF-STATUS 'STATUS'. 100 SET TITLEBAR 'TITLE'. 101 ENDMODULE. 102 103 *&---------------------------------------------------------------------* 104 *& Module USER_COMMAND_9000 INPUT 105 *&---------------------------------------------------------------------* 106 * text 107 *----------------------------------------------------------------------* 108 MODULE user_command_9000 INPUT. 109 110 ok_code = sy-ucomm. 111 112 CASE ok_code. 113 WHEN 'BACK' OR 'EXIT'. 114 LEAVE TO SCREEN 0. 115 WHEN 'CANC'. 116 LEAVE PROGRAM. 117 WHEN OTHERS. 118 ENDCASE. 119 120 ENDMODULE. 121 *&---------------------------------------------------------------------* 122 *& Form FRM_PRINT_DATA 123 *&---------------------------------------------------------------------* 124 *& text 125 *&---------------------------------------------------------------------* 126 *& --> p1 text 127 *& <-- p2 text 128 *&---------------------------------------------------------------------* 129 FORM frm_print_data . 130 131 TYPES: lty_spopli TYPE TABLE OF spopli WITH EMPTY KEY. 132 133 DATA(lt_spopli) = VALUE lty_spopli( 134 ( selflag = 'X' varoption = '打印') 135 ( varoption = '仅预览') 136 ( varoption = '输出PDF') 137 ). 138 139 DATA: antwort TYPE c. 140 141 "选择打印功能 142 CALL FUNCTION 'POPUP_TO_DECIDE_LIST' 143 EXPORTING 144 cursorline = 1 145 mark_flag = 'X' 146 mark_max = 1 147 * START_COL = 0 148 * START_ROW = 0 149 textline1 = '请选择输出功能' 150 * TEXTLINE2 = ' ' 151 * TEXTLINE3 = ' ' 152 titel = '功能选择' 153 * DISPLAY_ONLY = ' ' 154 IMPORTING 155 answer = antwort 156 TABLES 157 t_spopli = lt_spopli 158 EXCEPTIONS 159 not_enough_answers = 1 160 too_much_answers = 2 161 too_much_marks = 3 162 OTHERS = 4. 163 IF sy-subrc NE 0. 164 MESSAGE '请选择一个功能' TYPE 'I'. 165 RETURN. 166 ENDIF. 167 168 CHECK antwort IS NOT INITIAL. 169 170 DATA: fname(30) VALUE 'ZTEST_SMARTFORMS_01'. 171 DATA: fm_name TYPE rs38l_fnam. 172 173 174 DATA: ls_control_param TYPE ssfctrlop ."控制结构 175 DATA: ls_composer_param TYPE ssfcompop ."处理输出 176 DATA: outopt TYPE ssfcresop. 177 DATA: i_job_output_info TYPE ssfcrescl. 178 179 CASE antwort. 180 WHEN '1'. 181 ls_control_param-langu = '1'. 182 ls_control_param-no_open = 'X'."控制smartforms的调用 183 ls_control_param-no_close = 'X'."控制假脱机系统 184 ls_control_param-device = 'PRINTER'." 185 "最后一张票据需要关闭假脱机 ls_control_param-no_close = space 186 187 PERFORM frm_print_smf USING 188 ls_control_param 189 ls_composer_param 190 fname 191 CHANGING 192 i_job_output_info. 193 WHEN '2'."直接预览 194 ls_composer_param-tdnoprint = 'X'."预览界面无打印按钮 195 ls_control_param-langu = '1'. 196 ls_control_param-no_open = 'X'."控制smartforms的调用 197 ls_control_param-no_close = 'X'."控制假脱机系统 198 "最后一张票据需要关闭假脱机 ls_control_param-no_close = space 199 ls_control_param-no_dialog = 'X'." 200 ls_control_param-device = 'PRINTER'." 201 ls_control_param-preview = 'X'."预览 202 203 PERFORM frm_print_smf USING 204 ls_control_param 205 ls_composer_param 206 fname 207 CHANGING 208 i_job_output_info. 209 WHEN '3'. 210 211 DATA: lv_fullpath TYPE string. 212 DATA: l_filename TYPE string. 213 DATA: l_bin_filesize TYPE i. 214 DATA :l_bin_file TYPE xstring. 215 DATA: lt_lines TYPE STANDARD TABLE OF tline. 216 217 CALL METHOD cl_gui_frontend_services=>directory_browse 218 EXPORTING 219 window_title = '选择目录' 220 * INITIAL_FOLDER = 221 CHANGING 222 selected_folder = lv_fullpath. 223 224 * 根据SmartForm 名称获得Form的 Function Name 225 CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' 226 EXPORTING 227 formname = fname 228 IMPORTING 229 fm_name = fm_name 230 EXCEPTIONS 231 no_form = 1 232 no_function_module = 2 233 OTHERS = 3. 234 IF sy-subrc <> 0. 235 MESSAGE 'Call SSF_FUNCTION_MODULE_NAME Error' TYPE 'E'. 236 ENDIF. 237 238 ls_control_param-langu = '1'. 239 ls_control_param-no_dialog = 'X'." 240 ls_control_param-getotf = 'X'." 241 ls_composer_param-tddest = 'LP01'."打印机 242 243 244 * smartforms 245 CALL FUNCTION fm_name 246 EXPORTING 247 control_parameters = ls_control_param 248 output_options = ls_composer_param 249 IMPORTING 250 job_output_info = i_job_output_info 251 EXCEPTIONS 252 formatting_error = 1 253 internal_error = 2 254 send_error = 3 255 user_canceled = 4 256 OTHERS = 5. 257 IF sy-subrc <> 0. 258 MESSAGE 'Call SmartForms Error' TYPE 'E'. 259 ENDIF. 260 261 * Convert content to PDF 262 CALL FUNCTION 'CONVERT_OTF' 263 EXPORTING 264 format = 'PDF' 265 IMPORTING 266 bin_filesize = l_bin_filesize 267 * BIN_FILE = L_BIN_FILE 268 TABLES 269 otf = i_job_output_info-otfdata[] 270 lines = lt_lines 271 EXCEPTIONS 272 err_max_linewidth = 1 273 err_format = 2 274 err_conv_not_possible = 3 275 OTHERS = 4. 276 277 278 CONCATENATE lv_fullpath '\' sy-datum sy-uzeit 279 '.PDF' INTO l_filename. 280 281 282 * download pdf 283 CALL FUNCTION 'GUI_DOWNLOAD' 284 EXPORTING 285 bin_filesize = l_bin_filesize 286 filename = l_filename 287 filetype = 'BIN' 288 TABLES 289 data_tab = lt_lines 290 EXCEPTIONS 291 file_write_error = 1 292 no_batch = 2 293 gui_refuse_filetransfer = 3 294 invalid_type = 4 295 no_authority = 5 296 unknown_error = 6 297 header_not_allowed = 7 298 separator_not_allowed = 8 299 filesize_not_allowed = 9 300 header_too_long = 10 301 dp_error_create = 11 302 dp_error_send = 12 303 dp_error_write = 13 304 unknown_dp_error = 14 305 access_denied = 15 306 dp_out_of_memory = 16 307 disk_full = 17 308 dp_timeout = 18 309 file_not_found = 19 310 dataprovider_exception = 20 311 control_flush_error = 21 312 OTHERS = 22. 313 WHEN OTHERS. 314 ENDCASE. 315 316 ENDFORM. 317 *&---------------------------------------------------------------------* 318 *& Form FRM_PRINT_SMF 319 *&---------------------------------------------------------------------* 320 *& text 321 *&---------------------------------------------------------------------* 322 *& --> LS_CONTROL_PARAM 323 *& --> LS_COMPOSER_PARAM 324 *& --> FNAME 325 *& <-- I_JOB_OUTPUT_INFO 326 *&---------------------------------------------------------------------* 327 FORM frm_print_smf USING ls_control_param TYPE ssfctrlop 328 ls_composer_param TYPE ssfcompop 329 fname TYPE char30 330 CHANGING i_job_output_info TYPE ssfcrescl. 331 DATA: outopt TYPE ssfcresop, 332 fm_name TYPE rs38l_fnam. 333 334 CALL FUNCTION 'SSF_OPEN' 335 EXPORTING 336 control_parameters = ls_control_param 337 output_options = ls_composer_param 338 IMPORTING 339 job_output_options = outopt 340 EXCEPTIONS 341 formatting_error = 1 342 internal_error = 2 343 send_error = 3 344 user_canceled = 4 345 OTHERS = 5. 346 IF sy-subrc <> 0. 347 MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 348 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 349 EXIT. 350 ENDIF. 351 352 * 根据SmartForm 名称获得Form的 Function Name 353 CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' 354 EXPORTING 355 formname = fname 356 IMPORTING 357 fm_name = fm_name 358 EXCEPTIONS 359 no_form = 1 360 no_function_module = 2 361 OTHERS = 3. 362 IF sy-subrc <> 0. 363 MESSAGE 'Call SSF_FUNCTION_MODULE_NAME Error' TYPE 'E'. 364 ENDIF. 365 366 CALL FUNCTION fm_name 367 EXPORTING 368 control_parameters = ls_control_param 369 output_options = ls_composer_param 370 EXCEPTIONS 371 formatting_error = 1 372 internal_error = 2 373 send_error = 3 374 user_canceled = 4 375 OTHERS = 5. 376 377 378 CALL FUNCTION 'SSF_CLOSE' 379 IMPORTING 380 job_output_info = i_job_output_info 381 EXCEPTIONS 382 formatting_error = 1 383 internal_error = 2 384 send_error = 3 385 OTHERS = 4. 386 IF sy-subrc <> 0. 387 MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 388 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 389 ENDIF. 390 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.
效果如下:
1.打印,带控制台
2.仅预览,不允许打印,禁用打印按钮
3.输出PDF
-文至此,以展示打印,预览 和 PDF输出三种格式输出
-TAB 热爱技术 享受生活
"