Cleanup the printToPDF code

This commit is contained in:
Cheng Zhao 2016-06-01 15:24:53 +09:00
parent eb882855bc
commit 0864d3b1ee

View file

@ -15,7 +15,8 @@ const getNextId = function () {
return ++nextId return ++nextId
} }
const PDFPageSize = { // Stock page sizes
const PDFPageSizes = {
A5: { A5: {
custom_display_name: 'A5', custom_display_name: 'A5',
height_microns: 210000, height_microns: 210000,
@ -55,6 +56,31 @@ const PDFPageSize = {
} }
} }
// Default printing setting
const defaultPrintingSetting = {
pageRage: [],
mediaSize: {},
landscape: false,
color: 2,
headerFooterEnabled: false,
marginsType: 0,
isFirstRequest: false,
requestID: getNextId(),
previewModifiable: true,
printToPDF: true,
printWithCloudPrint: false,
printWithPrivet: false,
printWithExtension: false,
deviceName: 'Save as PDF',
generateDraftData: true,
fitToPageEnabled: false,
duplex: 0,
copies: 1,
collate: true,
shouldPrintBackgrounds: false,
shouldPrintSelectionOnly: false
}
// Following methods are mapped to webFrame. // Following methods are mapped to webFrame.
const webFrameMethods = [ const webFrameMethods = [
'insertText', 'insertText',
@ -158,29 +184,7 @@ const wrapWebContents = function (webContents) {
}) })
webContents.printToPDF = function (options, callback) { webContents.printToPDF = function (options, callback) {
const printingSetting = { const printingSetting = Object.assign({}, defaultPrintingSetting)
pageRage: [],
mediaSize: {},
landscape: false,
color: 2,
headerFooterEnabled: false,
marginsType: 0,
isFirstRequest: false,
requestID: getNextId(),
previewModifiable: true,
printToPDF: true,
printWithCloudPrint: false,
printWithPrivet: false,
printWithExtension: false,
deviceName: 'Save as PDF',
generateDraftData: true,
fitToPageEnabled: false,
duplex: 0,
copies: 1,
collate: true,
shouldPrintBackgrounds: false,
shouldPrintSelectionOnly: false
}
if (options.landscape) { if (options.landscape) {
printingSetting.landscape = options.landscape printingSetting.landscape = options.landscape
} }
@ -195,30 +199,29 @@ const wrapWebContents = function (webContents) {
} }
if (options.pageSize) { if (options.pageSize) {
let height = 0 const pageSize = options.pageSize
let width = 0 if (typeof pageSize === 'object') {
if (typeof options.pageSize === 'object') { if (!pageSize.height || !pageSize.width) {
return callback(new Error('Must define height and width for pageSize'))
}
// Dimensions in Microns // Dimensions in Microns
// 1 meter = 10^6 microns // 1 meter = 10^6 microns
height = options.pageSize.height ? options.pageSize.height : 0
width = options.pageSize.width ? options.pageSize.width : 0
}
if (height > 0 && width > 0) {
printingSetting.mediaSize = { printingSetting.mediaSize = {
height_microns: height,
name: 'CUSTOM', name: 'CUSTOM',
width_microns: width, custom_display_name: 'Custom',
custom_display_name: 'Custom' height_microns: pageSize.height,
width_microns: pageSize.width
} }
} else if (PDFPageSize[options.pageSize]) { } else if (PDFPageSizes[pageSize]) {
printingSetting.mediaSize = PDFPageSize[options.pageSize] printingSetting.mediaSize = PDFPageSizes[pageSize]
} else { } else {
printingSetting.mediaSize = PDFPageSize['A4'] return callback(new Error(`Does not support pageSize with ${pageSize}`))
} }
} else {
printingSetting.mediaSize = PDFPageSizes['A4']
} }
return this._printToPDF(printingSetting, callback) this._printToPDF(printingSetting, callback)
} }
} }