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

Bump lodash and standard #1

Open
wants to merge 17 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
10 changes: 10 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2.1

orbs:
node: circleci/[email protected]

workflows:
matrix-tests:
jobs:
- node/test:
version: 15.3.0
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# node-html-pdf
## HTML to PDF converter that uses phantomjs
![image](examples/businesscard.png)
[Example Business Card](examples/businesscard.pdf)
-> [and its Source file](examples/businesscard.html)
![image](examples/businesscard/businesscard.png)
[Example Business Card](examples/businesscard/businesscard.pdf)
-> [and its Source file](examples/businesscard/businesscard.html)

[Example Receipt](http://imgr-static.s3-eu-west-1.amazonaws.com/order.pdf)

Expand Down Expand Up @@ -119,20 +119,22 @@ config = {


// Rendering options
"base": "file:///home/www/your-asset-path", // Base path that's used to load files (images, css, js) when they aren't referenced using a host
"base": "file:///home/www/your-asset-path/", // Base path that's used to load files (images, css, js) when they aren't referenced using a host

// Zooming option, can be used to scale images if `options.type` is not pdf
"zoomFactor": "1", // default is 1

// File options
"type": "pdf", // allowed file types: png, jpeg, pdf
"quality": "75", // only used for types png & jpeg
"type": "pdf", // allowed file types: png, jpeg, pdf
"quality": "75", // only used for types png & jpeg

// Script options
"phantomPath": "./node_modules/phantomjs/bin/phantomjs", // PhantomJS binary which should get downloaded automatically
"phantomArgs": [], // array of strings used as phantomjs args e.g. ["--ignore-ssl-errors=yes"]
"script": '/url', // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
"timeout": 30000, // Timeout that will cancel phantomjs, in milliseconds
"localUrlAccess": false, // Prevent local file:// access by passing '--local-url-access=false' to phantomjs
// For security reasons you should keep the default value if you render arbritary html/js.
"script": '/url', // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
"timeout": 30000, // Timeout that will cancel phantomjs, in milliseconds

// Time we should wait after window load
// accepted values are 'manual', some delay in milliseconds or undefined to wait for a render event
Expand Down
3 changes: 0 additions & 3 deletions circle.yml

This file was deleted.

Binary file added examples/businesscard/businesscard.pdf
Binary file not shown.
28 changes: 28 additions & 0 deletions examples/businesscard/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var test = require('tape')
var pdf = require('../../')
var path = require('path')
var fs = require('fs')

test('allows custom html and css', function (t) {
t.plan(3)

var template = path.join(__dirname, 'businesscard.html')
var filename = template.replace('.html', '.pdf')
var templateHtml = fs.readFileSync(template, 'utf8')

var image = path.join('file://', __dirname, 'image.png')
templateHtml = templateHtml.replace('{{image}}', image)

var options = {
width: '50mm',
height: '90mm'
}

pdf
.create(templateHtml, options)
.toFile(filename, function (err, pdf) {
t.error(err)
t.assert(pdf.filename, 'Returns the filename')
t.assert(fs.existsSync(pdf.filename), 'Saves the file to the desired destination')
})
})
27 changes: 21 additions & 6 deletions lib/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ 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 || []

if (!this.options.localUrlAccess) this.options.phantomArgs.push('--local-url-access=false')
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, 10) || 30000
Expand Down Expand Up @@ -117,13 +119,26 @@ PDF.prototype.exec = function PdfExec (callback) {
// If we don't have an exit code, we kill the process, ignore stderr after this point
if (code === null) kill(child, onData, onError)

if (!data) {
if (!err && code) err = new Error("html-pdf: Received the exit code '" + code + "'")
else if (!err) err = new Error('html-pdf: Unknown Error')

// Since code has a truthy/falsy value of either 0 or 1, check for existence first.
// Ignore if code has a value of 0 since that means PhantomJS has executed and exited successfully.
// Also, as per your script and standards, having a code value of 1 means one can always assume that
// an error occured.
if (((typeof code !== 'undefined' && code !== null) && code !== 0) || err) {
var error = null

if (err) {
// Rudimentary checking if err is an instance of the Error class
error = err instanceof Error ? err : new Error(err)
} else {
// This is to catch the edge case of having a exit code value of 1 but having no error
error = new Error('html-pdf: Unknown Error')
}

// Append anything caught from the stderr
var postfix = stderr.length ? '\n' + Buffer.concat(stderr).toString() : ''
if (postfix) err.message += postfix
return callback(err, null)
if (postfix) error.message += postfix

return callback(error)
}

callback(null, data)
Expand Down
2 changes: 1 addition & 1 deletion lib/scripts/pdf_a4_portrait.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function createSection (section, content, options) {
var numPagesFinal = numPages + paginationOffset

if (pageNumFinal === 1 && !html) html = o.first || c.first
if (numPagesFinal === numPages && !html) html = o.last || c.last
if (pageNumFinal === numPages && !html) html = o.last || c.last
return (html || o.default || c.default || '')
.replace(/{{page}}/g, pageNumFinal)
.replace(/{{pages}}/g, numPagesFinal) + content.styles
Expand Down
Loading