Printing Receipt from Django Web Application

前端 未结 3 1759
南笙
南笙 2021-01-31 23:03

I am developing a web-based POS. Unfortunately, POS must print through a thermal receipt printer (TM-T88IV, Epson). The web application is based on Django. Is there any idea on

3条回答
  •  生来不讨喜
    2021-01-31 23:25

    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:

    Print this page
    

    (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

提交回复
热议问题