最近做项目, 需要使用到类似顺丰标签的那种标签打印, 需实现单打和批量打印,由于打印的功能是给到国外的同事使用,纸张大小为4*6, 有点特殊,国外打印机和国内的也不一样, 所以调试的时候也是花费了挺多时间, 最后采用的是容大的标签打印机和lodop 的结合使用, 这里重点记录下打印的实现方式。
使用步骤
1、 lodop.js 下载驱动和插件, 传送门:http://www.c-lodop.com/download.html
2、 将下载的插件放在静态文件夹下,其中注意的需要设置下加密秘钥, 可以用于删除水印 (官方代码未写, 需要购买正版获取秘钥), 下面直接贴代码
var CreatedOKLodop7766=null;
//====判断是否需要安装CLodop云打印服务器:====
export function needCLodop(){
try{
var ua=navigator.userAgent;
if (ua.match(/Windows\sPhone/i) !=null) return true;
if (ua.match(/iPhone|iPod/i) != null) return true;
if (ua.match(/Android/i) != null) return true;
if (ua.match(/Edge\D?\d+/i) != null) return true;
var verTrident=ua.match(/Trident\D?\d+/i);
var verIE=ua.match(/MSIE\D?\d+/i);
var verOPR=ua.match(/OPR\D?\d+/i);
var verFF=ua.match(/Firefox\D?\d+/i);
var x64=ua.match(/x64/i);
if ((verTrident==null)&&(verIE==null)&&(x64!==null))
return true; else
if ( verFF !== null) {
verFF = verFF[0].match(/\d+/);
if ((verFF[0]>= 42)||(x64!==null)) return true;
} else
if ( verOPR !== null) {
verOPR = verOPR[0].match(/\d+/);
if ( verOPR[0] >= 32 ) return true;
} else
if ((verTrident==null)&&(verIE==null)) {
var verChrome=ua.match(/Chrome\D?\d+/i);
if ( verChrome !== null ) {
verChrome = verChrome[0].match(/\d+/);
if (verChrome[0]>=42) return true;
};
};
return false;
} catch(err) {
return true;};
};
//====页面引用CLodop云打印必须的JS文件:====
if (needCLodop()) {
var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
var oscript = document.createElement("script");
oscript.src ="http://localhost:8000/CLodopfuncs.js?priority=1";
head.insertBefore( oscript,head.firstChild );
//引用双端口(8000和18000)避免其中某个被占用:
oscript = document.createElement("script");
oscript.src ="http://localhost:18000/CLodopfuncs.js?priority=0";
head.insertBefore( oscript,head.firstChild );
};
//====获取LODOP对象的主过程:====
export function getLodop(oOBJECT,oEMBED