css - 如何在 headless Chrome 中更改纸张大小 --print-to-pdf

标签 css google-chrome pdf printing google-chrome-headless

我正在使用 headless Chrome 将 html 文档导出为 pdf

google-chrome --headless --disable-gpu --print-to-pdf='output_path' 'url'

如何更改生成的 pdf 中的纸张大小?

我可以控制 Chrome 参数和 html。

我总是收到美国信件。

没有记录在案的命令行选项。

我试过设置 CSS:@page {size: A4;}。在 headless 模式下没有效果,但是当我在正常模式下按 Ctrl+P 时有效(为 Save as pdf 选择纸张大小的选项消失,导出的 pdf 具有 A4 页面大小)。

我已经在 Ubuntu 16.04 上的 Chrome 版本 59、60 和 61 上试过了。

最佳答案

页面大小可以以英寸/毫米为单位设置。我还没有测试过像素大小。这是一组 CSS 规则,对我有用:

@page {
  margin: 0;
  padding: 0;
  size: 5in 6.5in;
}

我的具体情况是渲染 svg-to-pdf,而不是 html;对于 svg,您可能还需要添加 widthheight <svg> 的属性标签:

<svg width="5in" height="6.5in" ...>

就是这样!输出 PDF 不会有边距,并且会保留所需的大小 - 在我的例子中为 5"x6.5"。

关于css - 如何在 headless Chrome 中更改纸张大小 --print-to-pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970113/

相关文章:

jquery - 滚动到指定部分时如何在 href 上添加事件类?

google-chrome - 奇怪的轮廓出现在我的 Windows 10 上的 Chrome 83/IE 中

html - IE 中的边框看起来比 Chrome 中的边框粗

css - 浏览器加载不在 CSS 文件中的宽度/高度值

.net - iTextSharp PDF 和 Firefox 出现内容损坏错误 : does not work with Firefox 10. xx

html - 悬停后向内容属性添加动画

html - Bootstrap : how to truly center an image in a column?

javascript - 摆脱联系人表单中的灰色字体和文本框边框

java - iText : PDFPTable - Not able to insert data in specific column

pdf - PDF字体对象的/Widths数组是冗余信息吗?