forked from dataverity/chromehtml2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·165 lines (154 loc) · 4.78 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
// Load packages that we need
const c = require('commander');
const p = require('puppeteer');
var args = {
out: {
pass: true,
configName: 'path',
desc: 'Output file name.'
},
executablePath: {
desc: 'If you don\'t want to use the chromium that is packaged with puppeteer, enter the full path to the executable you want here.'
},
landscape: {
pass: true,
isBool: true,
desc: 'Whether or not to print in landscape mode. Defaults to false.'
},
displayHeaderFooter: {
pass: true,
isBool: true,
desc: 'Display header and footer. Defaults to false.'
},
printBackground: {
pass: true,
isBool: true,
desc: 'Print background graphics. Defaults to false.'
},
scale: {
pass: true,
isFloat: true,
desc: 'Scale of the webpage rendering. Defaults to 1.'
},
width: {
pass: true,
desc: 'Paper width with units. Defaults to 8.5in.'
},
height: {
pass: true,
desc: 'Paper height with units. Defaults to 11in.'
},
format: {
pass: true,
desc: 'Format of page. This takes precedence over height/width. Options are Letter: 8.5in x 11in\n'+
'Legal: 8.5in x 14in\n'+
'Tabloid: 11in x 17in\n'+
'Ledger: 17in x 11in\n'+
'A0: 33.1in x 46.8in\n'+
'A1: 23.4in x 33.1in\n'+
'A2: 16.5in x 23.4in\n'+
'A3: 11.7in x 16.5in\n'+
'A4: 8.27in x 11.7in\n'+
'A5: 5.83in x 8.27in\n'+
'A6: 4.13in x 5.83in\n'
},
marginTop: {
desc: 'Top margin with units. Defaults to 1cm (~0.4 inches).'
},
marginBottom: {
desc: 'Bottom margin with units. Defaults to 1cm (~0.4 inches).'
},
marginLeft: {
desc: 'Left margin with units. Defaults to 1cm (~0.4 inches).'
},
marginRight: {
desc: 'Right margin with units. Defaults to 1cm (~0.4 inches).'
},
pageRanges: {
pass: true,
desc: 'Paper ranges to print, e.g., \'1-5, 8, 11-13\'. Defaults to the empty string, which means print all pages.'
},
/* not supported by puppeteer
ignoreInvalidPageRanges: {
'Whether to silently ignore invalid but successfully parsed page ranges, such as \'3-2\'. Defaults to false.'
},*/
headerTemplate: {
pass: true,
desc: 'HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: date - formatted print date; title - document title; url - document location; pageNumber - current page number; totalPages - total pages in the document. For example, <span class="title"></span> would generate a span containing the title. Make sure margins are such that the header will fit on the page. You may also need to explicity use CSS to set the font-size.'
},
footerTemplate: {
pass: true,
desc: 'HTML template for the print footer. Should use the same format as the `headerTemplate`. See there for more information.'
}/* not supported by puppeteer,
preferCSSPageSize: {
'Whether or not to prefer page size as defined by css. Defaults to false, in which case the content will be scaled to fit the paper size.'
}*/
};
c.arguments('<file>');
for(var i in args){
c.option('--'+i+' <'+i+'>',args[i].desc);
}
c.action(function(file){
console.log('Converting file: '+file);
// Prepare the config object
var config = {};
// Get the things that pass through.
for(var i in args){
var obj = args[i];
if(obj.pass && c[i]){
var val = c[i];
if(obj.isFloat){
val = parseFloat(val);
}
if(obj.isBool){
val = (val=='1' || val.toLowerCase()=='true');
}
var outKey = obj.configName || i;
config[outKey] = val;
console.log(i+' = '+val);
}
}
if(!config.path){
console.log("You need to include a parameter --out to hold the output file name.");
process.exit(1);
}
// Get the margins
config.margin = {};
if(c.marginTop){
console.log('marginTop = '+c.marginTop);
config.margin.top = c.marginTop
}
if(c.marginRight){
console.log('marginRight = '+c.marginRight);
config.margin.right = c.marginRight
}
if(c.marginBottom){
console.log('marginBottom = '+c.marginBottom);
config.margin.bottom = c.marginBottom
}
if(c.marginLeft){
console.log('marginLeft = '+c.marginLeft);
config.margin.left = c.marginLeft
}
// Get the page to create the PDF.
(async () => {
try{
var launchConfig = {args: ['--no-sandbox', '--disable-setuid-sandbox'] };
if(c.executablePath){
console.log('Using chrome executable: '+c.executablePath);
launchConfig.executablePath = c.executablePath;
}
const browser = await p.launch(launchConfig);
const page = await browser.newPage();
await page.goto(file, {waitUntil: 'networkidle0',timeout:0});
await page.pdf(config);
await browser.close();
}
catch(e){
console.log(e);
process.exit(1);
}
})();
}).parse(process.argv);
// console.log(process.argv);