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

[WIP] Puppeteer backend #285

Open
wants to merge 8 commits into
base: 2.0
from

Conversation

@barryvdh
Copy link
Contributor

commented Oct 11, 2017

This isn't much tested yet, more a proof-of-concept.

Uses https://github.com/GoogleChrome/puppeteer (see #284 )

Installation:
Requires Node 6.4+ or 7+

sudo apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
sudo npm install --global --unsafe-perm puppeteer
sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium

(Chrome is installed by Puppeteer by default, but needs to be executable. There also is an option to use your own Chrome, but could be out sync)

Example

require __DIR__ . '/../vendor/autoload.php';

$snappy = new \Knp\Snappy\Puppeteer\Pdf();

$result = $snappy->getOutput('http://www.github.com', ['format' => 'a6']);
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="file.pdf"');
echo $result;

$snappy->generate('http://www.github.com', 'github.pdf', [], true);

$snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/test.pdf');

Not all options are configured/tested yet, but should provide some more options then the CLI.

Only thing I saw is that all HTML is now passed through the commandline? Not sure if that has limitations. Also the chrome generator added file:// in front of the file, which doesn't work for urls.

The script is based on https://github.com/spatie/browsershot/tree/v3 but extended for more options and modified for Node6

barryvdh added some commits Oct 11, 2017

CS

@barryvdh barryvdh force-pushed the barryvdh:v2-puppeteer branch from 80544ec to 26715ea Oct 11, 2017

@barryvdh

This comment has been minimized.

Copy link
Contributor Author

commented Oct 11, 2017

To be fair, this does seem to be slower . Not tested with large ones, but wkhtmltopdf + chrome cli are 300 ms for just the '<h1>Bill</h1><p>You owe me money, dude.</p>' html, and puppeteer is around 600. But I suspect this is mainly initial overhead for the node script. And this doesn't have the title above the pdf for example. So flexibility over performance for this.

@barryvdh barryvdh force-pushed the barryvdh:v2-puppeteer branch 4 times, most recently from e5ed754 to ac28058 Oct 11, 2017

@barryvdh

This comment has been minimized.

Copy link
Contributor Author

commented Oct 11, 2017

Simplified it a bit, should also work without installing it globally, by passing the directory in which it is installed (eg. the root of the project).

$image = new \Knp\Snappy\Puppeteer\Image(['type' => 'png'],  __DIR__ .'/..');

@barryvdh barryvdh force-pushed the barryvdh:v2-puppeteer branch from ac28058 to 6739eeb Oct 11, 2017

barryvdh added some commits Oct 11, 2017

@barryvdh barryvdh force-pushed the barryvdh:v2-puppeteer branch from dd66e3d to e3b2f90 Oct 12, 2017

@barryvdh barryvdh force-pushed the barryvdh:v2-puppeteer branch from 12f030c to 3ba8baf Oct 12, 2017

@barryvdh

This comment has been minimized.

Copy link
Contributor Author

commented Oct 12, 2017

Add validation for the currently supported types.

So is this something your interested in? Otherwise I'll create a standalone package or something.

return implode(' ', [
'NODE_PATH=' . $nodePath,
'node',
escapeshellarg(dirname(dirname(dirname(dirname(__DIR__)))) . '//resources/puppeteer.js'),

This comment has been minimized.

Copy link
@NiR-

NiR- Oct 21, 2017

Member

Why don't you use the more common ../../../../? Due to windows?

This comment has been minimized.

Copy link
@NiR-

NiR- Dec 3, 2017

Member

Could you do only one call to dirname instead please?

@barryvdh

This comment has been minimized.

Copy link
Contributor Author

commented Oct 21, 2017

No special reason, just a shorter command path

'viewport' => null,
];
public function __construct($nodePath = null, array $options = [], array $env = [])

This comment has been minimized.

Copy link
@NiR-

NiR- Dec 3, 2017

Member

Could you add type string on $nodePath please?

@@ -0,0 +1,74 @@
#!/usr/bin/env node
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

This comment has been minimized.

Copy link
@NiR-

NiR- Dec 3, 2017

Member

IMHO, in order to provide compatibility with every version of NodeJS, we should either use plain ES5 code or use ESnext and transpile it to ES5 at commit-time, but in any case we shouldn't have to write and maintain this sort of function ourself.

return implode(' ', [
'NODE_PATH=' . $nodePath,
'node',
escapeshellarg(dirname(dirname(dirname(dirname(__DIR__)))) . '//resources/puppeteer.js'),

This comment has been minimized.

Copy link
@NiR-

NiR- Dec 3, 2017

Member

Could you do only one call to dirname instead please?

@NiR-

This comment has been minimized.

Copy link
Member

commented Dec 3, 2017

Could you fix my last comments and the little CS issues please? After that, I'd be happy to merge this PR :)

@Bavragor

This comment has been minimized.

Copy link

commented Jun 28, 2018

Maybe this could help https://github.com/nesk/puphpeteer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.