Javascript 在(斑马)打印机上直接从浏览器打印贴纸/条形码标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14641869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:28:09  来源:igfitidea点击:

Print sticker/barcode label directly from browser on a (zebra) printer

phpjavascriptprinting

提问by user2031649

We have to following situation, for one of our clients we are looking for a solution. He has build an PHP script that generates a label with some info and barcodes on it, so the workflow is as discribed : User put in some data ( some is data is pulled from the database, some not )

我们必须遵循以下情况,对于我们的一位客户,我们正在寻找解决方案。他已经构建了一个 PHP 脚本,该脚本生成一个带有一些信息和条形码的标签,因此工作流程如下所述:用户输入一些数据(有些是从数据库中提取的数据,有些不是)

When the user clicks on print label, a windows opens and the label is generated. So now the code ( i guess javascript can do the job ) must do the work. The client wants that when the user clicks on ok ( or print label ) the label will be printed and the window will close without any interuption from a user.

当用户点击打印标签时,会打开一个窗口并生成标签。所以现在代码(我猜 javascript 可以完成这项工作)必须完成这项工作。客户端希望当用户点击确定(或打印标签)时,标签将被打印,窗口将关闭而不会受到用户的任何干扰。

Possible solution : http://dymodevelopers.wordpress.com/2010/06/02/dymo-label-framework-javascript-library-samples-print-a-label/

可能的解决方案:http: //dymodevelopers.wordpress.com/2010/06/02/dymo-label-framework-javascript-library-samples-print-a-label/

So in fact :

所以实际上:

  1. The script must select the right printer ( not the default one )
  2. Script must select the right format ( landscape/portrait and sizes of the labels )
  3. Must be printed without user interuption.
  4. Window must close automatic after printing the label.
  1. 脚本必须选择正确的打印机(不是默认的)
  2. 脚本必须选择正确的格式(横向/纵向和标签大小)
  3. 必须在没有用户中断的情况下打印。
  4. 打印标签后窗口必须自动关闭。

Thanks for the help ;)

谢谢您的帮助 ;)

回答by Dave

I do exactly this but due to the limitations of web based stuff cannot control the windows print interface without either firing a java or an activex based sub app I have had to have the web browsers set to print to the dymo printers.

我正是这样做的,但由于基于 Web 的东西的限制,无法在不触发 java 或基于 Activex 的子应用程序的情况下控制 Windows 打印界面,我不得不将 Web 浏览器设置为打印到 dymo 打印机。

Now I enforce the usage of firefox which means I have a custom install bundle of firefox that sets the margins to zero, turns off headers and footers, selects the right printer and paper and landscape etc (I only did a custom bundle because I had 100+ pc's to configure if its only a few its a 60 second change job to do it manually). The advantage of enforcing firefox is that FF allows you to select and set as the default printer a different printer to the system default. So in my case ff is set for dymo's the system printer is set as the kyocera's so nothing gets lost/set etc outside of firefox.

现在我强制使用 firefox,这意味着我有一个自定义安装包,将边距设置为零,关闭页眉和页脚,选择正确的打印机和纸张和风景等(我只做了一个自定义包,因为我有 100 + pc 来配置它是否只有几个它的 60 秒更改工作来手动完成)。强制执行 firefox 的优点是 FF 允许您选择和设置与系统默认不同的打印机作为默认打印机。因此,在我的情况下,ff 设置为 dymo,系统打印机设置为 kyocera,因此在 Firefox 之外不会丢失/设置等。

As for generating the barcode I just create a basecode as a jpg on the fly display the jpg in a new popup window and use a little javascript to automatically popup the print box so all the user has to do is click ok. Its impossible to do this without user input!

至于生成条码,我只是创建一个基本代码作为 jpg 在新的弹出窗口中显示 jpg 并使用一点 javascript 自动弹出打印框,因此用户所要做的就是单击“确定”。没有用户输入就不可能做到这一点!

Unfortunately there's no easy way so close the window as there's no return event trap from the system print spooler/drivers back into the web browsers (they're sandboxed from each other for obvious reasons!) so the best you can do is implement a timed close after xxx seconds.

不幸的是,没有简单的方法可以关闭窗口,因为没有从系统打印后台处理程序/驱动程序返回到 Web 浏览器的返回事件陷阱(出于显而易见的原因,它们彼此隔离!) xxx 秒后关闭。

Short of it though is there's no easy way to do this with no user input and without making a few compromises. Now if cash and time is of no issue to you I do have a proper way of doing it fully automated etc but its a big job

简而言之,没有用户输入且不做出一些妥协,没有简单的方法可以做到这一点。现在,如果现金和时间对你来说都不是问题,我确实有一个完全自动化的正确方法,但这是一项艰巨的工作

回答by Artfldgr

We just use an FTP (used to use windows, now use whats built into cold fusion, but any would do) and you send the zebra printer the definition of your barcode to the machine that way... anything that can open up such a channel and send the code to the printer will work.. so if you want to do it with javascript see how fireftp does it... then use that... or you can send info in ZPLII coding... that tells it how to print.

我们只使用 FTP(过去使用 Windows,现在使用冷聚变中内置的东西,但任何都可以),然后您将条码的定义发送到 zebra 打印机以这种方式...任何可以打开这样的通道并将代码发送到打印机将起作用..所以如果你想用javascript来做,看看fireftp是怎么做的......然后使用它......或者你可以在ZPLII编码中发送信息......告诉它如何打印。

回答by Key

Since the link you posted talks about driver requirements, I assume you have a fair amount of control over the machine's software? If that's the case and you can use Firefox, then an add-on called JS Print Setup may work.

由于您发布的链接讨论了驱动程序要求,我假设您对机器的软件有相当多的控制权?如果是这种情况并且您可以使用 Firefox,那么名为 JS Print Setup 的附加组件可能会起作用。

It basically gives webpages the ability to print directly to a printer on the user's computer without a print dialog.

它基本上使网页能够直接打印到用户计算机上的打印机,而无需打印对话框。

I assume it would print a label fine as long as the right size is provided, but I haven't tested that. I did test it when a printer wasn't connected and it did send it to that printer's queue, so definitely tells the system to print the file.

我认为只要提供了正确的尺寸,它就会很好地打印标签,但我还没有测试过。我确实在未连接打印机时对其进行了测试,并且确实将其发送到该打印机的队列中,因此肯定会告诉系统打印文件。

Here is a code sample from the documentation: (It appears that it lets you set margins and everything as well.)

这是文档中的代码示例:(它似乎可以让您设置边距和所有内容。)

<script>
// set portrait orientation
jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);

// set top margins in millimeters
jsPrintSetup.setOption('marginTop', 15);
jsPrintSetup.setOption('marginBottom', 15);
jsPrintSetup.setOption('marginLeft', 20);
jsPrintSetup.setOption('marginRight', 10);

// set page header
jsPrintSetup.setOption('headerStrLeft', 'My custom header');
jsPrintSetup.setOption('headerStrCenter', '');
jsPrintSetup.setOption('headerStrRight', '&PT');

// set empty page footer
jsPrintSetup.setOption('footerStrLeft', '');
jsPrintSetup.setOption('footerStrCenter', '');
jsPrintSetup.setOption('footerStrRight', '');

// clears user preferences always silent print value
// to enable using 'printSilent' option
jsPrintSetup.clearSilentPrint();

// Suppress print dialog (for this context only)
jsPrintSetup.setOption('printSilent', 1);

// Do Print 
// When print is submitted it is executed asynchronous and
// script flow continues after print independently of completetion of print process! 
jsPrintSetup.print();

// next commands
</script>

The add-on can be found here on Mozilla's add-on site:

可以在 Mozilla 的附加站点上找到该附加组件:

https://addons.mozilla.org/en-US/firefox/addon/js-print-setup/

https://addons.mozilla.org/en-US/firefox/addon/js-print-setup/

回答by ice.cube

Never used it myself but Seagull Scientific' BarTenderseems like a good solution for direct printing of barcodes. It includes embedded drivers for working with databases such as Oracle, MS SQL, and .NET platform and offers Commander for printing from PHP web apps. Tutorial for printing through PHP apps here http://cases.azoft.com/bartender-print-server/

我自己从未使用过它,但 Seagull Scientific 的BarTender似乎是直接打印条形码的好解决方案。它包括用于处理 Oracle、MS SQL 和 .NET 平台等数据库的嵌入式驱动程序,并提供用于从 PHP Web 应用程序打印的 Commander。在此处通过 PHP 应用程序打印的教程http://cases.azoft.com/bartender-print-server/