fix: failure on immutable webContents.print(options)
(#39985)
fix: failure on immutable webContents.print(options)
This commit is contained in:
parent
689d1b76de
commit
c8156c3c57
1 changed files with 39 additions and 35 deletions
|
@ -345,49 +345,53 @@ WebContents.prototype.printToPDF = async function (options) {
|
||||||
// TODO(codebytere): deduplicate argument sanitization by moving rest of
|
// TODO(codebytere): deduplicate argument sanitization by moving rest of
|
||||||
// print param logic into new file shared between printToPDF and print
|
// print param logic into new file shared between printToPDF and print
|
||||||
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions, callback) {
|
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions, callback) {
|
||||||
if (typeof options === 'object') {
|
if (typeof options !== 'object') {
|
||||||
const pageSize = options.pageSize ?? 'A4';
|
throw new Error('webContents.print(): Invalid print settings specified.');
|
||||||
if (typeof pageSize === 'object') {
|
}
|
||||||
if (!pageSize.height || !pageSize.width) {
|
|
||||||
throw new Error('height and width properties are required for pageSize');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dimensions in Microns - 1 meter = 10^6 microns
|
const printSettings: Record<string, any> = { ...options };
|
||||||
const height = Math.ceil(pageSize.height);
|
|
||||||
const width = Math.ceil(pageSize.width);
|
|
||||||
if (!isValidCustomPageSize(width, height)) {
|
|
||||||
throw new Error('height and width properties must be minimum 352 microns.');
|
|
||||||
}
|
|
||||||
|
|
||||||
options.mediaSize = {
|
const pageSize = options.pageSize ?? 'A4';
|
||||||
name: 'CUSTOM',
|
if (typeof pageSize === 'object') {
|
||||||
custom_display_name: 'Custom',
|
if (!pageSize.height || !pageSize.width) {
|
||||||
height_microns: height,
|
throw new Error('height and width properties are required for pageSize');
|
||||||
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 {
|
|
||||||
throw new Error(`Unsupported pageSize: ${pageSize}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dimensions in Microns - 1 meter = 10^6 microns
|
||||||
|
const height = Math.ceil(pageSize.height);
|
||||||
|
const width = Math.ceil(pageSize.width);
|
||||||
|
if (!isValidCustomPageSize(width, height)) {
|
||||||
|
throw new Error('height and width properties must be minimum 352 microns.');
|
||||||
|
}
|
||||||
|
|
||||||
|
printSettings.mediaSize = {
|
||||||
|
name: 'CUSTOM',
|
||||||
|
custom_display_name: 'Custom',
|
||||||
|
height_microns: height,
|
||||||
|
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];
|
||||||
|
printSettings.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 {
|
||||||
|
throw new Error(`Unsupported pageSize: ${pageSize}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._print) {
|
if (this._print) {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
this._print(options, callback);
|
this._print(printSettings, callback);
|
||||||
} else {
|
} else {
|
||||||
this._print(options);
|
this._print(printSettings);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.error('Error: Printing feature is disabled.');
|
console.error('Error: Printing feature is disabled.');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue