feat: allow specifying pageSize for print (#22014)

This commit is contained in:
Shelley Vohr 2020-02-05 04:25:02 +00:00 committed by GitHub
parent 385388dd6b
commit 928175bdfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 2 deletions

View file

@ -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.')
}