web打印固定格式单据时不容易确定打印位置,使用打印插件可以很好的控制打印位置及纸张大小(打印位置、字体大小、线条宽度均以毫米为单位,便于使用直尺测量设置打印),便于使用控制针式打印机进纸,打印有印刷格式的单据如打印快递单等只需测量出打印位置距离左边和顶边的距离即可,具体实现如下:
1、创建Activex对象
var printer = new ActiveXObject("TPrint.mPrinter");
2、选择打印机
//选择打印机
function selp(){
alert(printer.SelectPrinter());
}
3、自定义打印
通过自定义纸张大小可以打印快递单、凭证、银行票据等各种尺寸单据及票据
function doprint(){
//开始单据打印
if(printer.PrintInit(
80, //纸张宽度 毫米
100, //纸张高度 毫米
1 //打印方向 1纵向 2横向
)){
//printer.NewPage();//新增一页
printer.FontSize=10; //毫米
printer.FontName="宋体";
printer.FontBold=true;
printer.ForeColor="#00ff00" //字体颜色设置
printer.PrintText("打印测试",0,0)
printer.FontSize=5;
printer.PrintText("123.00",0,10)
// printer.PrintText("无预览自定义纸张大小打印单据,打印文字内容,区域左上角坐标x,y,区域右下角坐标x1,y1,打印位置9宫格(1左上角,2上中,以此类推.1",0,20)
printer.PrintText("打印在x坐标0,y坐标30毫米位置",0,30)
printer.PrintLine(0,40,80,40,0.5,0,"#000");
printer.PrintLine(0,40,0,80,0.5,0,"#000");
printer.PrintLine(80,80,0,80,0.5,0,"#000");
printer.PrintLine(80,40,80,80,0.5,0,"#000");
printer.PrintText("无预览自定义纸张大小打印单据,打印文字内容,区域左",0,40,80,80,1) //打印文字内容,区域左上角坐标x,y,区域右下角坐标x1,y1,打印位置9宫格(1左上角,2上中,以此类推)
// printer.PrintText("区域靠右上打印",0,40,80,80,3)
printer.PrintLine(20,15,60,15,1,0,"#000");//起点x,y,终点x1,y1,线宽(毫米),线条样式,线条颜色
//printer.PrintPicFromBase64("",10,10,60,80);//打印图片的base64编码,x坐标(毫米),y坐标,宽度,高度
printer.PrintCommit(); //提交到打印机打印,完成打印
}
}
通过js直接控制打印机进行精确打印,实现在web系统中的各种单据的打印
打印插件及示例下载地址 http://download.csdn.net/detail/nxiaoping/5349478
以上打印效果预览