Chrome CLI 保存为 PDF 横向

Chrome CLI save to PDF landscape

我正在使用 Google Chrome 的 CLI 将 HTML 保存为 PDF。我需要 tp 以横向模式保存 PDF,找不到命令。我正在使用这个命令:

google-chrome --headless --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf-no-header --Landscape=true  --print-to-pdf="MyPdf.pdf" "MyHtml.html"

您不能在命令行 上将 Landcape 用于 chrome 或类似 Edge 的衍生产品。

这是有意为之的,因为到目前为止,开发团队一直拒绝为此类用途扩展类似的命令行设置,他们的建议是您应该使用 API 方法。

您可以通过使用@page html 或注入 javascript 来解决这个问题,否则打印机需要定义页面输出。

2017

We don't intend to provide all the flexibility that DevTools provides through command line options: There are various technical reasons why command line options cannot provide the same flexibility.

It appears that Headless Chrome does parse @page as well to some extent, but behaves differently than the desktop version: If you specify @page {size}, headless seems to change the dimensions of the page box (essentially, the print area), and not the sheet, which always remains US Letter sized. However, it does rotate the sheet if you specify {size: landscape}.

The print options are exposed via the DevTools API only (and not via command line flags), see comments #51 and prior.

2021/2?最好的选择可能是像 https://github.com/dataverity/chromehtml2pdf#readme

这样的推动因素