打印插件API参考手册

nengjj.getInstance()

获取打印实例,一个页面只能调用一次本函数,获取一个实例。

var instance = nengjj.getInstance(jsonConfig);

参数

jsonConfig

配置信息,Object类型,可选。包含的字段:

字段名称类型必选描述
hostString代理软件所在电脑的IP地址,默认值为"localhost"

返回值

返回Object类型的打印实例。

示例

指定IP、端口获取实例:

var instance = nengjj.getInstance({
    host: "192.168.1.10"
});

instance.getInfo()

获取插件的版本号,是否安装许可证等信息。

instance.getInfo(onsuccess, onerror);

参数

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultObject插件的相关信息

result中的字段:

字段名称类型描述
appVerString插件版本号
printVerString打印模块版本号
osString插件的运行平台,取值:win/linux/macos 之一

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

获取插件信息:

instance.getInfo(function(res){
    console.log(res);
});

输出:

{
    appVer: "1.2", // 插件版本
    isLicenseInstalled: true, // license是否安装
    os: "win", // 插件运行的平台
    printVer: "1.1" // 打印模块版本
    ...
}

instance.readLicense()

读取许可证(license)。

instance.readLicense(onsuccess, onerror);

参数

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultObjectlicense内容

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

安装许可证:

instance.readLicense(
    function(res) { // 调用成功回调函数
        console.log(res); 
    },
    function(status, text) { // 调用出错回调函数
        console.error(status + ":" + text);
    }
);

instance.installLicense()

更新许可证(license)。

instance.installLicense(license, onsuccess, onerror);

参数

license

许可证内容,String类型,必选。

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultString成功信息,例如:”设置成功”

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

安装许可证:

//
// 许可证只需安装一次,安装前可以先调用readLicense接口,获知许可证
// 是否已经安装
// 

instance.readLicense(
    function(res){
        console.log(res);
        if(!res) {
            var license = "..."; // 替换为license字符串
            instance.installLicense(license, null, function(st, text){
                console.log(st + ":" + text);
                alert(text);
            });
        }
    },
    function(st, text){
        console.log(st + ":" + text);
        if(st == 0) {
            alert("插件未启动,请确认插件已启动!");
        } else {
            alert(text);
        }
    }
);

instance.print.listPrinter()

获取打印机名称列表。

instance.print.listPrinter(onsuccess, onerror);

参数

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultArray打印机名称列表(字符串数组),例如:["Printer A", "Printer B" ]

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

获取打印机名称列表:

instance.print.listPrinter(
    function(res) { // 调用成功回调函数
        console.log(res);
    },
    function(status, text) { // 调用出错回调函数
        console.error(status + ":" + text);
    }
);

调用成功输出:

[
    "Microsoft XPS Document Writer", 
    "Microsoft Print to PDF", 
    "HP DeskJet 5820 series (网络)", 
    "Foxit Reader PDF Printer", 
    "Fax"
]

instance.print.getDefaultPrinter()

获取默认打印机名称。

instance.print.getDefaultPrinter(onsuccess, onerror);

参数

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultString打印机名称(字符串),例如:"Printer A"
  • result: 打印机名称(字符串),例如:"Printer A"

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

获取默认打印机名称:

instance.print.getDefaultPrinter(
    function(res) { // 调用成功回调函数
        console.log(res);
    },
    function(status, text) { // 调用出错回调函数
        console.error(status + ":" + text);
    }
);

调用成功过输出:

HP DeskJet 5820 series (网络)

instance.print.listPrinterPaper()

获取打印机支持的纸张规格。

instance.print.listPrinterPaper(printerName, onsuccess, onerror);

参数

printerName

指定打印机名称,String类型,必选。

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultArray纸张名称列表(json数组),格式如下。

result中,数组元素内容:

字段名称类型描述
nameString纸张名称,例如: “A4”
widthInteger纸张宽度, 单位0.01毫米
heightInteger纸张高度, 单位0.01毫米

result示例:

[
    {
        name: "A4",         // 纸张名称, String类型
        width: 21000,       // 纸张宽度, 单位0.01毫米, Integer类型
        height: 29700,      // 纸张高度, 单位0.01毫米, Integer类型
    },
    {
        name: "A5",         // 纸张名称, String类型
        width: 14800,       // 纸张宽度, 单位0.01毫米, Integer类型
        height: 21000,      // 纸张高度, 单位0.01毫米, Integer类型
    }
    ...
]

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

获取打印机支持的纸张规格:

var printerName = "xxx xxx";
instance.print.listPrinterPaper(
    printerName, 
    function(res) { // 调用成功回调函数
        console.log(res);
    },
    function(status, text) { // 调用出错回调函数
        console.error(status + ":" + text);
    }
);

调用成功过输出:

[
    {name: "信纸", height: 27940, width: 21590},
    {name: "法律专用纸", height: 35560, width: 21590},
    {name: "Statement", height: 21590,  width: 13970},
    {name: "Executive", height: 26670, width: 18410},
    {name: "A4", height: 29700, width: 21000},
    {name: "A5", height: 21000, width: 14800},
    // ...
]

instance.print.listPrinterJob()

获取打印机当前的作业列表。

instance.print.listPrinterJob(printerName, onsuccess, onerror);

参数

printerName

打印机名称,String类型,必选。

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultArray打印作业列表(json数组),格式如下

作业内容,Windows平台与Linux/macOS平台不同。

Windows平台作业信息字段:

字段名称类型描述
documentString文档名称, 详情参考 pDocument
idInteger作业ID,详情参考 JobId
statusInteger状态码, 详情参考 Status
priorityInteger作业优先级, 详情参考 Priority
sizeInteger打印内容大小,单位字节, 详情参考 Size
totalPagesInteger要打印的总页数, 详情参考 TotalPages
pagesPrintedInteger已打印页数,有些打印机总是返回0,详情参考 PagesPrinted
positionInteger当前作业在作业队列中位置, 详情参考 Position

Linux/macOS平台作业信息:

字段名称类型描述
documentString文档名称
idInteger作业ID
statusInteger作业状态

 

  • 3 (IPP_JSTATE_PENDING): 等待打印
  • 4 (IPP_JSTATE_HELD: 作业被保留用于打印
  • 5 (IPP_JSTATE_PROCESSING): 作业被保留用于打印
  • 6 (IPP_JSTATE_STOPPED): 作业已停止
  • 7 (IPP_JSTATE_CANCELED): 作业已被取消
  • 8 (IPP_JSTATE_ABORTED): 由于错误,作业已中止
  • 9 (IPP_JSTATE_COMPLETED): 工作已成功完成
priorityInteger作业优先级(1-100)
sizeInteger打印内容大小,单位字节

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

获取打印机当前的作业列表:

var printerName = "xxx xxx";
instance.print.listPrinterJob(
    printerName, 
    function(res) { // 调用成功回调函数
        console.log(res);
    },
    function(status, text) { // 调用出错回调函数
        console.error(status + ":" + text);
    }
);

instance.print.getPrinterState()

获取打印机状态。

instance.print.getPrinterState(printerName, onsuccess, onerror);

参数

printerName

指定打印机名称,String类型,必选。

onsuccess

成功回调函数,Function类型,可选。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultString打印机状态,是下面4个值之一:

 

  • "idle" 空闲
  • "active" 活跃
  • "aborted" 退出
  • "error" 错误

onerror

失败回调函数,Function类型,可选。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

获取打印机当前状态:

var printerName = "xxx xxx";
instance.print.getPrinterState(
    printerName, 
    function(res) { // 调用成功回调函数
        console.log(res); // 输出状态,例如:"idle"
    },
    function(status, text) { // 调用出错回调函数
        console.error(status + ":" + text);
    }
);

instance.print.print() / instance.print.printConfig() / instance.print.printPreview()

发送打印内容(文档)。这些接口都是异步调用,调用后立即返回。可通过监听事件,获取调用结果。

// 直接打印
function print(doc, onsuccess, onerror); 

// 弹出打印设置对话框,点击"打印"按钮打印
function printConfig(doc, onsuccess, onerror);  

// 显示打印预览窗口,点击"打印"按钮打印
function printPreview(doc, onsuccess, onerror); 

参数

doc

json格式,要打印的内容配置。

参数名称类型必选描述
printerNameString打印机名称,省略则使用默认打印机
nameString文档名称,默认值“文档”
typeString文档类型,可以取值html/url/pdf,默认值html
contentString要发送的打印内容。当typehtml类型时,content值为html内容,支持css,例如:"<div style='color: red; font-weight:bold;'>HTML内容</div>"。当typeurl类型时,content值为url,例如:"https://www.nengjj.com"
optionObject打印选项设置,选项详情见下面的表:option字段

表:option字段

参数名称类型必选描述
collateString是否逐份打印,取值"yes"/"no",默认值"yes"
colorString黑白或彩色打印,取值"yes",表示使用彩色打印,取值"no",表示使用黑白打印,默认值"yes"
orientationString纸张纵向或横向打印,取值"portrait",表示纵向打印,取值"landscape",表示横向打印,默认值"portrait"纵向打印
duplexString双面/单面打印(需要打印机支持)。
可取值如下:

 

  • "duplex_none" 单面打印
  • "duplex_auto" 由打印机的默认设置决定
  • "duplex_long_side" 双面(长边反转)
  • "duplex_short_side" 双面(短边反转)

默认值"duplex_none" 单面打印

copyCountInteger打印份数,默认值1
pageRangeString页码范围,可取值"1-8",表示打印1到8页,省略表示全部页面
paperWidth
paperHeight
Double纸张尺寸(规格),单位毫米。如果设置的纸张尺寸与标准纸张尺寸很接近,会自动转换为标准纸张尺寸。在设置纸张尺寸前,可使用listPrinterPaper函数获取打印机支持的纸张规格。省略使用打印机当前设置。
marginTop
marginBottom
marginLeft
marginRight
Double页面边距,单位毫米。打印机通常都有默认的最小边距(例如2.96mm),小于最小值的边距设置将无效。省略使用打印机当前设置。

onsuccess

成功回调函数。

onsuccess(result) {
    console.log(result); 
}

参数说明:

字段名称类型描述
resultObject打印内容发送结果

result包含的字段:

字段名称类型描述
docIdString生成文档的ID(UUID格式),可用于在监听事件时,过滤事件。

onerror

失败回调函数。

onerror(status, text) {
    console.log(status + ":" + text); 
}

参数说明:

字段名称类型描述
statusInteger错误状态号,例如:400
textString错误信息,例如:授权证书无效

返回值

示例

打印预览:

instance.print.printPreview({
    content: "<div style='color: red; font-weight:bold;'>HTML内容</div>",
    option: {
        marginTop: 10,
        marginBottom: 10,
        marginLeft: 10,
        marginRight: 10,
        paperWidth: 210,
        paperHeight: 297,
    }
},
function(result){
    console.log(result);
});


nengjj_event_print 事件

打印过程中的事件通知。打印请求都是异步请求,打印结果将以事件方式通知调用者,调用者需监听相关事件。

document.addEventListener("nengjj_event_print", function(e){
    console.log(e);
    console.log(e.detail);
});

事件详情包含在event.detail属性中。

event例子:

{
    type: "nengjj_event_print",
    detail: {
        docId: "1de9962c-1ea9-477a-af78-947988fe1020",
        state: "PRINT_INIT",
        ...
    }
    ...
}

参数

“PRINT_INIT”状态

表示插件收到网页端发送的打印请求。

字段名称类型描述
stateString状态值,此处取值"PRINT_INIT"
docIdString文档ID

“PRINT_CANCEL”状态

用户取消打印请求。例如,在printConfig调用时,弹出对话框,没有点击打印按钮,而是关闭对话框退出了,代理插件会发送此状态事件。

字段名称类型描述
stateString状态值,此处取值"PRINT_CANCEL"
docIdString文档ID

“PRINT_SUBMIT”状态

打印内容已经提交给打印机,打印任务将被放置到作业队列中。

字段名称类型描述
stateString状态值,此处取值"PRINT_SUBMIT"
docIdString文档ID

“PRINT_ERROR”状态

打印出错。

字段名称类型描述
stateString状态值,此处取值"PRINT_ERROR"
docIdString文档ID
stateTextString状态文本

“PRINT_DONE”状态

打印结束。Windows平台和Linux/macOS平台参数有区别。

windows平台:

字段名称类型描述
stateString状态值,此处取值"PRINT_DONE"
docIdString文档ID
jobIdInteger作业ID
totalPagesInteger要打印的总页数
pagesPrintedInteger已打印页数

Linux/macOS平台:

字段名称类型描述
stateString状态值,此处取值"PRINT_DONE"
docIdString文档ID
jobIdInteger作业ID

Linux/macOS平台收到此状态的事件,通常已经打印完全部页面。

示例

例1:

监听打印机状态改变事件。

document.addEventListener("nengjj_event_print", function(e){
    console.log(e);
});

输出:

{
    type: "nengjj_event_print",
    detail: {
        docId: "1de9962c-1ea9-477a-af78-947988fe1020",
        state: "PRINT_INIT",
        ...
    }
    ...
}

例2:

监听某次打印的事件。

instance.print.print({
	name:"测试",
	content: "<div style='color: red; font-weight:bold;'>Hello</div>",
}, 
function(result){
    console.log(result);

    document.addEventListener(
        "nengjj_event_print", // 事件类型总是 nengjj_event_print
        function(e) {
            console.log(e);

            if(e.detail.docId == result.docId) {
                switch(e.detail.state)
                {
                    case "PRINT_INIT":
                    // ...
                    break;

                    case "PRINT_DONE":
                    // ...
                    break;

                }
            }

        }
    );
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值