refactor: options parsing in WebContents.prototype.printToPDF() (#40257)
* refactor: options parsing in WebContents.prototype.printToPDF() * tweak parsePageSize
This commit is contained in:
parent
025af3500c
commit
beb0cbc6d0
3 changed files with 52 additions and 141 deletions
|
@ -187,149 +187,57 @@ WebContents.prototype.executeJavaScriptInIsolatedWorld = async function (worldId
|
||||||
return ipcMainUtils.invokeInWebContents(this, IPC_MESSAGES.RENDERER_WEB_FRAME_METHOD, 'executeJavaScriptInIsolatedWorld', worldId, code, !!hasUserGesture);
|
return ipcMainUtils.invokeInWebContents(this, IPC_MESSAGES.RENDERER_WEB_FRAME_METHOD, 'executeJavaScriptInIsolatedWorld', worldId, code, !!hasUserGesture);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function checkType<T> (value: T, type: 'number' | 'boolean' | 'string' | 'object', name: string): T {
|
||||||
|
// eslint-disable-next-line valid-typeof
|
||||||
|
if (typeof value !== type) {
|
||||||
|
throw new TypeError(`${name} must be a ${type}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parsePageSize (pageSize: string | ElectronInternal.PageSize) {
|
||||||
|
if (typeof pageSize === 'string') {
|
||||||
|
const format = paperFormats[pageSize.toLowerCase()];
|
||||||
|
if (!format) {
|
||||||
|
throw new Error(`Invalid pageSize ${pageSize}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { paperWidth: format.width, paperHeight: format.height };
|
||||||
|
} else if (typeof pageSize === 'object') {
|
||||||
|
if (typeof pageSize.width !== 'number' || typeof pageSize.height !== 'number') {
|
||||||
|
throw new TypeError('width and height properties are required for pageSize');
|
||||||
|
}
|
||||||
|
|
||||||
|
return { paperWidth: pageSize.width, paperHeight: pageSize.height };
|
||||||
|
} else {
|
||||||
|
throw new TypeError('pageSize must be a string or an object');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Translate the options of printToPDF.
|
// Translate the options of printToPDF.
|
||||||
|
|
||||||
let pendingPromise: Promise<any> | undefined;
|
let pendingPromise: Promise<any> | undefined;
|
||||||
WebContents.prototype.printToPDF = async function (options) {
|
WebContents.prototype.printToPDF = async function (options) {
|
||||||
const printSettings: Record<string, any> = {
|
const margins = checkType(options.margins ?? {}, 'object', 'margins');
|
||||||
|
const printSettings = {
|
||||||
requestID: getNextId(),
|
requestID: getNextId(),
|
||||||
landscape: false,
|
landscape: checkType(options.landscape ?? false, 'boolean', 'landscape'),
|
||||||
displayHeaderFooter: false,
|
displayHeaderFooter: checkType(options.displayHeaderFooter ?? false, 'boolean', 'displayHeaderFooter'),
|
||||||
headerTemplate: '',
|
headerTemplate: checkType(options.headerTemplate ?? '', 'string', 'headerTemplate'),
|
||||||
footerTemplate: '',
|
footerTemplate: checkType(options.footerTemplate ?? '', 'string', 'footerTemplate'),
|
||||||
printBackground: false,
|
printBackground: checkType(options.printBackground ?? false, 'boolean', 'printBackground'),
|
||||||
scale: 1.0,
|
scale: checkType(options.scale ?? 1.0, 'number', 'scale'),
|
||||||
paperWidth: 8.5,
|
marginTop: checkType(margins.top ?? 0.4, 'number', 'margins.top'),
|
||||||
paperHeight: 11.0,
|
marginBottom: checkType(margins.bottom ?? 0.4, 'number', 'margins.bottom'),
|
||||||
marginTop: 0.4,
|
marginLeft: checkType(margins.left ?? 0.4, 'number', 'margins.left'),
|
||||||
marginBottom: 0.4,
|
marginRight: checkType(margins.right ?? 0.4, 'number', 'margins.right'),
|
||||||
marginLeft: 0.4,
|
pageRanges: checkType(options.pageRanges ?? '', 'string', 'pageRanges'),
|
||||||
marginRight: 0.4,
|
preferCSSPageSize: checkType(options.preferCSSPageSize ?? false, 'boolean', 'preferCSSPageSize'),
|
||||||
pageRanges: '',
|
generateTaggedPDF: checkType(options.generateTaggedPDF ?? false, 'boolean', 'generateTaggedPDF'),
|
||||||
preferCSSPageSize: false,
|
...parsePageSize(options.pageSize ?? 'letter')
|
||||||
shouldGenerateTaggedPDF: false
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options.landscape !== undefined) {
|
|
||||||
if (typeof options.landscape !== 'boolean') {
|
|
||||||
throw new Error('landscape must be a Boolean');
|
|
||||||
}
|
|
||||||
printSettings.landscape = options.landscape;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.displayHeaderFooter !== undefined) {
|
|
||||||
if (typeof options.displayHeaderFooter !== 'boolean') {
|
|
||||||
throw new Error('displayHeaderFooter must be a Boolean');
|
|
||||||
}
|
|
||||||
printSettings.displayHeaderFooter = options.displayHeaderFooter;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.printBackground !== undefined) {
|
|
||||||
if (typeof options.printBackground !== 'boolean') {
|
|
||||||
throw new Error('printBackground must be a Boolean');
|
|
||||||
}
|
|
||||||
printSettings.shouldPrintBackgrounds = options.printBackground;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.scale !== undefined) {
|
|
||||||
if (typeof options.scale !== 'number') {
|
|
||||||
throw new Error('scale must be a Number');
|
|
||||||
}
|
|
||||||
printSettings.scale = options.scale;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { pageSize } = options;
|
|
||||||
if (pageSize !== undefined) {
|
|
||||||
if (typeof pageSize === 'string') {
|
|
||||||
const format = paperFormats[pageSize.toLowerCase()];
|
|
||||||
if (!format) {
|
|
||||||
throw new Error(`Invalid pageSize ${pageSize}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
printSettings.paperWidth = format.width;
|
|
||||||
printSettings.paperHeight = format.height;
|
|
||||||
} else if (typeof options.pageSize === 'object') {
|
|
||||||
if (!pageSize.height || !pageSize.width) {
|
|
||||||
throw new Error('height and width properties are required for pageSize');
|
|
||||||
}
|
|
||||||
|
|
||||||
printSettings.paperWidth = pageSize.width;
|
|
||||||
printSettings.paperHeight = pageSize.height;
|
|
||||||
} else {
|
|
||||||
throw new Error('pageSize must be a String or Object');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { margins } = options;
|
|
||||||
if (margins !== undefined) {
|
|
||||||
if (typeof margins !== 'object') {
|
|
||||||
throw new Error('margins must be an Object');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (margins.top !== undefined) {
|
|
||||||
if (typeof margins.top !== 'number') {
|
|
||||||
throw new Error('margins.top must be a Number');
|
|
||||||
}
|
|
||||||
printSettings.marginTop = margins.top;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (margins.bottom !== undefined) {
|
|
||||||
if (typeof margins.bottom !== 'number') {
|
|
||||||
throw new Error('margins.bottom must be a Number');
|
|
||||||
}
|
|
||||||
printSettings.marginBottom = margins.bottom;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (margins.left !== undefined) {
|
|
||||||
if (typeof margins.left !== 'number') {
|
|
||||||
throw new Error('margins.left must be a Number');
|
|
||||||
}
|
|
||||||
printSettings.marginLeft = margins.left;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (margins.right !== undefined) {
|
|
||||||
if (typeof margins.right !== 'number') {
|
|
||||||
throw new Error('margins.right must be a Number');
|
|
||||||
}
|
|
||||||
printSettings.marginRight = margins.right;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.pageRanges !== undefined) {
|
|
||||||
if (typeof options.pageRanges !== 'string') {
|
|
||||||
throw new Error('pageRanges must be a String');
|
|
||||||
}
|
|
||||||
printSettings.pageRanges = options.pageRanges;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.headerTemplate !== undefined) {
|
|
||||||
if (typeof options.headerTemplate !== 'string') {
|
|
||||||
throw new Error('headerTemplate must be a String');
|
|
||||||
}
|
|
||||||
printSettings.headerTemplate = options.headerTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.footerTemplate !== undefined) {
|
|
||||||
if (typeof options.footerTemplate !== 'string') {
|
|
||||||
throw new Error('footerTemplate must be a String');
|
|
||||||
}
|
|
||||||
printSettings.footerTemplate = options.footerTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.preferCSSPageSize !== undefined) {
|
|
||||||
if (typeof options.preferCSSPageSize !== 'boolean') {
|
|
||||||
throw new Error('preferCSSPageSize must be a Boolean');
|
|
||||||
}
|
|
||||||
printSettings.preferCSSPageSize = options.preferCSSPageSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.generateTaggedPDF !== undefined) {
|
|
||||||
if (typeof options.generateTaggedPDF !== 'boolean') {
|
|
||||||
throw new Error('generateTaggedPDF must be a Boolean');
|
|
||||||
}
|
|
||||||
printSettings.shouldGenerateTaggedPDF = options.generateTaggedPDF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._printToPDF) {
|
if (this._printToPDF) {
|
||||||
if (pendingPromise) {
|
if (pendingPromise) {
|
||||||
pendingPromise = pendingPromise.then(() => this._printToPDF(printSettings));
|
pendingPromise = pendingPromise.then(() => this._printToPDF(printSettings));
|
||||||
|
@ -346,7 +254,7 @@ WebContents.prototype.printToPDF = async function (options) {
|
||||||
// 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') {
|
||||||
throw new Error('webContents.print(): Invalid print settings specified.');
|
throw new TypeError('webContents.print(): Invalid print settings specified.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const printSettings: Record<string, any> = { ...options };
|
const printSettings: Record<string, any> = { ...options };
|
||||||
|
@ -361,7 +269,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
|
||||||
const height = Math.ceil(pageSize.height);
|
const height = Math.ceil(pageSize.height);
|
||||||
const width = Math.ceil(pageSize.width);
|
const width = Math.ceil(pageSize.width);
|
||||||
if (!isValidCustomPageSize(width, height)) {
|
if (!isValidCustomPageSize(width, height)) {
|
||||||
throw new Error('height and width properties must be minimum 352 microns.');
|
throw new RangeError('height and width properties must be minimum 352 microns.');
|
||||||
}
|
}
|
||||||
|
|
||||||
printSettings.mediaSize = {
|
printSettings.mediaSize = {
|
||||||
|
|
|
@ -3116,8 +3116,7 @@ v8::Local<v8::Promise> WebContents::PrintToPDF(const base::Value& settings) {
|
||||||
auto header_template = *settings.GetDict().FindString("headerTemplate");
|
auto header_template = *settings.GetDict().FindString("headerTemplate");
|
||||||
auto footer_template = *settings.GetDict().FindString("footerTemplate");
|
auto footer_template = *settings.GetDict().FindString("footerTemplate");
|
||||||
auto prefer_css_page_size = settings.GetDict().FindBool("preferCSSPageSize");
|
auto prefer_css_page_size = settings.GetDict().FindBool("preferCSSPageSize");
|
||||||
auto generate_tagged_pdf =
|
auto generate_tagged_pdf = settings.GetDict().FindBool("generateTaggedPDF");
|
||||||
settings.GetDict().FindBool("shouldGenerateTaggedPDF");
|
|
||||||
|
|
||||||
absl::variant<printing::mojom::PrintPagesParamsPtr, std::string>
|
absl::variant<printing::mojom::PrintPagesParamsPtr, std::string>
|
||||||
print_pages_params = print_to_pdf::GetPrintPagesParams(
|
print_pages_params = print_to_pdf::GetPrintPagesParams(
|
||||||
|
|
|
@ -90,7 +90,11 @@ app.whenReady().then(() => {
|
||||||
},
|
},
|
||||||
printBackground: true,
|
printBackground: true,
|
||||||
pageRanges: '1-3',
|
pageRanges: '1-3',
|
||||||
landscape: true
|
landscape: true,
|
||||||
|
pageSize: {
|
||||||
|
width: 100,
|
||||||
|
height: 100
|
||||||
|
}
|
||||||
}).then((data: Buffer) => console.log(data));
|
}).then((data: Buffer) => console.log(data));
|
||||||
|
|
||||||
mainWindow.webContents.printToPDF({}).then(data => console.log(data));
|
mainWindow.webContents.printToPDF({}).then(data => console.log(data));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue