diff --git a/lib/browser/api/web-contents.ts b/lib/browser/api/web-contents.ts index 8da3c91b72a..7c3fabfc935 100644 --- a/lib/browser/api/web-contents.ts +++ b/lib/browser/api/web-contents.ts @@ -337,10 +337,9 @@ WebContents.prototype.printToPDF = async function (options) { // TODO(codebytere): deduplicate argument sanitization by moving rest of // print param logic into new file shared between printToPDF and print -WebContents.prototype.print = function (printOptions: ElectronInternal.WebContentsPrintOptions, callback) { - const options = printOptions ?? {}; - if (options.pageSize) { - const pageSize = options.pageSize; +WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions, callback) { + if (typeof options === 'object') { + const pageSize = options.pageSize ?? 'A4'; if (typeof pageSize === 'object') { if (!pageSize.height || !pageSize.width) { throw new Error('height and width properties are required for pageSize'); @@ -357,10 +356,21 @@ WebContents.prototype.print = function (printOptions: ElectronInternal.WebConten name: 'CUSTOM', custom_display_name: 'Custom', height_microns: height, - width_microns: width + width_microns: width, + imageable_area_left_microns: 0, + imageable_area_bottom_microns: 0, + imageable_area_right_microns: width, + imageable_area_top_microns: height + }; + } else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) { + const mediaSize = PDFPageSizes[pageSize]; + options.mediaSize = { + ...mediaSize, + imageable_area_left_microns: 0, + imageable_area_bottom_microns: 0, + imageable_area_right_microns: mediaSize.width_microns, + imageable_area_top_microns: mediaSize.height_microns }; - } else if (PDFPageSizes[pageSize]) { - options.mediaSize = PDFPageSizes[pageSize]; } else { throw new Error(`Unsupported pageSize: ${pageSize}`); } diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents-spec.ts index 2c36d6250d2..176f200ef25 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -194,6 +194,14 @@ describe('webContents module', () => { }).to.throw('webContents.print(): Invalid print settings specified.'); }); + it('throws when an invalid pageSize is passed', () => { + const badSize = 5; + expect(() => { + // @ts-ignore this line is intentionally incorrect + w.webContents.print({ pageSize: badSize }); + }).to.throw(`Unsupported pageSize: ${badSize}`); + }); + it('throws when an invalid callback is passed', () => { expect(() => { // @ts-ignore this line is intentionally incorrect diff --git a/typings/internal-electron.d.ts b/typings/internal-electron.d.ts index 6c1da609207..cbdfa051f70 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -242,6 +242,10 @@ declare namespace ElectronInternal { custom_display_name: string, height_microns: number, width_microns: number, + imageable_area_left_microns?: number, + imageable_area_bottom_microns?: number, + imageable_area_right_microns?: number, + imageable_area_top_microns?: number, is_default?: 'true', }