Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI arguments #183

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
16 changes: 7 additions & 9 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <source> <destination>',
'Usage: html-pdf <source> <destination> [options]',
'e.g.: html-pdf source.html destination.pdf'
].join('\n')

Expand All @@ -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
})
}

6 changes: 6 additions & 0 deletions lib/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) !== '--') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO that's too much magic. I'd support --phantomArgs "--ignore-ssl-errors=yes". Then you'll also get a string in here and we can just split it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you, it would be better but unfortunately it does not work like this.
The double quotes are not kept and yargs-parser consider it as a normal argument.

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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down