使用 CUPS 打印 html 文件

发布于 2024-12-19 04:38:13 字数 77 浏览 3 评论 0原文

有没有办法明确告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-12-26 04:38:13

是的,有。

使用此命令行:

lp -d printername -o document-format=text/html file.html

更新(响应评论)

我为OP的问题提供了准确的答案。

但是,这(单独)并不能保证文件将成功打印。为此,CUPS 需要一个可以处理 MIME 类型 text/html 输入的过滤器

CUPS 本身不提供这样的过滤器。然而,将您自己的过滤器插入 CUPS 过滤系统很容易,并且一些 Linux 发行版提供了这样的过滤器,能够使用 HTML 文件并将其转换为可打印格式。

您可以在您的系统上检查这种情况下会发生什么。 cupsfilter 命令是一个帮助实用程序,用于运行可用/已安装的 CUPS 过滤器,而无需通过 CUPS 守护程序进行实际打印:

touch 1.html
/usr/sbin/cupsfilter --list-filters 1.html

现在,在没有准备好 HTML 消耗过滤器的系统上,您将收到此响应:

cupsfilter: No filter to convert from text/html to application/pdf.

在不同的系统上(例如在 Mac 上),您会看到以下内容:

xhtmltopdf

您甚至可以强制输入和输出 MIME 类型,以查看当要求使用支持该特定输出 MIME 类型的打印机打印此文件时,CUPS 会自动运行哪些过滤器(-i 设置输入 MIME 类型,-m 输出):

/usr/sbin/cupsfilter                  \
            -i text/html              \
            -m application/postscript \
            --list-filters            \
              1.html
  xhtmltopdf
  cgpdftops

这里首先将 HTML 转换为使用 xhtmltopdf 生成 PDF,然后使用 cgpdftops 将生成的 PDF 转换为 PostScript。

如果您跳过 --list-filters 参数,该命令实际上甚至会继续通过主动运行(不仅仅是列出)两个过滤器来进行转换,并将结果发送到 <标准输出>

您可以基于 Shell 脚本编写自己的 CUPS 过滤器。您唯一需要的其他成分是命令行工具,例如 htmldocwkhtmltopdf,它能够处理 HTML 输入并生成某种格式,而这些格式又可以被进一步的 CUPS 过滤链。

请注意,一些(尤其是大量 JavaScript)HTML 文件无法通过简单的命令行工具成功处理为打印就绪格式。

如果您需要更多详细信息,请提出另一个问题......

Yes, there is.

Use this commandline:

lp -d printername -o document-format=text/html file.html

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:

touch 1.html
/usr/sbin/cupsfilter --list-filters 1.html

Now on a system with no HTML consuming filter ready, you'd get this response:

cupsfilter: No filter to convert from text/html to application/pdf.

On a different system (like on a Mac), you'll see this:

xhtmltopdf

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):

/usr/sbin/cupsfilter                  \
            -i text/html              \
            -m application/postscript \
            --list-filters            \
              1.html
  xhtmltopdf
  cgpdftops

Here it would first convert HTML to PDF using xhtmltopdf, then transform the resulting PDF to PostScript using cgpdftops.

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 or wkhtmltopdf, 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文