首页 > 解决方案 > Electron:如何以pdf格式打印所有html内容

问题描述

我在我的第一个项目中使用 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

标签: javascriptnode.jspdfelectron

解决方案


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

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

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


推荐阅读