PrintToPDF不能在无头Chrome 60中工作
我正尝试通过无头版Chrome进行PDF打印。 这是我正在处理的错误:
(node:6761)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1): 错误:PrintToPDF未实现
Node.js包:
html-pdf-chrome
依赖关系:
✔ "chrome-remote-interface": "^0.23.1" (v0.23.2 installed) ✔ Chrome 59 (v60 beta installed)
驱动脚本:
const htmlPdf = require('html-pdf-chrome'); const html = '<p>Hello, world!</p>'; const options = { port: 9222, // port Chrome is listening on }; htmlPdf.create(html, options).then((pdf) => pdf.toFile('test.pdf'));
Chrome 60已安装并以无头模式运行:
> google-chrome --version Google Chrome 60.0.3112.24 beta
我跟踪了调用Page.printToPDF
的代码段,这是错误发生的地方:
const CDP = require("chrome-remote-interface"); ... const { Page } = client; ... // https://chromedevtools.github.io/debugger-protocol-viewer/tot/Page/#method-printToPDF const pdf = yield Page.printToPDF(options.printOptions);
我能够执行其他广告function,如Page.captureScreenshot
没有失败。
如何让Page.printToPDF
执行广告?
以下是我如何在PHP中创build无头Chrome命令,以便在apache
用户下运行Chrome:
private $headlessChromeCmd = [ '$(which google-chrome)', '--headless', '--disable-gpu', '--hide-scrollbars', '--remote-debugging-port=%u', '--no-first-run', '--safebrowsing-disable-auto-update', '--disable-background-networking', //'--disable-extensions', <-- This was the problem '--disable-translate', '--disable-sync' ]; ... // Launch Chrome in a non-blocking background process $cmd = sprintf(implode(' ', $this->headlessChromeCmd), $port) . ' < /dev/null >"'. $this->chromeStdoutFile . '" 2>&1 & echo $!;'; // For brevity without capturing stdout, stderr nor response codes shell_exec($cmd);
CLI param --disable-extensions
是罪魁祸首。 用于生成PDF的Chrome浏览器PDF查看器是由此参数禁用的扩展名之一。 谜团已揭开。