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

2024-06-28 19:52:35 发布

您现在位置: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创建或打印的帮助或建议。 如果你找到一个更好的方法来打印图像,请帮助我! 提前谢谢!

Tags: theimageimportimgnewpdfpage打印机