Skip to content

Commit b1fb7c7

Browse files
miniakzcbenz
authored andcommitted
fix: properly generate requestID in webContents.printToPDF() (electron#20769) (electron#20810)
1 parent 725dac3 commit b1fb7c7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/browser/api/web-contents.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ const defaultPrintingSetting = {
7272
headerFooterEnabled: false,
7373
marginsType: 0,
7474
isFirstRequest: false,
75-
requestID: getNextId(),
7675
previewUIID: 0,
7776
previewModifiable: true,
7877
printToPDF: true,
@@ -205,7 +204,10 @@ WebContents.prototype.executeJavaScript = function (code, hasUserGesture) {
205204

206205
// Translate the options of printToPDF.
207206
WebContents.prototype.printToPDF = function (options) {
208-
const printingSetting = Object.assign({}, defaultPrintingSetting)
207+
const printingSetting = {
208+
...defaultPrintingSetting,
209+
requestID: getNextId()
210+
}
209211
if (options.landscape) {
210212
printingSetting.landscape = options.landscape
211213
}

spec/api-web-contents-spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,25 @@ describe('webContents module', () => {
12751275
const data = await w.webContents.printToPDF({})
12761276
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
12771277
})
1278+
1279+
it('does not crash when called multiple times', async () => {
1280+
w.destroy()
1281+
w = new BrowserWindow({
1282+
show: false,
1283+
webPreferences: {
1284+
sandbox: true
1285+
}
1286+
})
1287+
await w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
1288+
const promises = []
1289+
for (let i = 0; i < 2; i++) {
1290+
promises.push(w.webContents.printToPDF({}))
1291+
}
1292+
const results = await Promise.all(promises)
1293+
for (const data of results) {
1294+
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
1295+
}
1296+
})
12781297
})
12791298

12801299
describe('PictureInPicture video', () => {

0 commit comments

Comments
 (0)