diff --git a/lib/browser/api/web-contents.ts b/lib/browser/api/web-contents.ts index 5ebf6a4509..f067e30cba 100644 --- a/lib/browser/api/web-contents.ts +++ b/lib/browser/api/web-contents.ts @@ -212,7 +212,7 @@ function parsePageSize (pageSize: string | ElectronInternal.PageSize) { // Translate the options of printToPDF. -let pendingPromise: Promise | undefined; +const printToPDFQueues = new WeakMap>(); WebContents.prototype.printToPDF = async function (options) { const margins = checkType(options.margins ?? {}, 'object', 'margins'); const pageSize = parsePageSize(options.pageSize ?? 'letter'); @@ -244,16 +244,19 @@ WebContents.prototype.printToPDF = async function (options) { ...pageSize }; - if (this._printToPDF) { - if (pendingPromise) { - pendingPromise = pendingPromise.then(() => this._printToPDF(printSettings)); - } else { - pendingPromise = this._printToPDF(printSettings); - } - return pendingPromise; - } else { + if (!this._printToPDF) { throw new Error('Printing feature is disabled'); } + + const prev = printToPDFQueues.get(this) ?? Promise.resolve(); + const next = prev.catch(() => {}).then(() => this._printToPDF(printSettings)); + printToPDFQueues.set(this, next); + next + .finally(() => { + if (printToPDFQueues.get(this) === next) printToPDFQueues.delete(this); + }) + .catch(() => {}); + return next; }; // TODO(codebytere): deduplicate argument sanitization by moving rest of diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents-spec.ts index 255e5dc424..3098541792 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -2996,6 +2996,16 @@ describe('webContents module', () => { expect(pdfInfo.numPages).to.equal(3); }); + it('recovers after a prior call fails with an invalid page range', async () => { + await w.loadURL('data:text/html,

Hello, World!

'); + + await expect(w.webContents.printToPDF({ pageRanges: '999' })).to.eventually.be.rejected(); + + const data = await w.webContents.printToPDF({}); + const pdfInfo = await readPDF(data); + expect(pdfInfo.numPages).to.equal(1); + }); + it('does not tag PDFs by default', async () => { await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'print-to-pdf-small.html'));