📜 ⬆️ ⬇️

Print a check document with Python and Parse

Not so long ago, I was faced with a task: printing a document of a certain format using a mobile device. On the phone, certain values ​​had to be entered, sent to the server (so that later this data could be used on the web site) and a document was printed with this data. From the very beginning, my choice fell on Google cloud print , since it is as simple as possible to use and solve such problems. But when using this option there are several drawbacks:


Therefore, I decided to write my own script for such operations.

My choice fell on the Python programming language, since it is the most flexible and suitable for these tasks. In my development, I used Ubuntu Linux. So the installation of libraries will be presented for her.

From the very beginning we need to learn how to create a PDF document. The easiest way to convert HTML markup to PDF is with the xhtml2pdf library. First, install the necessary packages:
')
sudo apt-get install python-cups python-pip python-dev 

python-cups is a library with which Python can work with your printer, python-pip will make installing xhtml2pdf easier.

Next install xhtml2pdf:

  sudo pip install xhtml2pdf 

The following code shows how everything works:

  #!/usr/bin/env python #   import cups from xhtml2pdf import pisa def main(): # ,    PDF  filename = "/home/pi/print.pdf" #     HTML  xhtml = "<h1>Test print</h1>\n" xhtml += "<h2>This is printed from within a Python application</h2>\n" xhtml += "<p style=\"color:red;\">Coloured red using css</p>\n" pdf = pisa.CreatePDF(xhtml, file(filename, "w")) if not pdf.err: #  PDF  -         pdf.dest.close() #    CUPS conn = cups.Connection() #    ,    printers = conn.getPrinters() for printer in printers: #      print printer, printers[printer]["device-uri"] #       printer_name = printers.keys()[0] conn.printFile(printer_name, filename, "Python_Status_print", {}) else: print "Unable to create pdf file" if __name__=="__main__": main() 

It's all very simple and clear. Images can be inserted with normal HTML code:

 <p><img src='/home/usr/image.jpg'/> 

But the question arises: " Where to store the data and where to get it for printing? ".

For this we will use the Parse service. This is a good free cloud for storing your data. It is convenient to use it in that it works on almost all platforms (Roughly speaking: you can add data to the cloud from an Android device, and get it on iOS, PHP, JavaScript and other platforms).

Especially for Python there is a REST API with which you can send any requests in several lines. To get them from Pars we use the code:

  connection = httplib.HTTPSConnection('api.parse.com', 443) connection.connect() connection.request('GET', '/1/classes/TestPay', '', { "X-Parse-Application-Id": "<b>yourAppId</b>", "X-Parse-REST-API-Key": "<b>yourRestApiKey</b>" }) result = json.loads(connection.getresponse().read()) 

Here yourAppId is the key to your application, and yourRestApiKey is the key to Api that you use. Read more on the site. Next, you process the result array as a JSON object and can insert it into your HTML page, which will immediately go to print.

If you have any questions, tips, or you just decide to express your opinion on the article - write in the comments. Happy New Year, everyone!

Source: https://habr.com/ru/post/246989/


All Articles