我有一个用node.js
编写的API,托管在heroku
上,我的前端应用程序是用Vue.js
编写的,它是在hostinger
上.我想知道是否有可能生成一个带有Puppeter的PDF文件,并立即发送到前端客户端,而不必先将其保存到磁盘?如果是的话,你能给我举一些如何做的例子吗?
目前我的功能是:
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);
}
};