fix: failure on immutable webContents.print(options) (#39985)

fix: failure on immutable webContents.print(options)
This commit is contained in:
Shelley Vohr 2023-09-28 10:41:46 +02:00 committed by GitHub
parent 689d1b76de
commit c8156c3c57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -345,7 +345,12 @@ 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 (options: ElectronInternal.WebContentsPrintOptions, callback) {
if (typeof options === 'object') {
if (typeof options !== 'object') {
throw new Error('webContents.print(): Invalid print settings specified.');
}
const printSettings: Record<string, any> = { ...options };
const pageSize = options.pageSize ?? 'A4';
if (typeof pageSize === 'object') {
if (!pageSize.height || !pageSize.width) {
@ -359,7 +364,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
throw new Error('height and width properties must be minimum 352 microns.');
}
options.mediaSize = {
printSettings.mediaSize = {
name: 'CUSTOM',
custom_display_name: 'Custom',
height_microns: height,
@ -371,7 +376,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
};
} else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
const mediaSize = PDFPageSizes[pageSize];
options.mediaSize = {
printSettings.mediaSize = {
...mediaSize,
imageable_area_left_microns: 0,
imageable_area_bottom_microns: 0,
@ -381,13 +386,12 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
} else {
throw new Error(`Unsupported pageSize: ${pageSize}`);
}
}
if (this._print) {
if (callback) {
this._print(options, callback);
this._print(printSettings, callback);
} else {
this._print(options);
this._print(printSettings);
}
} else {
console.error('Error: Printing feature is disabled.');