【发布时间】:2020-09-17 12:23:47
【问题描述】:
我创建了一个将 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。
请告诉我该怎么做?
【问题讨论】:
-
an HTML form in an html file你的意思是用户在浏览器中填写表单并提交吗? -
是的。填写后我可以生成它的PDF
-
不要硬编码您的类以使用
this.html(),而是将其作为 pdf() 函数的参数。然后将表单数据发送到服务器,编写html并使用现有的功能。 -
你能告诉我该怎么做吗?
-
我基本上只是做了,所以你需要更具体。你到底想知道什么?您的代码中有
const data;您需要做的就是使用提交的表单数据。
标签: javascript node.js pdf puppeteer