feat: promisify webContents.printToPDF() (#16795)
This commit is contained in:
parent
3effa6f20c
commit
36ce3e9546
12 changed files with 161 additions and 57 deletions
|
@ -263,7 +263,7 @@ WebContents.prototype.takeHeapSnapshot = function (filePath) {
|
|||
}
|
||||
|
||||
// Translate the options of printToPDF.
|
||||
WebContents.prototype.printToPDF = function (options, callback) {
|
||||
WebContents.prototype.printToPDF = function (options) {
|
||||
const printingSetting = Object.assign({}, defaultPrintingSetting)
|
||||
if (options.landscape) {
|
||||
printingSetting.landscape = options.landscape
|
||||
|
@ -282,7 +282,7 @@ WebContents.prototype.printToPDF = function (options, callback) {
|
|||
const pageSize = options.pageSize
|
||||
if (typeof pageSize === 'object') {
|
||||
if (!pageSize.height || !pageSize.width) {
|
||||
return callback(new Error('Must define height and width for pageSize'))
|
||||
return Promise.reject(new Error('Must define height and width for pageSize'))
|
||||
}
|
||||
// Dimensions in Microns
|
||||
// 1 meter = 10^6 microns
|
||||
|
@ -295,7 +295,7 @@ WebContents.prototype.printToPDF = function (options, callback) {
|
|||
} else if (PDFPageSizes[pageSize]) {
|
||||
printingSetting.mediaSize = PDFPageSizes[pageSize]
|
||||
} else {
|
||||
return callback(new Error(`Does not support pageSize with ${pageSize}`))
|
||||
return Promise.reject(new Error(`Does not support pageSize with ${pageSize}`))
|
||||
}
|
||||
} else {
|
||||
printingSetting.mediaSize = PDFPageSizes['A4']
|
||||
|
@ -304,9 +304,9 @@ WebContents.prototype.printToPDF = function (options, callback) {
|
|||
// Chromium expects this in a 0-100 range number, not as float
|
||||
printingSetting.scaleFactor *= 100
|
||||
if (features.isPrintingEnabled()) {
|
||||
this._printToPDF(printingSetting, callback)
|
||||
return this._printToPDF(printingSetting)
|
||||
} else {
|
||||
console.error('Error: Printing feature is disabled.')
|
||||
return Promise.reject(new Error('Printing feature is disabled'))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,6 +342,9 @@ WebContents.prototype.loadFile = function (filePath, options = {}) {
|
|||
}))
|
||||
}
|
||||
|
||||
WebContents.prototype.capturePage = deprecate.promisify(WebContents.prototype.capturePage)
|
||||
WebContents.prototype.printToPDF = deprecate.promisify(WebContents.prototype.printToPDF)
|
||||
|
||||
const addReplyToEvent = (event) => {
|
||||
event.reply = (...args) => {
|
||||
event.sender.sendToFrame(event.frameId, ...args)
|
||||
|
@ -385,8 +388,6 @@ WebContents.prototype._init = function () {
|
|||
// render-view-deleted event, so ignore the listeners warning.
|
||||
this.setMaxListeners(0)
|
||||
|
||||
this.capturePage = deprecate.promisify(this.capturePage)
|
||||
|
||||
// Dispatch IPC messages to the ipc module.
|
||||
this.on('-ipc-message', function (event, internal, channel, args) {
|
||||
if (internal) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue