我有一个 Ubuntu VM,其中安装、配置并运行了 cups 服务器。我可以从 Web 界面毫无问题地打印到每台打印机。在这个 VM 中有一个 docker 容器,里面有一个 python flask 应用程序,您可以在其中发布 pdf 文件,这些文件会保存在一个临时路径中。然后它使用 pycups 将这些文件打印到名为“DOCUMENTOS”的类中。每次都会失败,提示:
cups.IPPError: (1030, 'The printer or class does not exist.')
我尝试直接调用打印机(HP_LaserJet_Pro_M118dw),但结果是一样的。
在docker容器中我设置了一个绑定:
/tmp -> /tmp
导致文件名传递给 pycups printFile 函数:
/tmp/print-daemon/1696440073890032106.pdf
Python 列出打印机:
printers = self.conn.getPrinters()
printer_names = list(printers.keys())
print(f'printers {printer_names}', flush=True)
日志中的结果:
printers ['DOCUMENTOS', 'HP_LaserJet_Pro_M118dw']
相关python代码:
cups.setServer(cups_server)
cups.setPort(631)
cups.setEncryption(3)
cups.setUser("user")
cups.setPasswordCB(self.__passwordCB)
self.conn = cups.Connection(host=cups_server, port=631)
...
self.conn.printFile(printer='DOCUMENTOS', filename=filename, title='', options={})
lpstat-v:
dispositivo para DOCUMENTOS: ///dev/null
dispositivo para HP_LaserJet_Pro_M118dw: socket://192.168.5.XX
/etc/cups/cupsd.conf
LogLevel debug2
PageLogFormat
MaxLogSize 0
ErrorPolicy retry-job
Port 631
Listen /run/cups/cups.sock
Browsing On
BrowseLocalProtocols dnssd
DefaultAuthType Basic
WebInterface Yes
...
/var/log/cups/错误日志
D [04/Oct/2023:17:33:13 +0000] Get-Printer-Attributes file:///dev/null
d [04/Oct/2023:17:33:13 +0000] get_printer_attrs(0x5631dc6e8290[6], file:///dev/null)
D [04/Oct/2023:17:33:13 +0000] Get-Printer-Attributes client-error-not-found: The printer or class does not exist.
D [04/Oct/2023:17:33:13 +0000] [Client 6] Returning IPP client-error-not-found for Get-Printer-Attributes (file:///dev/null) from 172.18.0.4.