forked from fraserxu/electron-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (105 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var app = require('app')
var meow = require('meow')
var fs = require('fs')
var path = require('path')
var BrowserWindow = require('browser-window')
var wargs = require('./lib/args')
var markdownToHTMLPath = require('./lib/markdown')
app.on('ready', appReady)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
var cli = meow({
pkg: './package.json',
help: [
'Options',
' --help Show this help',
' --version Current version of package',
' -i | --input String - The path to the HTML file or url',
' -o | --output String - The path of the output PDF',
' -c | --css String - The path to custom CSS',
' -b | --printBackground Boolean - Whether to print CSS backgrounds.',
' false - default',
' -s | --printSelectionOnly Boolean - Whether to print selection only',
' false - default',
' -l | --landscape Boolean - true for landscape, false for portrait.',
' false - default',
' -m | --marginType Integer - Specify the type of margins to use',
' 0 - default',
' 1 - none',
' 2 - minimum',
'',
'Usage',
' $ electron-pdf <input> <output>',
' $ electron-pdf <input> <output> -l',
'',
'Examples',
' $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.pdf',
' $ electron-pdf ./index.html ~/Desktop/index.pdf',
' $ electron-pdf ./README.md ~/Desktop/README.pdf -l',
' $ electron-pdf ./README.md ~/Desktop/README.pdf -l -c my-awesome-css.css',
''
].join('\n')
})
function appReady () {
var input = cli.input[0] || cli.flags.i || cli.flags.input
var output = cli.input[1] || cli.flags.o || cli.flags.output
var customCss = cli.flags.c || cli.flags.css
if (!input || !output) {
cli.showHelp()
app.quit()
}
function isMarkdown (input) {
var ext = path.extname(input)
return ext.indexOf('md') > 0 || ext.indexOf('markdown') > 0
}
if (isMarkdown(input)) {
var opts = {}
if (customCss) {
opts.customCss = customCss
}
// if given a markdown, render it into HTML and return the path of the HTML
input = markdownToHTMLPath(input, opts, function (err, tmpHTMLPath) {
if (err) {
console.error('Parse markdown file error', err)
app.quit()
}
var indexUrl = wargs.urlWithArgs(tmpHTMLPath, {})
render(indexUrl, output)
})
} else {
var indexUrl = wargs.urlWithArgs(input, {})
render(indexUrl, output)
}
}
/**
* render file to pdf
* @param {String} indexUrl The path to the HTML or url
*/
function render (indexUrl, output) {
var win = new BrowserWindow({ width: 0, height: 0, show: false })
win.on('closed', function () { win = null })
win.loadUrl(indexUrl)
// print to pdf args
var opts = {
marginType: cli.flags.m || cli.flags.marginType || 0,
printBackground: cli.flags.p || cli.flags.printBackground || false,
printSelectionOnly: cli.flags.s || cli.flags.printSelectionOnly || false,
landscape: cli.flags.l || cli.flags.landscape || false
}
win.webContents.on('did-finish-load', function () {
win.printToPDF(opts, function (err, data) {
if (err) {
console.error(err)
}
fs.writeFile(path.resolve(output), data, function (err) {
if (err) {
console.error(err)
}
app.quit()
})
})
})
}