Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Puppeter google charts生成PDF格式的图表,并强制Puppeter加载google charts脚本_Javascript_Node.js_Google Visualization_Puppeteer - Fatal编程技术网

Javascript 如何使用Puppeter google charts生成PDF格式的图表,并强制Puppeter加载google charts脚本

Javascript 如何使用Puppeter google charts生成PDF格式的图表,并强制Puppeter加载google charts脚本,javascript,node.js,google-visualization,puppeteer,Javascript,Node.js,Google Visualization,Puppeteer,节点js代码: router.get('/pdf', async (req, res) => { console.log("Loading template file in memory") //Report template path try { const invoicePath = path.resolve("./valuation_report.html"); //Reading

节点js代码:

router.get('/pdf', async (req, res) => {
        console.log("Loading template file in memory") //Report template path
        try {
        const invoicePath = path.resolve("./valuation_report.html");
        //Reading the template
        const content = fs.readFileSync(invoicePath, "utf-8");
        const report_data = await pdf.pdfData(req.query.primary_key);
        const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable- 
        setuid-sandbox'] });
        //waiting to open a page in browser
        const page = await browser.newPage()
        // setting content to the page
        await page.setContent(content)
        
        await page.evaluate(report_data => {
         var view_for_historical_revenue = new google.visualization.DataView(google.visualization.arrayToDataTable(report_data.historical_revenue_barchart));
                 view_for_historical_revenue.setColumns([0, 1,
                     {
                         calc: "stringify",
                         sourceColumn: 1,
                         type: "string",
                         role: "annotation"
                     }]);
 
                 var option_for_bar_chart = { hAxis: { format: '0', title: "Years" }, colors: ['#175787'], tooltip : {trigger: 'none'} };
                 var historical_revenue_barchart = new google.visualization.ColumnChart(document.getElementById('historical-revenue-barchart'));
                 historical_revenue_barchart.draw(view_for_historical_revenue, option_for_bar_chart);
}, report_data);

// Giving a wait time so that all the resources gets loaded properly into the DOM
    await page.waitFor(20000)
        // using pdf function of puppeteer to load the pdf into the buffer
    const buffer = await page.pdf({
            format: 'A4',
            margin: {
                top: "2cm",
                right: "2cm",
                bottom: "2cm",
                left: "2cm"
            },
            displayHeaderFooter: true,
            headerTemplate: "<div/>",
            footerTemplate: "<div style=\"text-align: right;width: 297mm;font-size: 8px;\"><span style=\"margin-right: 1cm\"><span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></span></div>"
        })
        //close the headless browser
        await browser.close();
        console.log("Pdf generated successfully!")
        //sending response to the client as application/pdf
        var file_name = report_data.basic_info.company_name.replace(/[ ,.]/g, "_") + "_CVR-A_" + 
        report_data.basic_info.valuation_year + ".pdf";
        console.log(file_name);
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        res.setHeader('Content-disposition', 'attachment; filename=' + file_name);
        res.type('application/pdf')
        res.status(200).end(buffer);
    }
});
router.get('/pdf',异步(req,res)=>{
console.log(“在内存中加载模板文件”)//报告模板路径
试一试{
const invoicePath=path.resolve(“./valuation_report.html”);
//阅读模板
const content=fs.readFileSync(invoicePath,“utf-8”);
const report_data=wait pdf.pdfData(请求查询主键);
const browser=wait puppeter.launch({headless:true,args:['--no sandbox','--disable-
setuid沙盒“]});
//正在等待在浏览器中打开页面
const page=wait browser.newPage()
//设置页面内容
等待页面。设置内容(content)
等待页面。评估(报告数据=>{
历史收入的var视图=新的google.visualization.DataView(google.visualization.arrayToDataTable(report_data.historical_revenue_barchart));
查看历史收入的设置列([0,1,
{
计算:“字符串化”,
资料来源专栏:1,
键入:“字符串”,
角色:“注释”
}]);
var选项_for_bar_chart={hAxis:{format:'0',title:'Years'},颜色:['#175787'],工具提示:{trigger:'none'};
var historical_revenue_barchart=new google.visualization.ColumnChart(document.getElementById('historical-revenue-barchart');
历史收入条形图。绘制(查看历史收入,选择条形图);
},报告数据);
//提供一个等待时间,以便将所有资源正确加载到DOM中
等待页面。等待(20000)
//使用Puppeter的pdf函数将pdf加载到缓冲区
const buffer=wait page.pdf({
格式:“A4”,
保证金:{
顶部:“2厘米”,
右:“2厘米”,
底部:“2厘米”,
左:“2厘米”
},
displayHeaderFooter:对,
头模板:“,
页脚模板:“of”
})
//关闭无头浏览器
等待浏览器关闭();
log(“成功生成Pdf!”)
//以application/pdf格式向客户端发送响应
var文件名称=报告数据基本信息公司名称替换(/[,.]/g,“+”)“\U CVR-A”+
报告_data.basic_info.valuation_year+“.pdf”;
console.log(文件名);
res.header(“访问控制允许原点”、“*”);
res.header(“访问控制允许头”、“来源、X请求、内容类型、接受”);
res.setHeader('Content-disposition','attachment;filename='+文件名);
res.type('application/pdf')
恢复状态(200)。结束(缓冲区);
}
});
我试图在使用Puppeter加载html文件后生成pdf。使用此代码图表有时加载良好,有时根本不加载,需要帮助

我想要的东西,可以等待加载谷歌图表脚本,然后开始评估下一行代码