1
3

More than 3 years have passed since last update.

Electron 7.0.x で webContents.printToPDF() が promisification されたけどドキュメントの例が直っていない件

Last updated at Posted at 2019-11-02

Electron 7.0.x がリリースされたということで早速、アップデートしてみました。いっそう、promisification が進んでいるようです。webContents.printToPDF() も promisification されて、Promise を Return するようになりました。

https://electronjs.org/docs/api/web-contents#contentsprinttopdfoptions に webContents.printToPDF() の説明がありますが、メソッドの説明そのものは更新されていますが、コーディング例が更新されていません。以下のように修正すべきかと思います。

const { BrowserWindow } = require('electron')
const fs = require('fs')

let win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL('http://github.com')

win.webContents.on('did-finish-load', () => {
  // Use default printing options
  win.webContents.printToPDF({
  }).then(data => {
    fs.writeFile('/tmp/print.pdf', data, (error) => {
      if (error) throw error
      console.log('Write PDF successfully.')
    })
  }).catch(error => {
    throw error
  })
})

以下、差分。

--- a/docs/api/web-contents.md
+++ b/docs/api/web-contents.md
@@ -1329,12 +1329,14 @@ win.loadURL('http://github.com')

 win.webContents.on('did-finish-load', () => {
   // Use default printing options
-  win.webContents.printToPDF({}, (error, data) => {
-    if (error) throw error
+  win.webContents.printToPDF({
+  }).then(data => {
     fs.writeFile('/tmp/print.pdf', data, (error) => {
       if (error) throw error
       console.log('Write PDF successfully.')
     })
+  }).catch(error => {
+    throw error
   })
 })

Pull Request しようかと思いましたが、テンプレートが結構、面倒そうだったので尻すぼみしてます。

既に Electron の Contributor な方に取り込んでもらえたら、いいなあ。 < 他力本願 :sweat_smile:

蛇足

dialog.showSaveDialog() も 7.0.x で Promise を返すようになっています。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3