Skip to content

Commit

Permalink
Release 1.8.0, add scale checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Szpadel committed Sep 26, 2018
1 parent b82ebc8 commit e18df40
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
This tool require Google Chrome installed in version >59 to work,
but latest version is recommended.

# Usage:
```
Expand All @@ -19,14 +17,15 @@ chrome-headless-render-pdf [OPTIONS] --url=URL --pdf=OUTPUT-FILE [--url=URL2 --p
--paper-width specify page width in inches (defaults to 8.5 inches)
--paper-height specify page height in inches (defaults to 11 inches)
--page-ranges specify pages to render default all pages, e.g. 1-5, 8, 11-13
--scale specify scale of the webpage rendering (defaults to 1)
Example:
Render single pdf file
chrome-headless-render-pdf --url http://google.com --pdf test.pdf
Render pdf from local file
chrome-headless-render-pdf --url file:///tmp/example.html --pdf test.pdf
Render multiple pdf files
chrome-headless-render-pdf --url http://google.com --pdf test.pdf --url file:///tmp/example.html --pdf test.pdf
chrome-headless-render-pdf --url http://google.com --pdf test.pdf --url file:///tmp/example.html --pdf test2.pdf
```

# This tool can be also used programmatically:
Expand Down
10 changes: 7 additions & 3 deletions cli/chrome-headless-render-pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const argv = require('minimist')(process.argv.slice(2), {
boolean: [
'no-margins',
'include-background',
'landscape '
'landscape'
]
});

Expand Down Expand Up @@ -110,7 +110,11 @@ if(typeof argv['page-ranges'] === 'string') {

let scale;
if(typeof argv['scale'] === 'string') {
scale = argv['scale'];
scale = Number(argv['scale']);
if(isNaN(scale)) {
console.error('--scale must be a number');
process.exit(1);
}
}

(async () => {
Expand Down Expand Up @@ -175,5 +179,5 @@ function printHelp() {
console.log(' Render pdf from local file');
console.log(' chrome-headless-render-pdf --url file:///tmp/example.html --pdf test.pdf');
console.log(' Render multiple pdf files');
console.log(' chrome-headless-render-pdf --url http://google.com --pdf test.pdf --url file:///tmp/example.html --pdf test.pdf');
console.log(' chrome-headless-render-pdf --url http://google.com --pdf test.pdf --url file:///tmp/example.html --pdf test2.pdf');
}
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@ class RenderPDF {
}

if(this.options.scale !== undefined) {
options.scale = this.options.scale;
let scale = this.options.scale;
if(scale < 0.1) {
console.warn(`scale cannot be lower than 0.1, using 0.1`);
scale = 0.1;
}
if(scale > 2) {
console.warn(`scale cannot be higher than 2, using 2`);
scale = 2;
}
options.scale = scale;
}

return options;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrome-headless-render-pdf",
"version": "1.7.2",
"version": "1.8.0",
"main": "index.js",
"types": "index.d.ts",
"license": "MIT",
Expand Down

0 comments on commit e18df40

Please sign in to comment.