将此 CSS 添加到您创建为 PDF 的页面中,以删除 Chrome Headless 实现的页眉和页脚。
CSS:
@media print {
@page { margin: 0; }
body { margin: 1.6cm; }
}
你应该像下面这样格式化你的命令来创建 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 可执行文件的文件夹并运行以下命令来测试此功能:
chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/