diff --git a/README.md b/README.md index 3c1c73f..ba77b03 100644 --- a/README.md +++ b/README.md @@ -144,3 +144,8 @@ config = { The full options object gets converted to JSON and will get passed to the phantomjs script as third argument. There are more options concerning the paperSize, header & footer options inside the phantomjs script. + +### Command-line arguments example +``` +$ html-pdf test/businesscard.html businesscard.pdf --format=Letter --border.top=5mm --phantomArgs ignore-ssl-errors=yes +``` diff --git a/bin/index.js b/bin/index.js index d8b7661..f041447 100755 --- a/bin/index.js +++ b/bin/index.js @@ -3,18 +3,17 @@ var fs = require('fs') var pdf = require('../') var path = require('path') +var argv = require('yargs-parser')(process.argv.slice(2), {array: 'phantomArgs'}) -var args = process.argv.slice(2) - -if (args.length >= 2) { - htmlpdf(args[0], args[1]) +if (argv._.length >= 2) { + htmlpdf(argv._[0], argv._[1]) } else { help() } function help () { var help = [ - 'Usage: html-pdf ', + 'Usage: html-pdf [options]', 'e.g.: html-pdf source.html destination.pdf' ].join('\n') @@ -23,10 +22,9 @@ function help () { function htmlpdf (source, destination) { var html = fs.readFileSync(source, 'utf8') - var options = { - base: 'file://' + path.resolve(source) - } - pdf.create(html, options).toFile(destination, function (err, res) { + if (!argv.base) argv.base = 'file://' + path.resolve(source) + pdf.create(html, argv).toFile(destination, function (err, res) { if (err) throw err }) } + diff --git a/lib/pdf.js b/lib/pdf.js index 9195e7f..288f41e 100644 --- a/lib/pdf.js +++ b/lib/pdf.js @@ -35,6 +35,12 @@ function PDF (html, options) { if (this.options.filename) this.options.filename = path.resolve(this.options.filename) if (!this.options.phantomPath) this.options.phantomPath = phantomjs && phantomjs.path this.options.phantomArgs = this.options.phantomArgs || [] + this.options.phantomArgs = this.options.phantomArgs.map(function (elt) { + if (elt.substring(0, 2) !== '--') { + elt = '--' + elt + } + return elt + }) assert(this.options.phantomPath, "html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'") assert(typeof this.html === 'string' && this.html.length, "html-pdf: Can't create a pdf without an html string") this.options.timeout = parseInt(this.options.timeout) || 30000 diff --git a/package.json b/package.json index 51f4a55..e8fd918 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "tape": "^3.4.0" }, "optionalDependencies": { - "phantomjs-prebuilt": "^2.1.4" + "phantomjs-prebuilt": "^2.1.4", + "yargs-parser": "^3.2.0" }, "repository": { "type": "git",