【问题标题】:convert html form into pdf in node js?在节点js中将html表单转换为pdf?
【发布时间】: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


【解决方案1】:

你试过jsPDF吗?根据他们的网站,jsPDF 是用于生成 PDF 的领先 HTML5 客户端解决方案。非常适合用于活动门票、报告和证书。

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多