This User's route with Puppeteer code:
Router.get('/generate_invoice', (req, res) => {
const userData = req.session.user;
res.render("./patientpanel/invoice", { user: userData });
(async () => {
// launch a new chrome instance
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
const filePathName = path.resolve(__dirname, '../views/patientpanel/invoice.ejs');
const html = fs.readFileSync(filePathName, 'utf8')
await page.goto("http://localhost:5000/generate_invoice" + html);
await page.setContent(html, {
waitUntil: 'domcontentloaded'
});
const pdfBuffer = await page.pdf({
format: 'A4'
});
// or a .pdf file
await page.pdf({ path: "./user.pdf", format: pdfBuffer });
await browser.close()
})();
});
The PDF file generated successfully but it shows the EJS template as it is without any proper format and data which I rendered through the above router.
The EJS template code:
<tr class="information">
<td colspan="2">
<table>
<tr>
<td>
Name: <%- user.firstname %>
<%- user.lastname %><br />
Email: <%- user.email %><br />
Mobile No. : <%- user.mob %>
</td>
</tr>
</table>
</td>
</tr>