ABAP Smartforms
ABAP创建可以打印的窗体,二维码等。Tcode: SMARTFORMS,可以创建对应Form以及对应样式Style。
1.创建Style
创建Style:ZTOM_TEST_STYLE1;
整体样式,
指定默认Paragraph的格式L1;
指定字体,大小等;
Paragraph样式,
指定段落字体对齐方式;
设置段落字体及字体大小;
Character Formats:
打印二维码,条形码使用的字体格式;
2.创建Form
创建Smartforms:ZTOM_TEST_FORM1;
Form Attributes:
Page Format:指定打印纸张类型;
Characters per Inch:每字符间距;
Lines per Inch:每行间距;
Form Interface:form窗体提供给外部接口;
import:输入参数;
export:输出参数;
table:table参数,ZTOM_TSPFLI是Table type,line type为spfli;
Global Definition:全局参数定义,form内部使用的参数;
3.创建Form->Page,Window
点击Form Painter,可以看到窗口:MAIN,HEADER,PRINT_DATE,TABLE_FOOT的位置。
点击Field List on/off,可以打开或关闭Field List。这里可以看到我们定义的参数,已经系统提供的参数,例如:当前页码SFSY-PAGE,系统日期SFSY-DATE等。
Pages and Windows:定义页面以及窗口,右键选择创建Page,Window等。一个页面可以由多个窗口构成;
next Page:指定下一页;
Window type:设置窗口类型;
Windows窗口下可以创建多种组件。
Table:table组件显示Table内表;
Template:模板显示固定格式信息;
Text:文本信息;
Graphic:图片;
Address:地址;
Flow Logic:逻辑代码,Alternative分支判断;Loop循环语句;Program Lines:代码行;Command:命令,例如跳转新页;
创建Loop:主页面需要循环LT_HEADPRE,循环显示LT_ITEMPRE中内容;
Table定义两种行类型,LTYPE:表头及数据显示格式;TOTAL:汇总行格式;
创建Table:table显示内表LT_ITEMPRE,指定HEAD筛选对应信息;
Table由三部分组成:Header表头,Main Area数据,Footer表尾;
需要设定每个Cell下Text的内容;
这里有多少个cell决定于Table定义的Line Type;
Cell下面可以创建Text,ProgramLine等;
这里可以统计GV_SEATMAX_COUNT,最大座位数累计,显示在Footer;
Text内容,使用&xxxx&方法Form传入的参数数据,Styles就是使用ZTOM_TEST_STYLE1中C1;
这里创建Program Lines,LT_ITEMPRE循环完,页码加1,因为设计是相同carrid,connid显示同一个表。
创建Alternative,当条件为真,统计页数小于LT_HEAD内表行数,执行Command,跳转新页,继续执行循环;
这里最后创建Program Lines,清空累加的最大座位数加总等数据;
Window:HEADER,创建Template,可以看到有5行,第一行有三列,其余行两列,按照Template划分Cell,然后填充Text数据;
在Output Options指定Text所在行和列;
关于打印条形码、二维码,只需设置对应Text的Styles选择B1(创建的Barcode格式);
ABAP调用打印或者转换成PDF
示例:打印预览
"Smart forms
"SMARTFORMS:创建smart form
"SE78:上传图片到SAP
"SE73:SAPScript font maintaince,二维码字体维护:ZMARTX.
"打印预览
FORM f_print_preview.
DATA:fm_name TYPE RS38L_FNAM.
DATA:lv_ssfcrespd TYPE SSFCRESPD.
DATA:ls_ssfcrescl TYPE SSFCRESCL.
DATA:ls_ssfcresop TYPE SSFCRESOP.
"打印参数
DATA:ls_ssfctrlop TYPE SSFCTRLOP.
"弹出框参数
DATA:ls_ssfcompop TYPE SSFCOMPOP.
"获取form 对应 function module name
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
FORMNAME = 'ZTOM_TEST_FORM1' "Form name
* VARIANT = ' '
* DIRECT_CALL = ' '
IMPORTING
FM_NAME = fm_name
EXCEPTIONS
NO_FORM = 1
NO_FUNCTION_MODULE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE e000(ZTEST_MSG) WITH '获取function name失败'.
ENDIF.
"设置打印参数
"无弹窗
ls_ssfctrlop-NO_DIALOG = 'X'.
"获取OTF数据,不print,fax,display
"ls_ssfctrlop-GETOTF = 'X'.
"预览
ls_ssfctrlop-PREVIEW = 'X'.
"output参数
ls_ssfcompop-TDDEST = 'SAP'.
"动态调用
CALL FUNCTION fm_name
EXPORTING
* ARCHIVE_INDEX = ''
* ARCHIVE_INDEX_TAB = ''
* ARCHIVE_PARAMETERS = ''
CONTROL_PARAMETERS = ls_ssfctrlop
* MAIL_APPL_OBJ = ''
* MAIL_RECIPIENT = ''
* MAIL_SENDER = ''
OUTPUT_OPTIONS = ls_ssfcompop
* USER_SETTINGS = ''
IMPORTING
* DOCUMENT_OUTPUT_INFO = lv_ssfcrespd
JOB_OUTPUT_INFO = ls_ssfcrescl
JOB_OUTPUT_OPTIONS = ls_ssfcresop
TABLES
lt_headpre = lt_spfli
lt_itempre = lt_sflight.
ENDFORM.
示例:转换PDF
"导出pdf
FORM f_export_pdf.
DATA:fm_name TYPE RS38L_FNAM.
DATA:lv_ssfcrespd TYPE SSFCRESPD.
DATA:ls_ssfcrescl TYPE SSFCRESCL.
DATA:ls_ssfcresop TYPE SSFCRESOP.
"打印参数
DATA:ls_ssfctrlop TYPE SSFCTRLOP.
"弹出框参数
DATA:ls_ssfcompop TYPE SSFCOMPOP.
"下载pdf数据
DATA:lv_pdf_data TYPE xstring.
DATA:lv_pdf_line TYPE I.
DATA:lv_filelength TYPE I.
"转换为pdf格式
DATA:lt_pdf_table TYPE STANDARD TABLE OF tline.
"doc
DATA:lt_doctab TYPE TABLE OF docs WITH HEADER LINE.
"获取form 对应 function module name
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
FORMNAME = 'ZTOM_TEST_FORM1' "Form name
* VARIANT = ' '
* DIRECT_CALL = ' '
IMPORTING
FM_NAME = fm_name
EXCEPTIONS
NO_FORM = 1
NO_FUNCTION_MODULE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE e000(ZTEST_MSG) WITH '获取function name失败'.
ENDIF.
"设置打印参数
"无弹窗
ls_ssfctrlop-NO_DIALOG = 'X'.
"获取OTF数据,不print,fax,display
ls_ssfctrlop-GETOTF = 'X'.
"预览
"ls_ssfctrlop-PREVIEW = 'X'.
"output参数
"ls_ssfcompop-TDDEST = 'SAP'.
"动态调用
CALL FUNCTION fm_name
EXPORTING
* ARCHIVE_INDEX = ''
* ARCHIVE_INDEX_TAB = ''
* ARCHIVE_PARAMETERS = ''
CONTROL_PARAMETERS = ls_ssfctrlop
* MAIL_APPL_OBJ = ''
* MAIL_RECIPIENT = ''
* MAIL_SENDER = ''
OUTPUT_OPTIONS = ls_ssfcompop
* USER_SETTINGS = ''
IMPORTING
* DOCUMENT_OUTPUT_INFO = lv_ssfcrespd
JOB_OUTPUT_INFO = ls_ssfcrescl
JOB_OUTPUT_OPTIONS = ls_ssfcresop
TABLES
lt_headpre = lt_spfli
lt_itempre = lt_sflight.
* "OTF数据转换PDF
* CALL FUNCTION 'CONVERT_OTF'
* EXPORTING
* FORMAT = 'PDF'
* MAX_LINEWIDTH = 132
** ARCHIVE_INDEX = ' '
** COPYNUMBER = 0
** ASCII_BIDI_VIS2LOG = ' '
** PDF_DELETE_OTFTAB = ' '
** PDF_USERNAME = ' '
** PDF_PREVIEW = ' '
** USE_CASCADING = ' '
** MODIFIED_PARAM_TABLE =
* IMPORTING
* BIN_FILESIZE = lv_pdf_line
* BIN_FILE = lv_pdf_data
* TABLES
* OTF = ls_ssfcrescl-OTFDATA[]
* LINES = lt_pdf_table
* EXCEPTIONS
* ERR_MAX_LINEWIDTH = 1
* ERR_FORMAT = 2
* ERR_CONV_NOT_POSSIBLE = 3
* ERR_BAD_OTF = 4
* OTHERS = 5
* .
* IF SY-SUBRC <> 0.
* MESSAGE e000(ZTEST_MSG) WITH 'CONVERT_OTF error'.
* ENDIF.
"OTF数据转换PDF
CALL FUNCTION 'CONVERT_OTF_2_PDF'
* EXPORTING
* USE_OTF_MC_CMD = 'X'
* ARCHIVE_INDEX =
IMPORTING
BIN_FILESIZE = lv_pdf_line
TABLES
OTF = ls_ssfcrescl-OTFDATA[]
DOCTAB_ARCHIVE = lt_doctab
LINES = lt_pdf_table
* EXCEPTIONS
* ERR_CONV_NOT_POSSIBLE = 1
* ERR_OTF_MC_NOENDMARKER = 2
* OTHERS = 3
.
IF SY-SUBRC <> 0.
MESSAGE e000(ZTEST_MSG) WITH 'CONVERT_OTF_2_PDF error'.
ENDIF.
"下载PDF
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
BIN_FILESIZE = lv_pdf_line
FILENAME = 'E:\test.pdf'
FILETYPE = 'BIN'
* APPEND = ' '
* WRITE_FIELD_SEPARATOR = ' '
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE
* SHOW_TRANSFER_STATUS = ABAP_TRUE
* VIRUS_SCAN_PROFILE = '/SCET/GUI_DOWNLOAD'
IMPORTING
FILELENGTH = lv_filelength
TABLES
DATA_TAB = lt_pdf_table
* FIELDNAMES =
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 e000(ZTEST_MSG) WITH 'GUI_DOWNLOAD error'.
ENDIF.
ENDFORM.
问题
1.编辑文本Text时,弹窗报错:CSapEditorCtrl::GetObject:Object 13 does not exist
可以执行Program: RSCPSETEDITOR,修改Smartforms使用的文本编辑器。
使用Smart Forms,不使用SAPscript,点击激活。
设置完成后,退出Smartfroms,重新执行Tcode再次编辑时,发现已经解决问题。