使用win32api在内存对象中打印

时间:2018-05-07 11:28:35

标签: python windows winapi reportlab

尝试打印使用reportlab生成的PDF但ShellExecute()正在抛出TypeError: Objects of type 'bytes' can not be converted to Unicode.

我已尝试过pdf.decode('utf-8'),但我正在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)

1 个答案:

答案 0 :(得分:4)

你所尝试的是根本不可能的。您不能将字节数组作为参数传递给print shell动词。您需要将字节数组保存到临时文件,然后将该临时文件名的名称作为参数传递给print