使用 win32api 在内存对象中打印
Printing in memory object using win32api
正在尝试打印使用 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)
您的尝试根本不可能。您不能将字节数组作为参数传递给 print
shell 动词。您需要将字节数组保存到临时文件中,然后将该临时文件名的名称作为参数传递给 print
.
正在尝试打印使用 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)
您的尝试根本不可能。您不能将字节数组作为参数传递给 print
shell 动词。您需要将字节数组保存到临时文件中,然后将该临时文件名的名称作为参数传递给 print
.