bdoubleu
bdoubleu

Reputation: 6107

Printing in memory object using win32api

Attempting to print a PDF generated with reportlab but ShellExecute() is throwing TypeError: Objects of type 'bytes' can not be converted to Unicode.

I've tried pdf.decode('utf-8') but am getting UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 10: invalid start byte

from io import BytesIO
import win32api
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter


x = 100
y = 100
buffer = BytesIO()
p = canvas.Canvas(buffer, pagesize=letter)
p.drawString(x, y, "Hello World")
p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()

win32api.ShellExecute(0, "print", pdf, None, ".", 0)

Upvotes: 0

Views: 425

Answers (1)

David Heffernan
David Heffernan

Reputation: 613382

What you are attempting is simply not possible. You can't pass a byte array as the argument to the print shell verb. You need to save the byte array to a temporary file, and then pass the name of that temporary file name as the argument to print.

Upvotes: 5

Related Questions