【问题标题】:Electron: how to print all html content in pdfElectron:如何以pdf格式打印所有html内容
【发布时间】:2019-07-30 15:13:00
【问题描述】:

我在第一个项目中使用 Electron,我必须从我的 html 打印一个 PDF 文件。

现在我的代码中有什么:

    ipc.on('print-to-pdf', event => {
      const pdfPath = path.join(os.tmpdir(), 'temp_pdf.pdf')

      const win = BrowserWindow.fromWebContents(event.sender)

      win.webContents.printToPDF({marginsType: 1, pageSize:'Tabloid'}, (error, data) => {
        if (error) return console.log(error.message)

        fs.writeFile(pdfPath, data, err => {
          if (err) return console.log(err.message)
          shell.openExternal('file://' + pdfPath)
        })
      })
    })

but when my app create .pdf file, the content is sliced in the second page.
Like that:
[Error pdf][1]
maybe it depends on the setting of the page size:

    win.webContents.printToPDF({marginsType: 1, pageSize:'Tabloid'}, (error, data) => {
        if (error) return console.log(error.message)

我该如何解决这个问题?

[1]:https://i.stack.imgur.com/dJ6tT.png

【问题讨论】:

  • “内容在第二页被切片”是什么意思?你能提供一个你看到的样本吗?
  • 我修改了我的问题添加了一张图片,你可以看到第二页没有显示所有其余的内容

标签: javascript node.js pdf electron


【解决方案1】:

我解决了在我的 css 中添加这个问题:

@media print {
  body {
    overflow: visible !important;
  }
}

现在 pdf 已完成,显示所有页面

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2017-08-29
    • 1970-01-01
    • 2015-01-30
    • 2012-06-04
    相关资源
    最近更新 更多