rkn704
Posts: 49
Joined: Sun Dec 30, 2012 4:25 pm

Python program to print text file via CUPS ?

Wed Apr 12, 2017 8:19 am

I am looking for a python program example to open a text file and print it to a physical printer rather than the screen.

I have installed CUPS and can print from Leafpad via CUPS to my USB connected printer.

How do I print to the named printer (HP_Laserjet) from a python program?
Do I need to set this printer as the default printer?

Any help much appreciated. Thanks in advance

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python program to print text file via CUPS ?

Wed Apr 12, 2017 10:14 am

I've never tried it, but it looks like there's a python library for CUPS: https://pypi.python.org/pypi/pycups

There's an example of how to use it here: http://www.techradar.com/how-to/computi ... -1312717/2

Alternatively, if you've got a command line that works for you then you could just call this with the subprocess module (plenty of examples of using this on the forum).
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

rkn704
Posts: 49
Joined: Sun Dec 30, 2012 4:25 pm

Re: Python program to print text file via CUPS ?

Fri Apr 14, 2017 10:42 am

elParaguayo - Thank you for your suggestions and the links.

I have made some progress, first i tried the subprocess approach using the lp command. The -d option sets the destination. The printer name is the one shown in CUPS using the 192.168.0.xx:631 type address in a browser.

import os
os.system("lp -d HP_LaserJet_P2055dn -o cpi=12 -o lpi=8 bolprint.txt")

but this causes the printed file to be double spaced. I can change the size of the characters cpi and the number of lines per inch lpi but cannot get it to print without the blank lines between the text. If I print the same file from Leafpad it prints it without the double space issue.

Next i tried the pycups method.

import cups
conn = cups.Connection()
printers = conn.getPrinters()
for printer in printers:
print printer, printers[printer]["device-uri"]
printer_name=printers.keys()[0]
fileName = "bolprint.txt"
conn.printFile(printer_name, fileName, " ", {})


With this method it appears you can add options inside the last argument {} of the command. But I have been unable to find a list of the options or how to use them. At the moment the text size is too big and doesn't fit on the page.

Does anyone know how to set CPI and LPI values using the pycups method?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python program to print text file via CUPS ?

Fri Apr 14, 2017 12:57 pm

I assume that PyCups uses the same options that you would use if using the "-o" flag via the command line so try changing that last line to:

Code: Select all

conn.printFile(printer_name, fileName, " ", {"cpi": "12", "lpi": "8"})
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
scruss
Posts: 4940
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: Python program to print text file via CUPS ?

Fri Apr 14, 2017 3:36 pm

CUPS does a lot to a text file before sending it to a printer. Internally, it renders your text to a PDF then it renders to your specific printer. Are the lines in your file too long, causing wrapping? Are there spaces/tabs at the end causing wrapping too?

For debugging printer output, installing the cups-pdf package means you can test output without wasting paper. Any jobs sent to the PDF printer end up in the /home/pi/PDF folder.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

rkn704
Posts: 49
Joined: Sun Dec 30, 2012 4:25 pm

Re: Python program to print text file via CUPS ?

Sun Apr 30, 2017 7:19 pm

Thank you guys, both of you were right.

I used CUPS printing options in the pycups commands as suggested and it seems to be OK.

conn.printFile(printer_name, fileName, " ", { "page-left":"30", "cpi":"12", })


The issue with the double spacing of lines was down to the line length which was easily sorted by stripping the unnecessary blank spaces from the the end of the line.


In my project I am using cron to run my python program when the Raspberry Pi initially boots. The cron script is:

@reboot /usr/bin/python2.7 /home/pi/myprog.py

The first thing that was supposed to happen was that a page would be printed to show the Raspberry Pi was online.

What I have found is that it is necessary to delay the printing via pycups and CUPS to ensure the CUPS server is up and running. A thirty second delay at the top of the program gives everything time to settle down before trying to print the first page.

Return to “Python”