F.M
F.M

Reputation: 511

print a pdf file in python

I want to print a pdf file in python. My code is as below:

def printing_reports():
 import os
fp = open("/path-to-file/path.txt",'r')
for line in fp:
    os.system('lp -d HPLaserJ %s' % (str(line)))

I am on Fedora 20. path.txt is a file that contain path to the pdf file like '/home/user/a.pdf' When I run the code it says no such file or directory.

Thanks

Upvotes: 3

Views: 4711

Answers (2)

Red
Red

Reputation: 339

Old question, but as I needed a an answer of how to print a pdf file from python, I found this answer more profound:

import cups
conn = cups.Connection()
printers = conn.getPrinters()
printer_name = printers.keys()[0]
ppd_options = {}
cups_job_id = conn.printFile(printer_name,'/path/to/a.pdf',"Title printjob", ppd_connection_options) 

It uses the pycups module, which needs CUPS >= 1.7 installed on your system (according to their GitHub page)

The ppd_options dictionary might just be empty. (PPD - Postscript Printer Driver)

Upvotes: 3

lqhcpsgbl
lqhcpsgbl

Reputation: 3782

Try this code may help:

import os

def printing_reports():
  fp = open("/path-to-file/path.txt",'r')
  for line in fp:
      os.system('lp -d HPLaserJ {0}'.format(line.strip()))

 printing_reports()

Make sure the file in every line exists.

Upvotes: 2

Related Questions