Skip to content

Commit

Permalink
Fixes #166 - Add delay to png capture (#167)
Browse files Browse the repository at this point in the history
* Add delay before invoking capturePage, fixes #166

* bump version #166

* set window on the job as soon as it is created so the client can access it when the created event is emitted #166
  • Loading branch information
codecounselor authored Feb 9, 2017
1 parent 88467e8 commit 7e49079
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
38 changes: 21 additions & 17 deletions lib/exportJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const HUNG_WINDOW_THRESHOLD = process.env.ELECTRONPDF_WINDOW_LIFE_THRESHOLD || 1
const MICRONS_INCH_RATIO = 25400
/** When a ready event option is set, this is the default timeout. It is overridden by the wait option */
const MAX_READY_EVENT_WAIT = 10000
/** Amount of millis to wait before invoking WebContents.capturePage for PNG exports */
const PNG_CAPTURE_DELAY = process.env.ELECTRONPDF_PNG_CAPTURE_DELAY || 100
/** The event name for which Electron IPC is done over */
const IPC_MAIN_CHANNEL_RENDER = 'READY_TO_RENDER'
/** Prepended to events emitted during rendering */
Expand Down Expand Up @@ -139,9 +141,8 @@ class ExportJob extends EventEmitter {
*/
render () {
this.emit(`${RENDER_EVENT_PREFIX}start`)

const win = this._launchBrowserWindow()
this.window = win
this._launchBrowserWindow()
const win = this.window
registerOpenWindow(this)

// TODO: Check for different domains, this is meant to support only a single origin
Expand Down Expand Up @@ -340,6 +341,7 @@ class ExportJob extends EventEmitter {
const browserConfig = this._getBrowserConfiguration(this.args)
this.emit('window.open.start', {})
let win = new electron.BrowserWindow(browserConfig)
this.window = win
this.emit('window.open.end', {
width: browserConfig.width,
height: browserConfig.height
Expand All @@ -348,8 +350,6 @@ class ExportJob extends EventEmitter {
win.on('closed', function () {
win = null
})

return win
}

/**
Expand Down Expand Up @@ -643,18 +643,22 @@ class ExportJob extends EventEmitter {
}

_captureImage (window, outputFile, done) {
window.webContents.capturePage(image => {
// http://electron.atom.io/docs/api/native-image/#imagetopng
const pngBuffer = image.toPNG()
if (this.options.inMemory) {
this._emitResourceEvents(undefined, pngBuffer, done)
} else {
const target = path.resolve(outputFile)
fs.writeFile(target, pngBuffer, function (err) {
this._emitResourceEvents(err, target, done)
}.bind(this))
}
})
// We need a short timeout here or the image may not be captured fully
// https://github.com/electron/electron/issues/6622
setTimeout(() => {
window.webContents.capturePage(image => {
// http://electron.atom.io/docs/api/native-image/#imagetopng
const pngBuffer = image.toPNG()
if (this.options.inMemory) {
this._emitResourceEvents(undefined, pngBuffer, done)
} else {
const target = path.resolve(outputFile)
fs.writeFile(target, pngBuffer, function (err) {
this._emitResourceEvents(err, target, done)
}.bind(this))
}
})
}, PNG_CAPTURE_DELAY)
}

_capturePDF (args, window, done, outputFile) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-pdf",
"version": "1.1.5",
"version": "1.1.6",
"description": "A command line tool to generate PDF from URL, HTML or Markdown files",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 7e49079

Please sign in to comment.