PrintToPDF 在无头 Chrome 60 中不起作用

Dra*_*kes 2 pdf-generation google-chrome node.js google-chrome-devtools centos7

我正在尝试通过无头 Chrome 进行 PDF 打印。这是我正在处理的错误:

(节点:6761)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:未实现PrintToPDF

Node.js 包:

html-pdf-chrome
Run Code Online (Sandbox Code Playgroud)

依赖项:

? "chrome-remote-interface": "^0.23.1"  (v0.23.2 installed)   
? Chrome 59  (v60 beta installed)
Run Code Online (Sandbox Code Playgroud)

驱动脚本:

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'));
Run Code Online (Sandbox Code Playgroud)

Chrome 60 以无头模式安装和运行:

> google-chrome --version
Google Chrome 60.0.3112.24 beta
Run Code Online (Sandbox Code Playgroud)

我已经追踪到调用的代码部分,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);
Run Code Online (Sandbox Code Playgroud)

我能够执行其他宣传的功能,例如Page.captureScreenshot没有失败。

如何达到Page.printToPDF广告宣传的效果?

Dra*_*kes 5

以下是我在 PHP 中创建 headless 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);
Run Code Online (Sandbox Code Playgroud)

CLI 参数--disable-extensions是罪魁祸首。生成 PDF 所需的 Chrome 的 PDF 查看器是此参数禁用的扩展程序之一。谜团已揭开。