JavaScript:将原始文本发送到打印机 - 没有服务器请求/方法调用,能够脱机工作,纯粹是客户端

use*_*283 12 html javascript printing zpl

我对网络的深入研究为我提供了一些想法,但在我的特定用例中似乎没有一个能够正常工作.这是我有的:

1)Zebra打印机,使用ZPL作为其打印语言;

2)javascript中的一个字符串,由3个ZPL表单组成,用于打印3个标签.

我们的系统工程师已经验证过,ZPL语法都是正确的.我想要实现的是将字符串作为纯文本发送给打印机,以接受它作为打印标签的ZPL指令.到目前为止,我提出的最好的看起来像这样:

var mywindow = window.open('', 'Printing', 'width=800,height=600');
//mywindow.write("testDirectWrite"); // not working
mywindow.document.open('text/plain');
////mywindow.document.write('<html><head><title>Printing</title><meta charset="ISO-8859-1">');
///*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
////mywindow.document.write('</head><body>');
var theDiv = $(".test-printirane-po-usb");
var printContents = theDiv[0].innerText;
mywindow.document.write(printContents);
////mywindow.document.write('</body></html>');

//mywindow.document.close(); // necessary for IE >= 10
//mywindow.focus(); // necessary for IE >= 10

//mywindow.print();
//mywindow.close();
Run Code Online (Sandbox Code Playgroud)

就目前而言(测试目的),theDiv是我放置ZPL字符串的容器.基本上,我明白,最好的解决方案是打开一个新的弹出窗口,用ZPL字符串填充它并调用thePopupWindow.print(); 然后用户选择斑马打印机并点击"打印". 问题是:看起来打印机会将正在打印的内容解释为html页面(因为

<html><head></head><body>theZPLString comes here</body></html>
Run Code Online (Sandbox Code Playgroud)

标签,我看,当我检查Chrome中的弹出窗口时,并将ZPL代码打印为纯文本,而不是解释它并打印标签.我想我需要类似thePopupWindow.write()来避免写入窗口的document属性,这显然将字符串包装在html代码中.为了测试它,我使用Generic/Text Only驱动程序并将"打印"保存到.txt文件中.

在Chrome中,我收到一个空文件.

在Mozilla中,当我删除这一行时:mywindow.document.open('text/plain'); 我把ZPL作为字符,每行一个.当我添加它时,我只得到一个日期和时间,每行再一个字符.

在IE中 - 我得到这个(有或没有mywindow.document.open('text/plain');):

Page 1 o



    ^XA^PW400^LL37^





          12.4.2016
Run Code Online (Sandbox Code Playgroud)

我找到了各种解决方案,但它们涉及使用php,c#,甚至java,我不希望它是服务器端,如标题中所述.任何帮助将不胜感激.@forgivenson,谢谢你的观点.读完你的后,我看到了小'x',我可以点击删除我的评论,所以我在问题中添加了评论.我错过了一些非常重要的东西:打印机通过USB端口连接!

Lak*_*eld 12

打印到Zebra打印机时,将忽略之前^XA和之后的所有内容^XZ.zpl周围的html标签不会干扰.

您必须确保唯一的事情是将RAW文本打印到打印机.

使用Generic / Text Only用于Zebra打印机的内置Windows 驱动程序.而不是斑马司机.

  • 普通斑马驱动程序:将打印作业呈现为位图
    • 结果:zpl代码的慢速打印图像.
  • 仅文本驱动程序:将zpl代码直接发送到打印机
    • 结果:来自打印机上呈现的zpl的快速打印贴纸

关于jsfiddlegist.run的示例

function printZpl(zpl) {
  var printWindow = window.open();
  printWindow.document.open('text/plain')
  printWindow.document.write(zpl);
  printWindow.document.close();
  printWindow.focus();
  printWindow.print();
  printWindow.close();
}
Run Code Online (Sandbox Code Playgroud)

测试中

  • 边缘
  • IE浏览器
  • 火狐

不工作:


在打印机属性中选择Generic/Text Only驱动程序:

Zebra打印机 -  Generic/Text Only驱动程序