PDF 打印到贴纸/标签热敏打印机 [PYTHON]

我需要帮助来确定如何正确地将 pdf 打印到贴纸/标签打印机。尽管每行带有多个标签的打印机无法按预期工作,但每页一张贴纸的打印机目前工作正常。这是我生成 PDF 文件的代码:


from PIL import Image
import PyPDF2

img = Image.open('somelabelimage.png', 'r')  # load the image

# calculate pixels from mm
def gpm(mm):
    return round((72 * mm) / 25.4)

img = img.resize(
    (
       gpm(15)*10,gpm(5)*10 # 15mm X 5mm
    ),
    Image.LANCZOS
)

img_w, img_h = img.size

# create empty new PIL.Image
pivot = Image.new('RGB', ( 4*(img_w+ (gpm(4)*10))-(gpm(4)*10), img_h), (255, 255, 255))
x = 0

# here we paste each label-image onto the new surface
for y in range(4):
    pivot.paste(img,(x,0))
    x += img_w + (gpm(4)*10) # 4 mm offset increased by factor 10 

# save the image as pdf
pivot.save("new.pdf",resolution=200)

# code below resizes the pdf file to fit the mm specs
pr = PyPDF2.PdfFileReader(open('new.pdf','rb'))
pw = PyPDF2.PdfFileWriter()

def mti(mm):
    return mm / 25.4

page = pr.getPage(0)
page.scaleTo(mti(15)*72*4,mti(5)*72)

pw.addPage(page)
pw.write(open("resized.pdf",'wb'))

所以这就是我实际打印的方式:

import subprocess

# pdftopriter.exe is an exe that lets me print to printers. Here is the link to theri page : # http://www.columbia.edu/~em36/pdftoprinter.html
command = [
  "session/PDFtoPrinter.exe", "storage/storage_export.pdf",
  self.printer if self.printer is not None else "None"
]

subprocess.Popen(command)

单标签结果: enter image description here

所以他们工作得很好,但是这是另一台打印机的结果: enter image description here

如您所见,此打印作业已关闭。但我不知道如何修复/调整它。

我感谢有关 pdf 创建或打印的各种帮助或建议。如果您找到了更好的打印图像的方法,请帮助我!提前致谢!

stack overflow PDF print to Sticker/Label Thermal Printer [PYTHON]
原文答案