I have an API written with node.js
hosted on heroku
and my frontend app is written in Vue.js
, it is on hostinger
. I would like to know if is it possible to generate a PDF file with puppeteer and send it immediately to frontend client without saving it to the disk first? If yes, could you give me some example on how to do it?
Currently my function is like that:
exports.gerarPDFAvaliacao = async (dadosAvaliacao) => {
try {
const compile = async (fileName, data) => {
const filePath = path.join(process.cwd(), 'src/templates/client/operation/', `${fileName}.hbs`);
const html = await fs.readFile(filePath, 'utf-8');
return await hbs.compile(html)(data);
}
const browser = await puppeteer.launch();
const page = await browser.newPage();
let content = await compile('avaliations', dadosAvaliacao);
await page.goto(`data:text/html,${content}`, { waitUntil: 'networkidle0' });
await page.emulateMedia('screen');
await page.pdf({
path: 'src/dist/pdf/' + dadosAvaliacao.arquivo + '.pdf',
format: 'A4',
printBackground: true
})
await browser.close();
return dadosAvaliacao.arquivo + '.pdf';
} catch (error) {
console.log('Errors => ', error);
}
};