New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uncaught ReferenceError: ipcApi is not defined #288
Comments
That's a good write up, thanks. My team is actually still on the 4.x branch of electron-pdf. So it's most likely that something in the new(er) version(s) of Electron is causing your issue, because we rely on the ipc and If you are are able to solve the problem I'll happily merge in a PR to the appropriate branch (ideally master) |
We have found the issue. It is only triggered because of our options, explaining why you haven't seen more complaints about it. Let me explain:
The easiest way to solve this for our case would be to use Another possibility would be to load the default options first and then setup the webPreferences on top, because this is tighly related to how electron-pdf works. Any opinions? |
I think my original intent behind the I do not think we can break backwards compatibility by switching to One other consideration... Is the Line 353 in d5332fc
We could add a flag for this single option.
We could add another CLI option for |
For our particular case we don't need this anymore (we can run with CORS enabled now) so we don't feel strongly about any approach. We are still testing, but it looks like we can now run without this and everything is fine. I was trying to find a more general solution that allowed users to set any of the webPreference options because there seem to be some useful ones (e.g.: Of course we could create analogous options in _getBrowserConfiguration (args) {
const pageDim = WindowTailor.getPageDimensions(args.pageSize, args.landscape)
const defaultOpts = {
width: pageDim.x,
height: pageDim.y,
enableLargerThanScreen: true,
show: false,
center: true, // Display in center of screen,
}
let cmdLineBrowserConfig = {}
try {
cmdLineBrowserConfig = JSON.parse(args.browserConfig || '{}')
} catch (e) {
this.error('Invalid browserConfig provided, using defaults. Value:',
args.browserConfig,
'\nError:', e)
}
const opts = _.extend(defaultOpts, cmdLineBrowserConfig)
// This creates a new session for every browser window, otherwise the same
// default session is used from the main process which would break support
// for concurrency
// see http://electron.atom.io/docs/api/browser-window/#new-browserwindowoptions options.partition
opts.webPreferences.partition = this.jobId
// This ensures that we define the ipcApi bridge to node's apis
opts.webPreferences.preload = path.join(__dirname, 'preload.js')
// This is for backwards compatibility
opts.webPreferences.trustRemoteContent = _.get(this.options, 'trustRemoteContent', ._get(opts.webPreferences, 'trustRemoteContent', false))
return opts
} As a side-note, notice that just passing |
Or even this, where _getBrowserConfiguration (args) {
const pageDim = WindowTailor.getPageDimensions(args.pageSize, args.landscape)
const defaultOpts = {
width: pageDim.x,
height: pageDim.y,
enableLargerThanScreen: true,
show: false,
center: true, // Display in center of screen,
}
let cmdLineBrowserConfig = {}
try {
cmdLineBrowserConfig = JSON.parse(args.browserConfig || '{}')
} catch (e) {
this.error('Invalid browserConfig provided, using defaults. Value:',
args.browserConfig,
'\nError:', e)
}
const opts = _.extend(defaultOpts, cmdLineBrowserConfig)
opts.webPreferences = _.merge({
// This creates a new session for every browser window, otherwise the same
// default session is used from the main process which would break support
// for concurrency
// see http://electron.atom.io/docs/api/browser-window/#new-browserwindowoptions options.partition
partition: this.jobId,
// This ensures that we define the ipcApi bridge to node's apis
preload: path.join(__dirname, 'preload.js'),
// This is for backwards compatibility
trustRemoteContent = _.get(this.options, 'trustRemoteContent', false)
}, opts.webPreferences)
return opts
} |
Let's go with this option. If you can also add documentation of the change that would be great. |
We've been using electron-pdf for a while. Recently we noticed that PDF generation seemed to be slower than it used to. Inspecting the logs we saw that electron-pdf was logging some error:
This didn't seem a problem in our code but something in
electron
orelectron-pdf
being wrong. After enabling electron's logging (passing--enable-logging
to the command), we got the actual javascript error:This pointed us to the real issue:
electron-pdf
prepares a job, it injects thelib/preload.js
file, which is supposed to provide anipcApi
object with some methods that call node's internal APIs.view-ready
event notification cannot be bridged back to electron-pdf and our PDFs are generated only after the timeout. That's why it got slower for us.We have been able to reproduce this in several systems, including:
The text was updated successfully, but these errors were encountered: