python - 在 CUPS 中出现错误“页面设置信息无效。” 从 python 脚本打印图像
问题描述
我尝试使用杯子从 Debian 10 上的 python 脚本打印图像:
import cups
def printImageLinux(image_name):
conn = cups.Connection()
printer = conn.getDefault()
conn.printFile(printer, image_name, 'suo_ticket', {})
最后,图像开始打印,但我在 Cups 用户界面 (localhost:631) 中看到打印取消和错误消息:“页面设置信息无效。”
我想我应该为图像发送打印做一些准备,但我没有找到任何有关它的信息。我可以使用 win32print 模块和代码从 Windows 打印相同的图像:
import os, sys
from win32 import win32api, win32print
from PIL import Image, ImageWin
def printImage(image_name):
# Constants for GetDeviceCaps
# HORZRES / VERTRES = printable area
HORZRES = 8
VERTRES = 10
# LOGPIXELS = dots per inch
LOGPIXELSX = 88
LOGPIXELSY = 90
# PHYSICALWIDTH/HEIGHT = total area
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
# PHYSICALOFFSETX/Y = left / top margin
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter ()
file_name = image_name
#
# You can only write a Device-independent bitmap
# directly to a Windows device context; therefore
# we need (for ease) to use the Python Imaging
# Library to manipulate the image.
#
# Create a device context from a named printer
# and assess the printable size of the paper.
#
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
#
# Open the image, rotate it if it's wider than
# it is high, and work out how much to multiply
# each pixel by to get it as big as possible on
# the page without distorting.
#
bmp = Image.open (file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90)
ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios)
#
# Start the print job, and draw the bitmap to
# the printer device at the scaled size.
#
hDC.StartDoc (file_name)
hDC.StartPage ()
dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
它工作正常。
我可以对 Debian 中的图像进行相同的操作吗?
感谢您的任何帮助!
解决方案
使用 PIL 模块解决了问题。工作代码:
import cups
from PIL import Image
def printImageLinux(image_name):
conn = cups.Connection()
printer = conn.getDefault()
image1 = Image.open(image_name)
im1 = image1.convert('RGB')
im1.save('temp_image.pdf')
conn.printFile(printer, 'temp_image.pdf', 'suo_ticket', {'fit-to-page':'True'})
推荐阅读
- excel - 有没有办法在新工作表上打印列中的最后一行?
- assembly - 怎么可能 MOVE.W 一个 24 位的值?
- vb.net - 是否可以在 vb.net 中创建全局事件?
- azure-data-factory - Azure 数据流无法连接到私有云中的托管实例
- javascript - 使用反应钩子打字时如何修复模态关闭
- mysql - 如何在 Azure 数据工厂中将 XML 多个节点映射到数据库字段
- php - PHP gettype返回双精度而不是整数?
- java - Java:无论在switch-case中是否区分大小写,都获取字符串值
- r - 在 KableExtra 中插入列分隔符
- excel - 如何通过变量(ID)将 2 个 excel 文件合并为 1 个