Merge pull request #2338 from atom/page_size_option
Add 'pageSize' option in printToPDF API.
This commit is contained in:
commit
c140077d53
2 changed files with 63 additions and 28 deletions
|
@ -6,6 +6,34 @@ ipc = require 'ipc'
|
||||||
nextId = 0
|
nextId = 0
|
||||||
getNextId = -> ++nextId
|
getNextId = -> ++nextId
|
||||||
|
|
||||||
|
PDFPageSize =
|
||||||
|
A4:
|
||||||
|
custom_display_name: "A4"
|
||||||
|
height_microns: 297000
|
||||||
|
name: "ISO_A4"
|
||||||
|
is_default: "true"
|
||||||
|
width_microns: 210000
|
||||||
|
A3:
|
||||||
|
custom_display_name: "A3"
|
||||||
|
height_microns: 420000
|
||||||
|
name: "ISO_A3"
|
||||||
|
width_microns: 297000
|
||||||
|
Legal:
|
||||||
|
custom_display_name: "Legal"
|
||||||
|
height_microns: 355600
|
||||||
|
name: "NA_LEGAL"
|
||||||
|
width_microns: 215900
|
||||||
|
Letter:
|
||||||
|
custom_display_name: "Letter"
|
||||||
|
height_microns: 279400
|
||||||
|
name: "NA_LETTER"
|
||||||
|
width_microns: 215900
|
||||||
|
Tabloid:
|
||||||
|
height_microns: 431800
|
||||||
|
name: "NA_LEDGER"
|
||||||
|
width_microns: 279400
|
||||||
|
custom_display_name: "Tabloid"
|
||||||
|
|
||||||
wrapWebContents = (webContents) ->
|
wrapWebContents = (webContents) ->
|
||||||
# webContents is an EventEmitter.
|
# webContents is an EventEmitter.
|
||||||
webContents.__proto__ = EventEmitter.prototype
|
webContents.__proto__ = EventEmitter.prototype
|
||||||
|
@ -41,32 +69,27 @@ wrapWebContents = (webContents) ->
|
||||||
|
|
||||||
webContents.printToPDF = (options, callback) ->
|
webContents.printToPDF = (options, callback) ->
|
||||||
printingSetting =
|
printingSetting =
|
||||||
pageRage:[],
|
pageRage: []
|
||||||
mediaSize:
|
mediaSize: {}
|
||||||
height_microns:297000,
|
landscape: false
|
||||||
is_default:true,
|
color: 2
|
||||||
name:"ISO_A4",
|
headerFooterEnabled: false
|
||||||
width_microns:210000,
|
marginsType: 0
|
||||||
custom_display_name:"A4",
|
isFirstRequest: false
|
||||||
landscape:false,
|
requestID: getNextId()
|
||||||
color:2,
|
previewModifiable: true
|
||||||
headerFooterEnabled:false,
|
printToPDF: true
|
||||||
marginsType:0,
|
printWithCloudPrint: false
|
||||||
isFirstRequest:false,
|
printWithPrivet: false
|
||||||
requestID: getNextId(),
|
printWithExtension: false
|
||||||
previewModifiable:true,
|
deviceName: "Save as PDF"
|
||||||
printToPDF:true,
|
generateDraftData: true
|
||||||
printWithCloudPrint:false,
|
fitToPageEnabled: false
|
||||||
printWithPrivet:false,
|
duplex: 0
|
||||||
printWithExtension:false,
|
copies: 1
|
||||||
deviceName:"Save as PDF",
|
collate: true
|
||||||
generateDraftData:true,
|
shouldPrintBackgrounds: false
|
||||||
fitToPageEnabled:false,
|
shouldPrintSelectionOnly: 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
|
||||||
|
@ -77,6 +100,11 @@ wrapWebContents = (webContents) ->
|
||||||
if options.printBackgrounds
|
if options.printBackgrounds
|
||||||
printingSetting.shouldPrintBackgrounds = options.printBackground
|
printingSetting.shouldPrintBackgrounds = options.printBackground
|
||||||
|
|
||||||
|
if options.pageSize and PDFPageSize[options.pageSize]
|
||||||
|
printingSetting.mediaSize = PDFPageSize[options.pageSize]
|
||||||
|
else
|
||||||
|
printingSetting.mediaSize = PDFPageSize['A4']
|
||||||
|
|
||||||
@_printToPDF printingSetting, callback
|
@_printToPDF printingSetting, callback
|
||||||
|
|
||||||
binding._setWrapWebContents wrapWebContents
|
binding._setWrapWebContents wrapWebContents
|
||||||
|
|
|
@ -1001,6 +1001,12 @@ size.
|
||||||
* 0 - default
|
* 0 - default
|
||||||
* 1 - none
|
* 1 - none
|
||||||
* 2 - minimum
|
* 2 - minimum
|
||||||
|
* `pageSize` String - Specify page size of the generated PDF
|
||||||
|
* `A4`
|
||||||
|
* `A3`
|
||||||
|
* `Legal`
|
||||||
|
* `Letter`
|
||||||
|
* `Tabloid`
|
||||||
* `printBackground` Boolean - Whether to print CSS backgrounds.
|
* `printBackground` Boolean - Whether to print CSS backgrounds.
|
||||||
* `printSelectionOnly` Boolean - Whether to print selection only.
|
* `printSelectionOnly` Boolean - Whether to print selection only.
|
||||||
* `landscape` Boolean - `true` for landscape, `false` for portrait.
|
* `landscape` Boolean - `true` for landscape, `false` for portrait.
|
||||||
|
@ -1027,9 +1033,10 @@ win.webContents.on("did-finish-load", function() {
|
||||||
// Use default printing options
|
// Use default printing options
|
||||||
win.webContents.printToPDF({}, function(error, data) {
|
win.webContents.printToPDF({}, function(error, data) {
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
fs.writeFile(dist, data, function(error) {
|
fs.writeFile("/tmp/print.pdf", data, function(error) {
|
||||||
if (err)
|
if (err)
|
||||||
alert('write pdf file error', error);
|
throw error;
|
||||||
|
console.log("Write PDF successfully.");
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue