Skip to content

Commit

Permalink
Updated to ES6, updated dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritix committed Jun 5, 2020
1 parent f4c251b commit 81b7012
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

## [1.5.0] - 2020-06-05
### Changed
- Updated all dependencies to the latest versions
- Refactored JS code to ES6

## [1.4.1] - 2019-11-11
### Fixed
- NPM warning for invalid version
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ and all the other fancy stuff people use these days.
## Requirements

- PHP 7.0+ with enabled program execution functions (proc_open) and 'fopen wrappers'
- Node.js 6.4+
- Node.js 10.18.1+ (for older Node.js versions use [v.1.4.1](https://github.com/spiritix/php-chrome-html2pdf/releases/tag/v1.4.1))
- A few [OS specific dependencies](https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md)

## Installation
Expand Down
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ const getStdin = require('get-stdin');
const program = require('commander');
const Converter = require('./lib/Converter');

(() => {
(async () => {
program
.option('-o, --options [options]', 'PDF options for puppeteer')
.parse(process.argv);

const options = JSON.parse(program.options);

getStdin().then(html => {
const converter = new Converter(html, options);
const converter = new Converter(await getStdin(), options);
const buffer = await converter.run();

converter.run().then((buffer) => {
process.stdout.write(buffer.toString('binary'), 'binary');
});
});
process.stdout.write(buffer.toString('binary'), 'binary');
})();
55 changes: 24 additions & 31 deletions lib/Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,81 +30,74 @@ class Converter
return Object.assign({}, defaultOptions, this.options);
}

run()
async run()
{
return new Promise((resolve) => {
this._launchBrowser().then((browser) => {
const browser = await this._launchBrowser();
const page = await this._initPage(browser);
const buffer = await this._convert(page);

this._initPage(browser)
.then((page) => this._convert(page))
.then((buffer) => {
await this._close(browser);

this._close(browser);
resolve(buffer);
});
});
});
return buffer;
}

_launchBrowser()
async _launchBrowser()
{
return puppeteer.launch({
ignoreHTTPSErrors: true,
args: ['--no-sandbox', '--disable-web-security']
});
}

_initPage(browser)
async _initPage(browser)
{
return browser.newPage();
}

_convert(page)
async _convert(page)
{
let options = this.getOptions();

if (options.hasOwnProperty('mediaType')) {
page.emulateMedia(options.mediaType);
await page.emulateMedia(options.mediaType);
delete options.mediaType;
}

if (options.hasOwnProperty('viewport')) {
page.setViewport(options.viewport);
await page.setViewport(options.viewport);
delete options.viewport;
}

if (options.hasOwnProperty('cookies')) {
page.setCookie(...options.cookies);
await page.setCookie(...options.cookies);
delete options.cookies;
}

return this._setHtml(page, this.html)
.then(() => {
if (options.hasOwnProperty('pageWaitFor')) {
const timeout = parseInt(options.pageWaitFor);
delete options.pageWaitFor;
return page.waitFor(timeout).then(() => this._getPdf(page, options));
} else {
return this._getPdf(page, options)
}
});
await this._setHtml(page, this.html);

if (options.hasOwnProperty('pageWaitFor')) {
await page.waitFor(parseInt(options.pageWaitFor));
delete options.pageWaitFor;
}

return this._getPdf(page, options)
}

_setHtml(page, html)
async _setHtml(page, html)
{
return page.setContent(html, {
waitUntil: ['load', 'networkidle0']
});
}

_getPdf(page, options)
async _getPdf(page, options)
{
return page.pdf(options);
}

_close(browser)
async _close(browser)
{
browser.close();
return browser.close();
}
}

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "php-chrome-html2pdf",
"version": "1.4.1",
"version": "1.5.0",
"description": "A PHP library for converting HTML to PDF using Google Chrome.",
"main": "index.js",
"directories": {
"test": "tests/js"
},
"engines": {
"node": ">=6.4.0"
"node": ">=10.18.1"
},
"dependencies": {
"commander": "^2.12.2",
"get-stdin": "^6.0.0",
"puppeteer": "^1.12.2"
"puppeteer": "^3.3.0"
},
"devDependencies": {
"chai": "^4.1.2",
"codeclimate-test-reporter": "^0.5.0",
"mocha": "^4.1.0"
"chai": "^4.2.0",
"codeclimate-test-reporter": "^0.5.1",
"mocha": "^7.2.0"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha --timeout=10000 tests/js"
Expand Down
2 changes: 1 addition & 1 deletion tests/js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Converter', () => {
});

describe('run', () => {
it('returns a buffer', async () => {
it('returns a buffer', async (done) => {
const converter = new Converter('<p>Hello</p>', {});
expect(await converter.run()).to.be.instanceof(Buffer);
});
Expand Down

0 comments on commit 81b7012

Please sign in to comment.