1 | _ = require 'lodash'
|
2 | path = require 'path'
|
3 | commander = require 'commander'
|
4 | colors = require 'colors/safe'
|
5 |
|
6 | HTMLToPDF = require './'
|
7 | packageJSON = require '../package.json'
|
8 |
|
9 | class Command
|
10 | printMissing: (message) =>
|
11 | commander.outputHelp()
|
12 | console.error colors.red message if message?
|
13 | process.exit 1
|
14 |
|
15 | run: =>
|
16 | commander
|
17 | .version packageJSON.version
|
18 | .option '-i --include <path>..<path>',
|
19 | 'path to either a javascript asset, or a css asset'
|
20 | .option '--page-size [size]',
|
21 | "'A3', 'A4', 'Legal', 'Letter' or 'Tabloid'"
|
22 | .option '--margin-type [n]',
|
23 | 'Specify the type of margins to use: 0 - default, 1 - none, 2 - minimum'
|
24 | .option '--landscape',
|
25 | 'If set it will change orientation to landscape from portriat'
|
26 | .option '--print-background',
|
27 | 'Whether to print CSS backgrounds'
|
28 | .option '-t --template [template]',
|
29 | 'The template to used. Defaults to html5bp.'
|
30 | .option '--template-path [/path/to/template/folder]',
|
31 | 'Specifies the template folder path for static assets, this will override template.'
|
32 | .option '-d --render-delay [milli-seconds]',
|
33 | 'Delay before rendering the PDF (give HTML and CSS a chance to load)'
|
34 | .option '-o --output <path>',
|
35 | 'Path of where to save the PDF'
|
36 | .usage '[options] <path/to/html-file-path>'
|
37 | .parse process.argv
|
38 |
|
39 | inputPath = _.first commander.args
|
40 | outputPath = commander.output
|
41 | @printMissing 'Missing input path first argument' unless inputPath?
|
42 |
|
43 | {
|
44 | pageSize,
|
45 | template,
|
46 | templatePath,
|
47 | renderDelay,
|
48 | marginType
|
49 | } = commander
|
50 | printBackground = commander.printBackground?
|
51 | landscape = commander.landscape?
|
52 |
|
53 | includeAssets = _.compact _.castArray(commander.include?.split(','))
|
54 | include = _.map includeAssets, (filePath) =>
|
55 | type = path.extname(filePath).replace('.', '')
|
56 | return @printMissing 'Invalid asset path' unless type in [ 'css', 'js' ]
|
57 | return { type, filePath }
|
58 |
|
59 | options = {
|
60 | inputPath,
|
61 | outputPath,
|
62 | template,
|
63 | templatePath,
|
64 | renderDelay,
|
65 | include,
|
66 | options: {
|
67 | landscape,
|
68 | printBackground,
|
69 | pageSize,
|
70 | marginType,
|
71 | }
|
72 | }
|
73 |
|
74 | new HTMLToPDF(options).build (error, buf) =>
|
75 | return @die error if error?
|
76 | process.stdout.write buf if buf?
|
77 | process.exit(0)
|
78 |
|
79 | die: (error) =>
|
80 | return process.exit(0) unless error?
|
81 | console.log 'Error:', error
|
82 | process.exit 1
|
83 |
|
84 | module.exports = Command
|