-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2pdf.js
More file actions
126 lines (110 loc) · 4.74 KB
/
Copy pathhtml2pdf.js
File metadata and controls
126 lines (110 loc) · 4.74 KB
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
// @ts-check
/**
* @name Html2Pdf
* @description Html2Pdf - A simple commandline HTML to PDF converter using node and puppeteer
* @author Sten Schmidt
* @version 1.0
*/
const args = require('optimist').argv;
const log4js = require('log4js');
const logger = log4js.getLogger();
const html2PdfLib = require('./Html2PdfLib');
(async () => {
try {
var help = `A simple commandline HTML to PDF converter using node and puppeteer
Usage: node html2pdf.js --html=<HtmlFile> --pdf=<PdfFile> <optional parameters>
Parameters:
--html=HTML_PATH
URL to a webressource or path to a local HTML file. URL must start with
'http'. A path to a local file is automatically converted into file:///
syntax.
--pdf=PDF_PATH
The file path to save the PDF to. If 'pdf' is a relative path, then it
is resolved relative to current working directory.
Optional parameters:
--css=CSS_PATH
URL to a webressource or path to a local CSS file. Additional CSS styles
will applyed before pdf generation. URL must start with 'http'. A path
to a local file is automatically converted into file:/// syntax.
--landscape
Paper orientation. default false.
--format=PAPER_FORMAT
Paper format, possible values are Letter, Legal, Tabloid, Ledger,
A0, A1, A2, A3, A4, A5, A6. If set, takes priority over width or
height options. Defaults to 'A4'.
--width=PAPER_WIDTH
Paper width, accepts values labeled with units, possible units: px, in,
cm, mm.
--height=PAPER_HEIGHT
Paper height, accepts values labeled with units, possible units: px,
in, cm, mm.
--margintop=VALUE
Top margin, accepts values labeled with units, possible units: px, in,
cm, mm.
--marginright=VALUE
Right margin, accepts values labeled with units, possible units: px,
in, cm, mm.
--marginbottom=VALUE
Bottom margin, accepts values labeled with units, possible units: px,
in, cm, mm.
--marginleft=VALUE
Left margin, accepts values labeled with units, possible units: px, in,
cm, mm.
--pageRanges=RANGE
Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty
string, which means print all pages.
--printBackground
Print background graphics. default false.
--scale=SCALE
Scale of the webpage rendering. default 1
--emulateScreen
Changes the CSS media type of the page to 'screen', default is 'print'.
--displayHeaderFooter
Display header and footer. default false.
--headerTemplate=HTML
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.
--footerTemplate=HTML
HTML template for the print footer. 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.
--loglevel=LOGLEVEL
Set loglevel, possible loglevels are: debug, info, error, fatal, off.
Default is info.
--logfile=LOGFILE
Set name and path to the logfile. Default is html2pdf.log.
Exit-Codes:
- In case of success: 0
- In case of error: 1
`;
if (typeof args.help !== "undefined" || typeof args.html === "undefined" || typeof args.pdf === "undefined") {
console.log(help);
process.exit();
}
if (typeof args.loglevel === "undefined") args.loglevel = "info";
if (typeof args.logfile === "undefined") args.logfile = "html2pdf.log";
log4js.configure({
appenders: {
html2pdflog: { type: 'file', filename: args.logfile },
out: { type: 'stdout', layout: { type: "basic" }}
},
categories: {
default: { appenders: ['out', 'html2pdflog'], level: args.loglevel }
}
});
var result = false;
result = await html2PdfLib.convertHtml(args.html, args.css, args.pdf, args.scale,
args.displayHeaderFooter, args.headerTemplate, args.footerTemplate, args.printBackground,
args.landscape, args.pageRanges, args.format, args.width, args.height, args.margintop,
args.marginright, args.marginbottom, args.marginleft, args.emulateScreen);
process.exitCode = result ? 0 : 1;
} catch (e) {
logger.error(e);
process.exitCode = 1;
}
})();