printData

2022-06-27 10:13 更新

设置打印数据并打印

printData({params}, callback(ret, err))

params

data:

  • 类型:JSON 对象

  • 描述:(必填项)打印数据

  • 内部字段:

  [{
    rowtype: 'printText',//打印文字,文字宽度满一行自动换行排版,不满一整行不打印除非强制换行
    text : '要打印的文字字符串'    //要打印的文字字符串
  },{
    rowtype: 'printOriginalText',   //文字按矢量文字宽度原样输出,即每个字符不等宽
    text : '要打印的文字字符串'    //要打印的文字字符串
  },{
    rowtype: 'printTextWithFont',   //打印指定字体的文本,字体设置只对本次有效
    text : '要打印的文字字符串',    //要打印的文字字符串
    typeface:'', //字体名称(目前只支持"gh"字体)
    fontsize:12 //字体大小
  },{
    rowtype: 'printColumnsText',   //打印表格的一行,可以指定列宽、对齐方式
    colsTextArr : ['商品名称','数量','单位','金额'],  //各列文本字符串数组
    colsWidthArr : [10, 6, 6, 8], //各列宽度数组(以英文字符计算, 每个中文字符占两个英文字符, 每个宽度大于0)
    colsAlign : [0, 2, 2, 2] //各列对齐方式(0居左, 1居中, 2居右)
    //备注: 三个参数的数组长度应该一致, 如果colsText[i]的宽度大于colsWidth[i], 则文本换行
  },{
    rowtype: 'setFontSize',   //设置字体大小, 对之后打印有影响,除非初始化
    fontsize:12 //字体大小
  },{
    rowtype: 'lineWrap',  //打印机走纸(强制换行,结束之前的打印内容后走纸n行)
    n:3  //走纸行数
  },{
    rowtype: 'setAlignment',  //设置对齐模式,对之后打印有影响,除非初始化
    alignment:0  //对齐方式 0--居左 , 1--居中, 2--居右
  },{
    rowtype: 'printBarCode',  //打印一维条码
    data:'123456',  //条码数据
    symbology:8,//0-8
    width:160, //条码宽度: 取值2至6, 默认2
    height:160, //条码高度:取值1到255, 默认162
    textposition : 2 //文字位置 0--不打印文字, 1--文字在条码上方, 2--文字在条码下方, 3--条码上下方均打印
  },{
    rowtype: 'printQRCode',  //打印二维条码
    data:'www.hanchao9999.com',  //二维码数据
    size:240 //二维码块大小(单位:点)
  },{
    rowtype: 'printBitmap',  //打印图片
    image:'widget://res/Images/1.jpg'  //图片bitmap对象(最大宽度384像素,超过无法打印并且回调callback异常函数) 支持widget:// fs://
  },{
    rowtype: 'printTypeHorizontalLine',  //画特殊水平线
    type:0, //水平线类型(0至11)
  }]

条码说明(不同机型略有不同)

编码 说明
code39 最长打印 13 个数字
code93 最长打印 17 个数字
code128 最长打印 15 个数字
ean8 要求 8 位数字(最后一位校验位) ,有效长度 8 个数字
ean13 有效长度 13 个数字,其中最后一位为校验位
ITF 要求输入数字,且有效小于 14 位,必须是偶数位
Codebar 要求 0-9 及 6 个特殊字符,最长打印 18 个数字
UPC-E 要求 8 位数字(最后一位校验位)
UPC-A 要求 12 位数字(最后一位校验位)

callback(ret)

ret:

  • 类型:JSON对象
  • 内部字段:result 为返回结果各参数可参考以上参数

{
    status: true, //false
    print:"打印完毕"
}

示例代码

var obj = api.require('sunmiT1Printer');
obj.printData({
                data : [{
                    rowtype : 'printColumnsText',
                    colsTextArr : ['名称','数量','单价','金额'],
                    colsWidthArr : [10, 6, 6, 8],
                    colsAlign : [1, 2, 2, 2]
                },{
                    rowtype : 'printColumnsText',
                    colsTextArr : ['大白兔奶糖','4','12.00','48.00'],
                    colsWidthArr : [10, 6, 6, 8],
                    colsAlign : [0, 2, 2, 2]
                },{
                    rowtype : 'printText',
                    text : '支付宝\n'
                }, {
                    rowtype : 'printText',
                    text : 'Alipay\n'
                }, {
                    rowtype : 'printText',
                    text : '===============================\n'
                }, {
                    rowtype : 'printText',
                    text : '订单金额                 0.01元\n'
                }, {
                    rowtype : 'printText',
                    text : '    参与优惠金额        0.01元\n'
                }, {
                    rowtype : 'printText',
                    text : '商家实收\n'
                }, {
                    rowtype : 'printText',
                    text : '-------------------------------\n'
                }, {
                    rowtype : 'printText',
                    text : '开票金额(用户实付)   0.01元\n'
                }, {
                    rowtype : 'printText',
                    text : '--------------------------------\n'
                }, {
                    rowtype : 'printText',
                    text : '交易号:\n'
                }, {
                    rowtype : 'printText',
                    text : '201805291136661631631\n'
                }, {
                    rowtype : 'printText',
                    text : '门店名称  北国超市(新石店)\n'
                }, {
                    rowtype : 'printText',
                    text : '买家帐号   service@hanchao9999.com\n'
                }, {
                    rowtype : 'printText',
                    text : '--------------------------------\n'
                }, {
                    rowtype : 'printText',
                    text : '日期           2018-05-26 11:36\n'
                }, {
                    rowtype : 'printText',
                    text : '--------------------------------\n'
                }, {
                    rowtype : 'printText',
                    text : '此小票不需要用户签字\n'
                }, {
                    rowtype : 'setFontSize',
                    fontsize : 32
                }, {
                    rowtype : 'printText',
                    text : 'http://www.hanchao9999.com\n'
                }, {
                    rowtype : 'printOriginalText',
                    text : 'http://www.hanchao9999.com\n'
                }, {
                    rowtype : 'setFontSize',
                    fontsize : 24
                }, {
                    rowtype : 'printText',
                    text : 'http://www.hanchao9999.com\n'
                }, {
                    rowtype : 'printOriginalText',
                    text : 'http://www.hanchao9999.com\n'
                }, {
                    rowtype : 'lineWrap',
                    n : 2
                }, {
                    rowtype : 'setAlignment',
                    alignment : 1
                }, {
                    rowtype : 'printBarCode',
                    data : '123456',
                    width : 160,
                    height : 160
                }, {
                    rowtype : 'setAlignment',
                    alignment : 1
                }, {
                    rowtype : 'printQRCode',
                    data : 'http://www.hanchao9999.com',
                    size : 240
                }, {
                    rowtype : 'printTypeHorizontalLine',
                    type : 0
                }, {
                    rowtype : 'printTypeHorizontalLine',
                    type : 1
                }, {
                    rowtype : 'printTypeHorizontalLine',
                    type : 2
                }, {
                    rowtype : 'printTypeHorizontalLine',
                    type : 3
                }, {
                    rowtype : 'printBitmap',
                    image : 'widget://res/Images/1.jpg'
                }, {
                    rowtype : 'lineWrap',
                    n : 3
                }]
            }, function(ret, err) {
                alert(JSON.stringify(ret));
            });

可用性

Android系统

可提供的1.0.0及更高版本

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号