Skip to content
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

fix: crashes when calling webContents.printToPDF() multiple times #20802

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/browser/api/web-contents.js
Expand Up @@ -71,7 +71,6 @@ const defaultPrintingSetting = {
headerFooterEnabled: false,
marginsType: 0,
isFirstRequest: false,
requestID: getNextId(),
previewUIID: 0,
previewModifiable: true,
printToPDF: true,
Expand Down Expand Up @@ -204,7 +203,10 @@ WebContents.prototype.executeJavaScript = function (code, hasUserGesture) {

// Translate the options of printToPDF.
WebContents.prototype.printToPDF = function (options) {
const printingSetting = Object.assign({}, defaultPrintingSetting)
const printingSetting = {
...defaultPrintingSetting,
requestID: getNextId()
}
if (options.landscape) {
printingSetting.landscape = options.landscape
}
Expand Down
13 changes: 13 additions & 0 deletions spec-main/api-web-contents-spec.ts
Expand Up @@ -1402,6 +1402,19 @@ describe('webContents module', () => {
const data = await w.webContents.printToPDF({})
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
})

it('does not crash when called multiple times', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } })
await w.loadURL('data:text/html,<h1>Hello, World!</h1>')
const promises = []
for (let i = 0; i < 2; i++) {
promises.push(w.webContents.printToPDF({}))
}
const results = await Promise.all(promises)
for (const data of results) {
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
}
})
})

describe('PictureInPicture video', () => {
Expand Down