Chrome无头打印为pdf的其他选项 [英] additional options in Chrome headless print-to-pdf

查看:927
本文介绍了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中使用这些选项的功能.还可以从硒中调用Devtools吗?

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

此外,我如何在Dev工具中调用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:\PATH\TO\CHROME\EXECUTABLE\FILE", "--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:\user\fileName.pdf --no-margins C:\Projects\index.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天全站免登陆