Printing Receipt from Django Web Application

*爱你&永不变心* 提交于 2019-12-31 10:48:36

问题


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?

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.

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!


回答1:


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 $1 > $printer
else
    cat $1 > $printer
fi

Firefox

  • Manual setup:
    1. Open about:config
    2. Create a new boolean value called print.always_print_silent and set it to True
    3. Create a new boolean value called print.show_print_progress and set it to False
  • Use an extension, like: 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/

Chrome

You can start it with those options: --kiosk --kiosk-printing

Internet Explorer

For kiosk mode see: http://support.microsoft.com/kb/154780

Second method - Server handles every printer

Notes

Good solution if:

  1. You have more clients than printers (few money or faulty printers)
  2. More printers than clients (different printers or paper colors for different needs)
  3. Clients that can't print directly (PDA/smartphones)
  4. You want to know the printer status

How to do

  1. Connect printers (to the clients and/or to the server)
  2. Share printers connected to clients over the network
  3. Manage every printer from your Django server



回答2:


Two options here: print an html page or provide a PDF file.

Note: it was not clear initially that prints should be automatic, which means the answer is not directly useful to OP.

HTML + "Print Me"

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.

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:

<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 printing for a better way.)

PDF

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.

  • XSL-FO can 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



回答3:


If using raw/esc/p try jzebra on google code.



来源:https://stackoverflow.com/questions/9257580/printing-receipt-from-django-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!