html-pdf-node
Advanced tools
Comparing version 1.0.5 to 1.0.6
35
index.js
@@ -8,7 +8,13 @@ const puppeteer = require('puppeteer'); | ||
// we are using headless mode | ||
let args = [ | ||
'--no-sandbox', | ||
'--disable-setuid-sandbox', | ||
]; | ||
if(options.args) { | ||
args = options.args; | ||
delete options.args; | ||
} | ||
const browser = await puppeteer.launch({ | ||
args: [ | ||
'--no-sandbox', | ||
'--disable-setuid-sandbox', | ||
], | ||
args: args | ||
}); | ||
@@ -27,3 +33,5 @@ const page = await browser.newPage(); | ||
} else { | ||
await page.goto(file.url); | ||
await page.goto(file.url, { | ||
waitUntil: 'networkidle0', // wait for page to load completely | ||
}); | ||
} | ||
@@ -41,7 +49,12 @@ | ||
// we are using headless mode | ||
let args = [ | ||
'--no-sandbox', | ||
'--disable-setuid-sandbox', | ||
]; | ||
if(options.args) { | ||
args = options.args; | ||
delete options.args; | ||
} | ||
const browser = await puppeteer.launch({ | ||
args: [ | ||
'--no-sandbox', | ||
'--disable-setuid-sandbox', | ||
], | ||
args: args | ||
}); | ||
@@ -60,3 +73,5 @@ let pdfs = []; | ||
} else { | ||
await page.goto(file.url); | ||
await page.goto(file.url, { | ||
waitUntil: 'networkidle0', // wait for page to load completely | ||
}); | ||
} | ||
@@ -63,0 +78,0 @@ let pdfObj = JSON.parse(JSON.stringify(file)); |
{ | ||
"name": "html-pdf-node", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Convert any html content or html page to PDF", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,1 @@ | ||
# html-pdf-node | ||
@@ -25,2 +24,5 @@ | ||
let options = { format: 'A4' }; | ||
// Example of options with args // | ||
// let options = { format: 'A4', args: ['--no-sandbox', '--disable-setuid-sandbox'] }; | ||
let file = { content: "<h1>Welcome to html-pdf-node</h1>" }; | ||
@@ -45,2 +47,3 @@ // or // | ||
- `args` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/). This options will overwrite the default arguments. The default arguments are `['--no-sandbox', '--disable-setuid-sandbox']`. | ||
- `path` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). If no path is provided, the PDF won't be saved to the disk. | ||
@@ -72,1 +75,50 @@ - `scale` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2. | ||
Promise which resolves with PDF buffer. | ||
### html_to_pdf.generatePdfs ( [files], [options], [callback] ) | ||
**Parameters** | ||
`files` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>> File object should have one of the follwing properties: | ||
- `url` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Any public url for the PDF content . | ||
- `content`<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Content of the HTML file for the PDF content. | ||
`options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> Options object which might have the following properties: | ||
- `args` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/). This options will overwrite the default arguments. The default arguments are `['--no-sandbox', '--disable-setuid-sandbox']`. | ||
- `path` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). If no path is provided, the PDF won't be saved to the disk. | ||
- `scale` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2. | ||
- `displayHeaderFooter` <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Display header and footer. Defaults to `false`. | ||
- `headerTemplate` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: | ||
- `date` formatted print date | ||
- `title` document title | ||
- `url` document location | ||
- `pageNumber` current page number | ||
- `totalPages` total pages in the document | ||
- `footerTemplate` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML template for the print footer. Should use the same format as the `headerTemplate`. | ||
- `printBackground` <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Print background graphics. Defaults to `false`. | ||
- `landscape` <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Paper orientation. Defaults to `false`. | ||
- `pageRanges` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages. | ||
- `format` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Paper format. If set, takes priority over `width` or `height` options. Defaults to 'Letter'. | ||
- `width` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Paper width, accepts values labeled with units. | ||
- `height` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Paper height, accepts values labeled with units. | ||
- `margin` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> Paper margins, defaults to none. | ||
- `top` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Top margin, accepts values labeled with units. | ||
- `right` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Right margin, accepts values labeled with units. | ||
- `bottom` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Bottom margin, accepts values labeled with units. | ||
- `left` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Left margin, accepts values labeled with units. | ||
- `preferCSSPageSize` <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size. | ||
**Return value** | ||
Promise which resolves with array of object which contains file objects with PDF buffers. | ||
***Example:*** | ||
```js | ||
let options = { format: 'A4' }; | ||
let file = [{ url: "https://example.com", name: 'example.pdf' }]; | ||
html_to_pdf.generatePdfs(file, options).then(output => { | ||
console.log("PDF Buffer:-", output); // PDF Buffer:- [{url: "https://example.com", name: "example.pdf", buffer: <PDF buffer>}] | ||
}); | ||
``` |
@@ -35,2 +35,47 @@ 'use strict'; | ||
}); | ||
it('convert-html-to-pdf-with-args', function(done) { | ||
html.generatePdf({ url: 'https://www.google.com/' }, { format: 'A4', args: ['--no-sandbox'] }, function(err, result) { | ||
expect(err).to.be.null; | ||
expect(result).to.be.an.instanceOf(Buffer); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('convert-array-of-htmls-to-pdfs', function() { | ||
it('returns promise', function() { | ||
let promise = html.generatePdfs([{ content: file }], { format: 'A4' }); | ||
expect(promise.then).to.be.an.instanceof(Function); | ||
}); | ||
it('convert-html-to-pdf-with-callback', function(done) { | ||
html.generatePdfs([{ content: file }], { format: 'A4' }, function(err, result) { | ||
expect(err).to.be.null; | ||
expect(result).to.be.an.instanceOf(Array); | ||
expect(result[0]).to.be.an.instanceOf(Object); | ||
expect(result[0].buffer).to.be.an.instanceOf(Buffer); | ||
done(); | ||
}); | ||
}); | ||
it('convert-html-to-pdf-with-url', function(done) { | ||
html.generatePdfs([{ url: 'https://www.google.com/' }], { format: 'A4' }, function(err, result) { | ||
expect(err).to.be.null; | ||
expect(result).to.be.an.instanceOf(Array); | ||
expect(result[0]).to.be.an.instanceOf(Object); | ||
expect(result[0].buffer).to.be.an.instanceOf(Buffer); | ||
done(); | ||
}); | ||
}); | ||
it('convert-html-to-pdf-with-args', function(done) { | ||
html.generatePdfs([{ url: 'https://www.google.com/' }], { format: 'A4', args: ['--no-sandbox'] }, function(err, result) { | ||
expect(err).to.be.null; | ||
expect(result).to.be.an.instanceOf(Array); | ||
expect(result[0]).to.be.an.instanceOf(Object); | ||
expect(result[0].buffer).to.be.an.instanceOf(Buffer); | ||
done(); | ||
}); | ||
}); | ||
}); |
56271
150
122