print a pdf file in python

Question:

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

Asked By: F.M

||

Answers:

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.

Answered By: lqhcpsgbl

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)

Answered By: Red
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.