JavaScript:将原始文本发送到打印机 - 没有服务器请求/方法调用,能够脱机工作,纯粹是客户端 [英] JavaScript: Send raw text to printer - no server requests/method calls, able to work offline, purely clientside
问题描述
我对网络的深入研究为我提供了一些想法,但在我的特定用例中似乎没有一个能够正常工作。这就是我所拥有的:
1)Zebra打印机,它使用ZPL作为其打印语言;
2 )javascript中的一个字符串,由3个ZPL表单组成,用于打印3个标签。
我们的系统工程师已经验证过,ZPL语法都是正确的。
我想要实现的是将字符串作为纯文本发送给打印机,以接受它作为ZPL指令来打印标签。
到目前为止,我想出的最好看起来像这样:
var mywindow = window.open('' ,'Printing','width = 800,height = 600');
//mywindow.write(\"testDirectWrite); //不工作
mywindow.document.open('text / plain');
////mywindow.document.write('<html><head><title>Printing</title><meta charset =ISO-8859-1>');
/// *可选样式表* / //mywindow.document.write('<link rel =stylesheethref =main.csstype =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(); // IE的必要> = 10
//mywindow.focus(); // IE的必要> = 10
//mywindow.print();
//mywindow.close();
目前(测试目的),theDiv是我放置ZPL字符串的容器。基本上,我明白,最好的解决方案是打开一个新的弹出窗口,用ZPL字符串填充它并调用thePopupWindow.print();然后用户选择斑马打印机并点击打印。
问题:似乎打印机会将正在打印的内容解释为html页面(因为
< html>< head>< / head>< body> theZPLString来到这里< / body>< / html>
标签,我看到,例如,当我检查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
我发现了各种解决方案,但它们涉及使用php,c#,甚至java,我不希望它是服务器端,如标题中所述。
任何帮助将不胜感激。
@forgivenson,谢谢你的观点。读完你的后,我看到了小'x',我可以点击删除我的评论,所以我在问题中添加了评论。
我错过了一些非常重要的事情:打印机通过USB端口连接!
打印到Zebra打印机时, ^ XA
之前和 ^ XZ
之后的所有内容都将被忽略。 zpl周围的html标签不会干扰。
您唯一必须确保将RAW文本打印到打印机。
使用Zebra打印机中的内置版 Generic / Text Only
驱动程序。而不是斑马司机。
- 普通斑马驱动程序:将打印作业呈现为位图
- result:zpl代码的慢速打印图像。
- 纯文本驱动程序:将zpl代码直接发送到打印机
- 结果:来自打印机上呈现的zpl的快速打印贴纸
有关
My thorough research on the web provided me with a couple of ideas, but none of them seem to work correctly in my particular use case. Here is what I have:
1) Zebra printer, which uses ZPL as its printing language;
2) A string in javascript which consists of 3 ZPL forms for printing 3 labels.
Our system engineer has verified already, that the ZPL syntax is all correct. What I am trying to achieve is to send the string as plain text for the printer to accept it as ZPL instructions to print labels. The best I have come up with so far looks like this:
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();
For now (testing purposes), theDiv is the container where i place the ZPL string. Basically, I understood, that the best solution is to open a new popup window, fill it with the ZPL string and call thePopupWindow.print(); The user then selects the zebra printer and hits 'Print'. The problem: it seems like the printer interprets what's being printed as an html page (because of the
<html><head></head><body>theZPLString comes here</body></html>
tags, that i see, when I inspect the popup in Chrome, for example) and prints the ZPL code as plain text, rather than interpret it and print a label. I suppose I need something like thePopupWindow.write() to avoid writing to the document property of the window, which obviously wraps the string in html code. In order to test it, I use the Generic/Text Only driver and save what's "printed" into a .txt file.
In Chrome I get an empty file.
In Mozilla, when I remove this line: mywindow.document.open('text/plain'); I get the ZPL as characters, one per line. When I add it, I get only a date and time, again one char per line.
In IE - I get this (with or without mywindow.document.open('text/plain');):
Page 1 o
^XA^PW400^LL37^
12.4.2016
I found various solutions, but they involve using php, c#, even java and I don't want it to be server-side, as mentioned in the title. Any help will be appreciated. @forgivenson, thanks for the point. After reading yours, I saw the little 'x', that I can click to delete my comment, so I added the comment within the question. I missed something very important: the printer is connected via USB port!
When printing to a Zebra printer, everything before ^XA
and after ^XZ
is ignored. The html tags around the zpl don't interfere.
The only thing you must ensure is that you print RAW text to the printer.
Use the build in windows Generic / Text Only
driver for your Zebra printer. Instead of the zebra driver.
- The normal zebra driver: renders the print job to a bitmap
- result: a slow printed image of your zpl code.
- The text only driver: sends the zpl code straight to the printer
- result: a fast printed sticker, from zpl rendered on the printer
Example on jsfiddle or on gist.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();
}
Tested in
- Edge
- Internet Explorer
- Firefox
Not working in:
- Chrome
- Chrome creates an image before sending it to the printer driver (https://productforums.google.com/forum/#!topic/chrome/GYf6iI42Ug4)
Select the Generic / Text Only driver in your printer properties:
这篇关于JavaScript:将原始文本发送到打印机 - 没有服务器请求/方法调用,能够脱机工作,纯粹是客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!