JavaScript:将原始文本发送到打印机 - 没有服务器请求/方法调用,能够脱机工作,纯客户端 [英] JavaScript: Send raw text to printer - no server requests/method calls, able to work offline, purely clientside

查看:9
本文介绍了JavaScript:将原始文本发送到打印机 - 没有服务器请求/方法调用,能够脱机工作,纯客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>打印</title><meta charset="ISO-8859-1">');///*可选样式表*///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();//IE >= 10 所必需的//mywindow.focus();//IE >= 10 所必需的//mywindow.print();//mywindow.close();

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

ZPLString 来了

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

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

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

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

第1页^XA^PW400^LL37^12.4.2016

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

解决方案

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

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

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

  • 普通斑马驱动程序:将打印作业渲染为位图
    • 结果:您的 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:


Select the Generic / Text Only driver in your printer properties:

这篇关于JavaScript:将原始文本发送到打印机 - 没有服务器请求/方法调用,能够脱机工作,纯客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆