Cleanup the printToPDF code
authorCheng Zhao <zcbenz@gmail.com>
Wed, 1 Jun 2016 06:24:53 +0000 (15:24 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 1 Jun 2016 06:32:37 +0000 (15:32 +0900)
lib/browser/api/web-contents.js

index d7bcdb4..ed7bfe0 100644 (file)
@@ -15,7 +15,8 @@ const getNextId = function () {
   return ++nextId
 }
 
-const PDFPageSize = {
+// Stock page sizes
+const PDFPageSizes = {
   A5: {
     custom_display_name: 'A5',
     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.
 const webFrameMethods = [
   'insertText',
@@ -158,29 +184,7 @@ const wrapWebContents = function (webContents) {
   })
 
   webContents.printToPDF = function (options, callback) {
-    const printingSetting = {
-      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
-    }
+    const printingSetting = Object.assign({}, defaultPrintingSetting)
     if (options.landscape) {
       printingSetting.landscape = options.landscape
     }
@@ -195,30 +199,29 @@ const wrapWebContents = function (webContents) {
     }
 
     if (options.pageSize) {
-      let height = 0
-      let width = 0
-      if (typeof options.pageSize === 'object') {
+      const pageSize = options.pageSize
+      if (typeof pageSize === 'object') {
+        if (!pageSize.height || !pageSize.width) {
+          return callback(new Error('Must define height and width for pageSize'))
+        }
         // Dimensions in 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 = {
-          height_microns: height,
           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]) {
-        printingSetting.mediaSize = PDFPageSize[options.pageSize]
+      } else if (PDFPageSizes[pageSize]) {
+        printingSetting.mediaSize = PDFPageSizes[pageSize]
       } 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)
   }
 }