Puppeteer Throwing Invalid Parameters Error

六眼飞鱼酱① 提交于 2019-12-25 00:12:19

问题


I am trying to convert an HTML content to PDF, but I am getting Invalid parameters for scale and preferCSSPageSize when passed using variables.

Error Message:

Error: Protocol error (Page.printToPDF): Invalid parameters scale: double value expected; preferCSSPageSize: boolean value expected at Promise (/home/santhosh-4759/Downloads/node-v8.11.3-linux-x64/bin/node_modules/puppeteer/lib/Connection.js:202:56) at new Promise ()

Command Used:

./node puppeteerpdf.js test.pdf 1 false '' '' false false 210mm 297mm 0 0 0 0 false 'htmlcontent'

This doesn't work:

await page.pdf({path: output, scale: vcale, displayHeaderFooter: displayHeaderFooter, headerTemplate: headerTemplate, footerTemplate: footerTemplate, printBackground: printBackground, landscape: landscape, width: width, height: height, margin: marginParams, preferCSSPageSize: preferCSSPageSize});

This is working:

await page.pdf({path: output, scale: 1, displayHeaderFooter: displayHeaderFooter, headerTemplate: headerTemplate, footerTemplate: footerTemplate, printBackground: printBackground, landscape: landscape, width: width, height: height, margin: marginParams, preferCSSPageSize: false});

回答1:


It appears that the variables you are passing to page.pdf() as the values for scale and preferCSSPageSize are not of the correct type.

Your working example shows scale to be equal to 1 and preferCSSPageSize to be equal to false.

These are the default values of these parameters, so you could safely exclude them from the options passed to page.pdf().

If these values can change, and you are obtaining the values of these attributes from the command line, make sure to convert them from a string to the correct type before sending them to page.pdf():

vcale             = parseInt( vcale );
preferCSSPageSize = preferCSSPageSize === 'true';


来源:https://stackoverflow.com/questions/51910292/puppeteer-throwing-invalid-parameters-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!