forked from elliotblackburn/mdpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·151 lines (137 loc) · 4.66 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
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const meow = require('meow');
const mdpdf = require('../');
const cli = meow(
`
Usage:
$ mdpdf <source> [<destination>] [options]
<source> must be a markdown file, with the extension '.md'.
Examples:
$ mdpdf README.md
$ mdpdf README.md --style=styles.css --header=header.hbs --h-height=22mm
$ mdpdf README.md --footer=footer.hbs --f-height=22mm --debug
$ mdpdf README.md --border-left=30mm
Options:
--style=<filename> A single css stylesheet you wish to apply to the PDF
--header=<filename> A HTML (.html) file to inject into the header of the PDF
--h-height=<height> The height of the header section
--footer=<filename> A HTML (.html) file to inject into the footer of the PDF
--f-height=<height> The height of the footer section
--border=<size> Border (top, left, bottom, right; default: 20mm)
--border-top=<size> Top border (default: 20mm)
--border-left=<size> Left border (default: 20mm)
--border-bottom=<size> Bottom border (default: 20mm)
--border-right=<size> Right border (default: 20mm)
--no-emoji Disables emoji conversions
--debug Save the generated html for debugging
--help Display this menu
--version Display the application version
--format=<format> PDF size format: A3, A4, A5, Legal, Letter, Tabloid (Default: A4)
--orientation=<orientation> PDF orientation: portrait or landscape (Default: portrait)
Length parameters (<height> and <size>) require a unit. Valid units are mm, cm, in and px.
Global Settings:
You can also set a global default stylesheet by setting the MDPDF_STYLES environment
variable as the path to your single css stylesheet. The --style flag will override this.
`,
{
alias: {
s: 'style',
h: 'header',
f: 'footer',
d: 'debug',
v: 'version',
r: 'format',
o: 'orientation',
},
}
);
function isMd(path) {
if (!path) {
return true;
}
const accepted = ['md'];
const current = path.split('.').pop();
if (accepted.indexOf(current) !== -1) {
return true;
}
return false;
}
const source = cli.input[0];
if (!source || !isMd(source)) {
// Invalid source, show help and exit
cli.showHelp();
}
const destination =
cli.input[1] || source.slice(0, source.indexOf('.md')) + '.pdf';
const debug = cli.flags.debug || false;
let style = cli.flags.style;
const header = cli.flags.header;
const headerHeight = cli.flags.hHeight;
const footer = cli.flags.footer;
const footerHeight = cli.flags.fHeight;
const border = cli.flags.border || '20mm';
const borderTop = cli.flags.borderTop || border;
const borderLeft = cli.flags.borderLeft || border;
const borderBottom = cli.flags.borderBottom || border;
const borderRight = cli.flags.borderRight || border;
const pdfFormat = cli.flags.format || 'A4';
const pdfOrientation = cli.flags.orientation || 'portrait';
// Name of the environement variable
const envStyleName = 'MDPDF_STYLES';
// If styles have not been provided through the CLI flag, but the environment variable exists
if (!style && process.env[envStyleName]) {
// Ensure the css file exists
const envCssPath = path.resolve(process.env[envStyleName]);
if (fs.existsSync(envCssPath)) {
style = envCssPath;
}
}
const options = {
ghStyle: !style,
defaultStyle: true,
source: path.resolve(source),
destination: path.resolve(destination),
styles: style ? path.resolve(style) : null,
header: header ? path.resolve(header) : null,
footer: footer ? path.resolve(footer) : null,
noEmoji: cli.flags.noEmoji || false,
debug: debug
? path.resolve(source.slice(0, source.indexOf('.md')) + '.html')
: null,
pdf: {
format: pdfFormat,
orientation: pdfOrientation,
quality: '100',
base: path.join('file://', __dirname, '/assets/'),
header: {
height: headerHeight || null,
},
footer: {
height: footerHeight || null,
},
border: {
top: borderTop,
left: borderLeft,
bottom: borderBottom,
right: borderRight,
},
},
};
return mdpdf
.convert(options)
.then(pdfPath => {
// Pretty print for terminals, or just return the output
// path for scripts and pipes.
if (process.stdout.isTTY) {
console.log('✨ PDF created successfully at:', pdfPath);
} else {
console.log(pdfPath);
}
})
.catch(err => {
console.error(err);
process.exitCode = 1;
});