javascript - 在节点js中将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 是用于生成 PDF 的领先 HTML5 客户端解决方案。非常适合活动门票、报告和证书。
推荐阅读
- python - 如何使用 Python 将 DF 列中的单词替换为另一个单词 - 不包括子字符串
- java - 通过就地修改数组来复制数组中的零
- javascript - 如何检测 React Web 应用程序中的空闲时间?
- azure - 使用 Kusto 查询语言的具有单个数字的图表
- join - 修复连接具有相同变量的两个数据集
- android-asynctask - Android Asynctask 已弃用
- c# - 使用 .NET Core 3.1 搜索网络上的所有 SQL Server 实例
- variables - 特定于 grafana 中的面板的过滤器
- sql - Oracle SQL:根据另一列获取不同列值的计数
- c++ - 尝试使用 ostream<< 运算符对象时与 {fmt} 6.2.1-3 + clang 10.0.0 的链接问题