Chrome 无头打印到 pdf 中的附加选项 [英] additional options in Chrome headless print-to-pdf

查看:77
本文介绍了Chrome 无头打印到 pdf 中的附加选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再需要一次帮助.我正在尝试使用 chrome 的无头功能将页面打印为 pdf.但是,pdf 中存在页眉和页脚.我发现这个选项已经在 Devtools 中实现了.

I need help one more time. I am trying to print a page to pdf using headless feature of the chrome. However, header and footer is present in the pdf. I found that this option as been implemented in Devtools.

https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF

但是,我找不到如何在 CLI 中使用这些选项.也可以从 selenium 调用 Devtools 吗?

However, i can't find how can i use these options in CLI. Also is it possible to invoke the Devtools from selenium?

此外,我如何在开发工具中调用 Page.PrintToPDF.我试图在控制台中运行命令.它显示页面未定义.

Additionally how can i invoke Page.PrintToPDF in Dev tools. I tried to run the command in Console. It is showing Page is undefined.

推荐答案

将此 CSS 添加到您创建的页面并转换为 PDF 以移除 Chrome Headless 实现的页眉和页脚.

Add this CSS to the page your creating into a PDF to remove Chrome Headless's implemented Header and Footer.

CSS:

@media print {
  @page { margin: 0; }
  body { margin: 1.6cm; }
}

您应该像下面这样格式化命令以创建 PDF:

You should format your command like below to create the PDF:

"C:PATHTOCHROMEEXECUTABLEFILE", "--headless","--disable-gpu","--print-to-pdf=" + directory path to where you want the file to go followed by the desired file name/fileName.pdf,"--no-margins", "the path to the file you want turned into a pdf"

示例 1:

C:chrome-win/chrome --headless --disable-gpu --print-to-pdf=C:userfileName.pdf --no-margins C:Projectsindex.html

示例 2:

您还可以通过在命令行中导航到包含 Chrome 可执行文件的文件夹并运行以下命令来测试此功能:

You can also test this functionality by navigating in your command line to the folder containing Chrome executable file, and running this command:

chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/

这篇关于Chrome 无头打印到 pdf 中的附加选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆