I'm trying to generate a PDF using Puppeteer and insert there a Chart.js graph (but the whole PDF should be a text based - not a webpage screenshot). When I open the template I can see graph loaded, but in PDF there is just a blank space instead. I'm generating it like this:
async function generatePdf() {
let data = { value: "myValue" };
getTemplateHtml()
.then(async (res) => {
// Now we have the html code of our template in res object
// you can check by logging it on console
// console.log(res)
console.log("Compiing the template with handlebars");
const template = hb.compile(res, { strict: true });
// we have compile our code with handlebars
const result = template(data);
// We can use this to add dyamic data to our handlebas template at run time from database or API as per need. you can read the official doc to learn more https://handlebarsjs.com/
const html = result;
// we are using headless mode
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
// We set the page content as the generated html by handlebars
await page.setContent(html);
// We use pdf function to generate the pdf in the same folder as this file.
await page.pdf({
path: "output2.pdf",
format: "A4",
displayHeaderFooter: true,
margin: {
top: "2.5cm",
right: "2.5cm",
bottom: "2.5cm",
left: "2.5cm",
},
headerTemplate: "<div></div>",
footerTemplate:
'<div style="width: 100%; font-size: 11px !important; overflow: auto; margin-left: 2.5cm; margin-right: 2.5cm; color: ghostwhite;"><span>TEST</span><a href="http://test.com" style="float: right; color: #1E90FF; text-decoration: none;">www.test.com</a></div>',
});
await browser.close();
console.log("PDF Generated");
})
.catch((err) => {
console.error(err);
});
}
generatePdf();
Thanks