This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
forked from peterdemartini/html5-to-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.js
executable file
·105 lines (96 loc) · 2.7 KB
/
command.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
#!/usr/bin/env node
const split = require("lodash/split")
const first = require("lodash/first")
const colors = require("colors/safe")
const commander = require("commander")
const compact = require("lodash/compact")
const toNumber = require("lodash/toNumber")
const castArray = require("lodash/castArray")
const { version } = require("./package.json")
const HTML5ToPDF = require("./lib")
const printMissing = message => {
commander.outputHelp()
if (message) {
console.error(colors.red(message))
}
process.exit(1)
}
const die = error => {
if (!error) return process.exit(0)
console.error("Error:", error)
process.exit(1)
}
const getOptions = () => {
commander
.version(version)
.option(
"-i --include <path>..<path>",
"path to either a javascript asset, or a css asset",
)
.option("--page-size [size]", "'A3', 'A4', 'Legal', 'Letter' or 'Tabloid'")
.option(
"--landscape",
"If set it will change orientation to landscape from portriat",
)
.option("--print-background", "Whether to print CSS backgrounds")
.option(
"-t --template [template]",
"The template to used. Defaults to html5bp.",
)
.option(
"--template-path [/path/to/template/folder]",
"Specifies the template folder path for static assets, this will override template.",
)
.option(
"--template-url [http://localhost:8080]",
"Specifies the template url to use. Cannot be used with --template-path.",
)
.option(
"-d --render-delay [milli-seconds]",
"Delay before rendering the PDF (give HTML and CSS a chance to load)",
)
.option("-o --output <path>", "Path of where to save the PDF")
.usage("[options] <path/to/html-file-path>")
.parse(process.argv)
const inputPath = first(commander.args)
const outputPath = commander.output
if (inputPath == null && commander.templateUrl == null) {
printMissing("Missing input path first argument or template-url")
}
const {
pageSize,
template,
templatePath,
templateUrl,
renderDelay,
} = commander
const printBackground = commander.printBackground
const landscape = commander.landscape
const include = compact(castArray(split(commander.include, ",")))
return {
inputPath,
outputPath,
template,
templateUrl,
templatePath,
renderDelay: toNumber(renderDelay),
include,
options: {
landscape,
printBackground,
pageSize,
},
}
}
const run = async () => {
const options = getOptions()
const pdf = new HTML5ToPDF(options)
await pdf.start().catch(die)
const buf = await pdf.build().catch(die)
await pdf.close().catch(die)
if (buf != null) {
process.stdout.write(buf)
}
process.exit(0)
}
run()