使用 CUPS 打印 html 文件
有没有办法明确告诉 CUPS 服务器您正在发送的文件是 text/html
从而覆盖 mime.types 查找?
Is there a way to explicitly tell the CUPS server that the file you are sending is text/html
thus overriding the mime.types lookup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有。
使用此命令行:
更新(响应评论)
我为OP的问题提供了准确的答案。
但是,这(单独)并不能保证文件将成功打印。为此,CUPS 需要一个可以处理 MIME 类型
text/html
输入的过滤器。CUPS 本身不提供这样的过滤器。然而,将您自己的过滤器插入 CUPS 过滤系统很容易,并且一些 Linux 发行版提供了这样的过滤器,能够使用 HTML 文件并将其转换为可打印格式。
您可以在您的系统上检查这种情况下会发生什么。 cupsfilter 命令是一个帮助实用程序,用于运行可用/已安装的 CUPS 过滤器,而无需通过 CUPS 守护程序进行实际打印:
现在,在没有准备好 HTML 消耗过滤器的系统上,您将收到此响应:
在不同的系统上(例如在 Mac 上),您会看到以下内容:
您甚至可以强制输入和输出 MIME 类型,以查看当要求使用支持该特定输出 MIME 类型的打印机打印此文件时,CUPS 会自动运行哪些过滤器(
-i
设置输入 MIME 类型,-m
输出):这里首先将 HTML 转换为使用
xhtmltopdf
生成 PDF,然后使用cgpdftops
将生成的 PDF 转换为 PostScript。如果您跳过
--list-filters
参数,该命令实际上甚至会继续通过主动运行(不仅仅是列出)两个过滤器来进行转换,并将结果发送到<标准输出>
。您可以基于 Shell 脚本编写自己的 CUPS 过滤器。您唯一需要的其他成分是命令行工具,例如
htmldoc
或wkhtmltopdf
,它能够处理 HTML 输入并生成某种格式,而这些格式又可以被进一步的 CUPS 过滤链。请注意,一些(尤其是大量 JavaScript)HTML 文件无法通过简单的命令行工具成功处理为打印就绪格式。
如果您需要更多详细信息,请提出另一个问题......
Yes, there is.
Use this commandline:
Update (in response to comments)
I provided an exact answer to the OP's question.
However, this (alone) does not guarantee that the file will be successfully printed. To achieve that, CUPS needs a filter which can process the input of MIME type
text/html
.Such a filter is not provided by CUPS itself. However, it is easy to plug your own filter into the CUPS filtering system, and some Linux distributions ship such a filter capable to consume HTML files and convert them to a printable format.
You can check what happens in such a situation on your system. The
cupsfilter
command is a helper utility to run available/installed CUPS filters without the need to do actual printing through the CUPS daemon:Now on a system with no HTML consuming filter ready, you'd get this response:
On a different system (like on a Mac), you'll see this:
You can even force input and output MIME types to see which filters CUPS would run automatically when asked to print this file an a printer supporting that particular output MIME type (
-i
sets the input MIME type,-m
the output):Here it would first convert HTML to PDF using
xhtmltopdf
, then transform the resulting PDF to PostScript usingcgpdftops
.If you skip the
--list-filters
parameter, the command would actually even go ahead and do the conversion by actively running (not just listing) the two filters and emit the result to<stdout>
.You could write your own CUPS filter based on a Shell script. The only other ingredient you need is a command line tool, such as
htmldoc
orwkhtmltopdf,
which is able to process HTML input and produce some format that in turn could be consumed by the CUPS filtering chain further down the road.Be aware, that some (especially JavaScript-heavy) HTML files cannot be successfully processed by simple command line tools into print-ready formats.
If you need more details about this, just ask another question...