如何解决nodejs转换html-pdf格式问题
我正在尝试下载pdf文件,我首先将其从ejs模板转换为pdf。我遇到格式问题。 pdf格式受到干扰。我的代码在下面。
app.get('/billtemp',(req,res)=>{
const public = __dirname + "/public";
const file = public + "/pdfs/mypdf.pdf";
const image = public + "/images/trotdrive.png";
const Amount = { price: 3000 };
const logo = `data:image/png;base64,${Buffer.from(
fs.readFileSync(image).toString("base64")
)}`;
res.render('templatebill.ejs',{ logo:logo,Amount: Amount });
});
app.get('/download/:filename',async(req,res) => {
const file= __dirname + "/public/pdfs/mypdf.pdf";
try {
const url="http://localhost:4030/billtemp"
const browser=await puppeteer.launch({headless:true});
// navigate to the website
const page=await browser.newPage();
await page.goto(url,{waitUntil:"networkidle2"})
// await page.setContent('<h1>Hello</h1>');
// await page.emulateMedia('screen');
await page.pdf({
path: file,format: "A4",margin: {
right: 25,left: 25},printBackground:true
});
console.log('done')
await browser.close();
return res.header('Content-type','octet/stream').header(
"Content-Disposition",`attachment; filename="${
req.params.filename || Date.now() + ".pdf"
}"`
).sendFile(file)
}catch(e){
console.log('our error',e)
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。