Skip to content

Commit

Permalink
fixes #159 (#160)
Browse files Browse the repository at this point in the history
* fixes #159

* bumped ava version (new features) and updated the test for the landscape fix #159
  • Loading branch information
codecounselor authored Feb 7, 2017
1 parent 4083437 commit a7ae6f7
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 73 deletions.
159 changes: 96 additions & 63 deletions lib/exportJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class ExportJob extends EventEmitter {
if (_.startsWith(this.args.pageSize, '{')) {
this.args.pageSize = JSON.parse(this.args.pageSize)
}

this.originalArgs = _.cloneDeep(this.args)
}

// ***************************************************************************
Expand All @@ -79,31 +81,28 @@ class ExportJob extends EventEmitter {
render (window) {
const eventPrefix = 'job.render.'
this.emit(`${eventPrefix}start`)
const args = this.args

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

// TODO: Check for different domains, this is meant to support only a single origin
const firstUrl = this.input[0]
this._setSessionCookies(args.cookies, firstUrl, win.webContents.session.cookies)
this._setSessionCookies(this.args.cookies, firstUrl, win.webContents.session.cookies)

const windowEvents = []
// The same listeners can be used for each resource
this.passThroughEvents(win, eventPrefix)
this._passThroughEvents(win, eventPrefix)

this.input.forEach((uriPath, i) => {
windowEvents.push((pageDone) => {
// Reset the generated flag for each input URL because this same job/window
// can be reused in this scenario
this.generated = false
this._initializeWindowForResource()
const targetFile = this._getTargetFile(i)
const generateFunction = this._generateOutput.bind(this, win, targetFile, args, pageDone)
const waitFunction = this._waitForPage.bind(this, win, generateFunction, args.outputWait)
const generateFunction = this._generateOutput.bind(this, win, targetFile, pageDone)
const waitFunction = this._waitForPage.bind(this, win, generateFunction, this.args.outputWait)
// Need a new listener to generate the resource
win.webContents.removeAllListeners('did-finish-load')
win.webContents.on('did-finish-load', waitFunction)
this._loadURL(win, uriPath, args)
this._loadURL(win, uriPath)
})
})

Expand All @@ -124,51 +123,6 @@ class ExportJob extends EventEmitter {
})
}

/**
* Listen for events and emit them from this job so clients can
* do logging or event handling
*
* @param win
* @param renderPrefix
*/
passThroughEvents (win, renderPrefix) {
win.webContents.on('did-fail-load', (r) => {
// http://electron.atom.io/docs/api/web-contents/#event-did-fail-load
this.emit(`${renderPrefix}did-fail-load`, {results: r})
})
win.webContents.on('did-start-loading', (r) => {
this.emit(`${renderPrefix}did-start-loading`, {results: r})
})
win.webContents.on('did-finish-load', (r) => {
this.emit(`${renderPrefix}did-finish-load`, {results: r})
})
win.webContents.on('dom-ready', (r) => {
this.emit(`${renderPrefix}dom-ready`, {results: r})
})
win.webContents.on('did-get-response-details',
function (event,
status,
newURL,
originalURL,
httpResponseCode,
requestMethod,
referrer,
headers,
resourceType) {
this.emit(`${renderPrefix}did-get-response-details`, {
event: event,
status: status,
newURL: newURL,
originalURL: originalURL,
httpResponseCode: httpResponseCode,
requestMethod: requestMethod,
referrer: referrer,
headers: headers,
resourceType: resourceType
})
})
}

/**
* If the html page requested emits a CustomEvent
* (https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent)
Expand Down Expand Up @@ -225,7 +179,68 @@ class ExportJob extends EventEmitter {
// ************************* Private Functions *******************************
// ***************************************************************************

// Events
/**
* Listen for events and emit them from this job so clients can
* do logging or event handling
*
* @param win
* @param renderPrefix
*/
_passThroughEvents (win, renderPrefix) {
win.webContents.on('did-fail-load', (r) => {
// http://electron.atom.io/docs/api/web-contents/#event-did-fail-load
this.emit(`${renderPrefix}did-fail-load`, {results: r})
})
win.webContents.on('did-start-loading', (r) => {
this.emit(`${renderPrefix}did-start-loading`, {results: r})
})
win.webContents.on('did-finish-load', (r) => {
this.emit(`${renderPrefix}did-finish-load`, {results: r})
})
win.webContents.on('dom-ready', (r) => {
this.emit(`${renderPrefix}dom-ready`, {results: r})
})
win.webContents.on('did-get-response-details',
function (event,
status,
newURL,
originalURL,
httpResponseCode,
requestMethod,
referrer,
headers,
resourceType) {
this.emit(`${renderPrefix}did-get-response-details`, {
event: event,
status: status,
newURL: newURL,
originalURL: originalURL,
httpResponseCode: httpResponseCode,
requestMethod: requestMethod,
referrer: referrer,
headers: headers,
resourceType: resourceType
})
})
}

// Browser Setup

/**
*
* @private
*/
_initializeWindowForResource () {
// Reset the generated flag for each input URL because this same job/window
// can be reused in this scenario
this.generated = false

// args can be modified by the client, restore them for each resource
this.args = _.cloneDeep(this.originalArgs)
this._setWindowDimensions()
}

/**
*
* @param {String} cookies - ';' delimited cookies, '=' delimited name/value
Expand Down Expand Up @@ -262,8 +277,8 @@ class ExportJob extends EventEmitter {
*
* @private
*/
_launchBrowserWindow (args) {
const browserConfig = this._getBrowserConfiguration(args)
_launchBrowserWindow () {
const browserConfig = this._getBrowserConfiguration(this.args)
this.emit('window.open.start', {})
let win = new electron.BrowserWindow(browserConfig)
this.emit('window.open.end', {
Expand All @@ -278,6 +293,19 @@ class ExportJob extends EventEmitter {
return win
}

/**
* Inpects this.args and sets the window size based on the pageSize and orientation
* @private
*/
_setWindowDimensions () {
const pageDim = this._getPageDimensions(this.args.pageSize, this.args.landscape)
var size = this.window.getSize()
if (size[0] !== pageDim.x || size[1] !== pageDim.y) {
this.emit('window.resize', {dimensions: pageDim})
this.window.setSize(pageDim.x, pageDim.y)
}
}

/**
* see
* http://electron.atom.io/docs/api/browser-window/#new-browserwindowoptions
Expand Down Expand Up @@ -350,9 +378,9 @@ class ExportJob extends EventEmitter {
}
} else {
pageDim = pageDimensions[pageSize]
// Flip if landscape
pageDim = landscape ? {x: pageDim.y, y: pageDim.x} : pageDim
}
// Flip if landscape
pageDim = landscape ? {x: pageDim.y, y: pageDim.x} : pageDim
return pageDim
}

Expand All @@ -363,9 +391,9 @@ class ExportJob extends EventEmitter {
*
* @private
*/
_loadURL (window, url, args) {
_loadURL (window, url) {
const loadOpts = {}
if (args.disableCache) {
if (this.args.disableCache) {
loadOpts.extraHeaders += 'pragma: no-cache\n'
}
window.loadURL(wargs.urlWithArgs(url, {}), loadOpts)
Expand Down Expand Up @@ -505,17 +533,20 @@ class ExportJob extends EventEmitter {
*
* @private
*/
_generateOutput (window, outputFile, args, done) {
_generateOutput (window, outputFile, done) {
if (!this.generated) {
this.generated = true
// Multi-resource jobs can have different orientations, so resize the window
// based on the orientation, which can be updated by the client using changeArgValue
this._setWindowDimensions()
this.emit('window.capture.start', {})

if (outputFile.toLowerCase().endsWith('.png')) {
this._captureImage(window, outputFile, done)
} else if (outputFile.toLowerCase().endsWith('.html')) {
this._captureHtml(window, outputFile, done)
} else {
this._capturePDF(args, window, done, outputFile)
this._capturePDF(this.args, window, done, outputFile)
}
}
}
Expand Down Expand Up @@ -553,6 +584,7 @@ class ExportJob extends EventEmitter {
pageSize: args.pageSize,
landscape: args.landscape
}
logger(pdfOptions)
window.webContents.printToPDF(pdfOptions, this._handlePDF.bind(this, outputFile, done))
}

Expand Down Expand Up @@ -616,6 +648,7 @@ class ExportJob extends EventEmitter {
}
return this.output
}

}

module.exports = ExportJob
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-pdf",
"version": "1.1.4",
"version": "1.1.5",
"description": "A command line tool to generate PDF from URL, HTML or Markdown files",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -35,7 +35,7 @@
},
"homepage": "https://github.com/fraserxu/electron-pdf",
"devDependencies": {
"ava": "^0.17.0",
"ava": "^0.18.0",
"jasmine": "^2.5.2",
"standard": "^8.4.0",
"tap-diff": "^0.1.1",
Expand Down
11 changes: 5 additions & 6 deletions test/event-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
<body>
<h1>Used to test responding to a ready event emitted on document</h1>

<table>
<table id="table" border="1" cellpadding="1" >
<thead>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>A</th>
<th>B</th>
<th>C</th>
</thead>
<tbody id="table">

<tbody>
</tbody>
</table>
<script>
Expand Down
4 changes: 2 additions & 2 deletions test/exportJob-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ test('getPageDimensions_Letter_Landscape', t => {
t.deepEqual(dim, {x: 1056, y: 816})
})

test('getPageDimensions_object', t => {
const dim = job._getPageDimensions(micronDims, false)
test('getPageDimensions_object_landscapeIsDisregarded', t => {
const dim = job._getPageDimensions(micronDims, true)
t.deepEqual(dim, {
x: ExportJob.HTML_DPI * 12,
y: ExportJob.HTML_DPI * 9
Expand Down

0 comments on commit a7ae6f7

Please sign in to comment.