在nodejs中将html表单转换为pdf



我创建了一个将HTML文件转换为pdf的类。以下是代码

class Invoice {  
async html() {
try {
const data = {
your: 'data'
}
console.log("Current directory:", __dirname); 
const templatePath = Path.resolve('index.html')
console.log(templatePath)
const content = await ReadFile(templatePath, 'utf8')
// compile and render the template with handlebars
const template = Handlebars.compile(content)
return template(data)
} catch (error) {
throw new Error('Cannot create invoice HTML template.')
}
}
async pdf() {
const html = await this.html()
console.log(html)
const browser = await Puppeteer.launch()
const page = await browser.newPage()
await page.setContent(html)
return page.pdf()
}
}

但这读取了磁盘上的HTML文件。但是我想在HTML文件中创建HTML表单的pdf。

请告诉我该怎么做?

您尝试过jsPDF吗?根据他们的网站,jsPDF是一个领先的HTML5客户端生成PDF的解决方案。完美的活动门票,报告&证书。

最新更新