Search code examples
pythonlinuxprintingreportlab

Python printing bytes string directly to printer on Linux


A temp file can print by using

subprocess.run(["lp", "-d", "<printer>", tmp])

But is it possible to print a bytes string representing a ReportLab generated pdf?

buffer = BytesIO()
c = canvas.Canvas(buffer)
c.drawString(100, 100, "Hello World")
c.showPage()
c.save()

pdf = buffer.getvalue()
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(pdf)
buffer.close()

This code starts a print job but with a size of 0k and a status of Held.


Solution

  • I guess you are missing a lpr.communicate() call, compare with: Understanding Popen.communicate