feat: allow specifying pageSize for print (#22014)
This commit is contained in:
parent
385388dd6b
commit
928175bdfe
5 changed files with 48 additions and 2 deletions
|
@ -340,9 +340,38 @@ WebContents.prototype.printToPDF = function (options) {
|
|||
}
|
||||
}
|
||||
|
||||
WebContents.prototype.print = function (...args) {
|
||||
WebContents.prototype.print = function (options = {}, callback) {
|
||||
// TODO(codebytere): deduplicate argument sanitization by moving rest of
|
||||
// print param logic into new file shared between printToPDF and print
|
||||
if (typeof options === 'object') {
|
||||
// Optionally set size for PDF.
|
||||
if (options.pageSize !== undefined) {
|
||||
const pageSize = options.pageSize
|
||||
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
|
||||
options.mediaSize = {
|
||||
name: 'CUSTOM',
|
||||
custom_display_name: 'Custom',
|
||||
height_microns: Math.ceil(pageSize.height),
|
||||
width_microns: Math.ceil(pageSize.width)
|
||||
}
|
||||
} else if (PDFPageSizes[pageSize]) {
|
||||
options.mediaSize = PDFPageSizes[pageSize]
|
||||
} else {
|
||||
throw new Error(`Unsupported pageSize: ${pageSize}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (features.isPrintingEnabled()) {
|
||||
this._print(...args)
|
||||
if (callback) {
|
||||
this._print(options, callback)
|
||||
} else {
|
||||
this._print(options)
|
||||
}
|
||||
} else {
|
||||
console.error('Error: Printing feature is disabled.')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue