javascript 从 Django Web 应用程序打印收据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9257580/
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
Printing Receipt from Django Web Application
提问by Haikal Nashuha
I am developing a web-based POS. Unfortunately, POS must print thru a thermal receipt printer (TM-T88IV, Epson). The web application is based on Django. Is there any idea on how the system could automatically print a receipt whenever a user click a control in the web application?
我正在开发一个基于网络的 POS。不幸的是,POS 必须通过热敏收据打印机(TM-T88IV,Epson)进行打印。该 Web 应用程序基于 Django。有没有关于系统如何在用户单击 Web 应用程序中的控件时自动打印收据的想法?
I was thinking of creating another services in python for that purposes, but that would defeat the purpose of having a web application, where all you need is browser, without any extra installation.
我正在考虑为此目的在 python 中创建另一个服务,但这会破坏拥有一个 web 应用程序的目的,你需要的只是浏览器,而无需任何额外的安装。
The printer is connected to the client by the way, and the printing should be "silently" triggered, which means that there is no need for a human intervention. Once the transaction is finalized, the printing should starts.
打印机顺便连接到客户端,打印应该是“静默”触发的,也就是说不需要人为干预。交易完成后,应开始打印。
Any suggestion is welcomed!
欢迎任何建议!
采纳答案by Francesco Frassinelli
I see two ways to accomplish it:
我看到两种方法来实现它:
First method - Configure your browser
第一种方法 - 配置您的浏览器
Notes
笔记
Good solution if you have one printer for every client (because you can use the default printer only). Keep in mind that you can remove your print server (useful for very resource limited devices) making a script that the browser should automatically execute for open your file. You can use something like this:
如果您为每个客户端配备一台打印机,这是一个很好的解决方案(因为您只能使用默认打印机)。请记住,您可以删除打印服务器(对于资源非常有限的设备很有用),制作一个脚本,浏览器应自动执行以打开您的文件。你可以使用这样的东西:
#!/bin/bash
printer="/dev/usb/lp0"
encoding_needed=true #false
if $encoding_needed; then
iconv -c -t 437 > $printer
else
cat > $printer
fi
Firefox
火狐
- Manual setup:
- Open about:config
- Create a new boolean value called
print.always_print_silent
and set it toTrue
- Create a new boolean value called
print.show_print_progress
and set it toFalse
- Use an extension, like: https://addons.mozilla.org/en-us/firefox/addon/attendprint/
- 手动设置:
- 打开关于:配置
- 创建一个名为的新布尔值
print.always_print_silent
并将其设置为True
- 创建一个名为的新布尔值
print.show_print_progress
并将其设置为False
- 使用扩展名,例如:https: //addons.mozilla.org/en-us/firefox/addon/attendprint/
Keep in mind that there are other extensions for making kiosks, for example:
请记住,还有其他用于制作自助服务终端的扩展,例如:
- https://addons.mozilla.org/en-us/firefox/addon/r-kiosk/
- https://addons.mozilla.org/en-us/firefox/addon/mkiosk/
- https://addons.mozilla.org/en-us/firefox/addon/r-kiosk/
- https://addons.mozilla.org/en-us/firefox/addon/mkiosk/
Chrome
铬合金
You can start it with those options: --kiosk --kiosk-printing
您可以使用以下选项启动它: --kiosk --kiosk-printing
Internet Explorer
IE浏览器
For kiosk mode see: http://support.microsoft.com/kb/154780
有关信息亭模式,请参阅:http: //support.microsoft.com/kb/154780
Second method - Server handles every printer
第二种方法 - 服务器处理每台打印机
Notes
笔记
Good solution if:
好的解决方案,如果:
- You have more clients than printers (few money or faulty printers)
- More printers than clients (different printers or paper colors for different needs)
- Clients that can't print directly (PDA/smartphones)
- You want to know the printer status
- 您的客户比打印机多(钱很少或打印机有问题)
- 打印机多于客户(不同打印机或纸张颜色满足不同需求)
- 无法直接打印的客户端(PDA/智能手机)
- 您想知道打印机状态
How to do
怎么做
- Connect printers (to the clients and/or to the server)
- Share printers connected to clients over the network
- Manage every printer from your Django server
- 连接打印机(到客户端和/或到服务器)
- 通过网络共享连接到客户端的打印机
- 从 Django 服务器管理每台打印机
回答by Quentin Pradet
Two options here: print an html page or provide a PDF file.
这里有两个选项:打印 html 页面或提供 PDF 文件。
Note: it was not clear initially that prints should be automatic, which means the answer is not directly useful to OP.
注意:最初不清楚打印应该是自动的,这意味着答案对 OP 没有直接用处。
HTML + "Print Me"
HTML +“打印我”
Show the receipt as an html page, then create a media="print"
CSS stylesheet which the browser will use when printing the receipt. There's a lot to say about CSS print style sheets, but what's important is that you should remove all navigation elements and images that are going to be expensive to print.
将收据显示为 html 页面,然后创建一个media="print"
CSS 样式表,浏览器将在打印收据时使用该样式表。有很多要说CSS打印样式表,但最重要的是,你应该删除所有的导航元素和图像将要打印昂贵。
When you do this, the user will simply have to print the page himself. You can also add a "Print Me" button which is going to show your user a printer dialog. This is done via JavaScript:
执行此操作时,用户只需自己打印页面。您还可以添加一个“打印我”按钮,该按钮将向您的用户显示打印机对话框。这是通过 JavaScript 完成的:
<a href="javascript:window.print()">Print this page</a>
(This is a bit obstrusive for your clients who don't have JS, check this tutorial about JS printingfor a better way.)
(这对于没有 JS 的客户来说有点麻烦,请查看有关 JS 打印的本教程以获得更好的方法。)
Generate a PDF in Django, and show it to the user. He will be free to print it or save it on his computer later. Most web sites do this since it's far easier to control the layout of a PDF file, and it will be easier to make it look like a real receipt.
在 Django 中生成一个 PDF,并展示给用户。他以后可以自由打印或保存在他的电脑上。大多数网站都这样做,因为控制 PDF 文件的布局要容易得多,而且更容易让它看起来像一个真正的收据。
- XSL-FOcan help you do this (it translates an XML to a PDF with a "stylesheet").
- A more Pythonic way seems to be explained in the Django docs
- The above pages lists alternatives such as xhtml2pdf (Pisa) which seems to be used a lot on StackOverflow
- XSL-FO可以帮助您做到这一点(它将 XML 转换为带有“样式表”的 PDF)。
- Django 文档中似乎解释了一种更 Pythonic 的方式
- 上面的页面列出了替代品,例如 xhtml2pdf (Pisa),它似乎在 StackOverflow 上被大量使用
回答by Tres
If using raw/esc/p try jzebra on google code.
如果使用 raw/esc/p,请在 google 代码上尝试 jzebra。