A custom PHP library to generate PDFs and screenshots (WiP) from URLs, static HTML, or dynamic PHP files using Headless Chrome powered by Spatie’s Browsershot, Node.js and Puppeteer. Works in Yii2 framework.
To use this library, ensure you have the following:
- PHP >= 8.1
- Composer (PHP dependency manager)
- Node.js >= 14
- Google Chrome or Chromium (or any chromium based browser ie: Edge, Brave)
- Puppeteer (Node.js library for controlling Headless Chrome)
-
Install the Library via Composer
Run the following command in your project directory:composer require hemike1/browsershot-yii2
-
Install Node.js for npm
The next step requires npm for Puppeteer.- On Linux: use terminal to fetch the latest package.
sudo apt-get install nodejs npm # after suc 8000 cessful install, check version with: node -v # version should be >= 14.
- On Windows: you can download a prebuilt installer from nodejs.org
-
Install Puppeteer
Browsershot relies on Puppeteer to run Headless Chrome. Install it via npm in the project repository:sudo apt-get install chromium-browser sudo apt-get install libx11-xcb1 libxcomposite1 libasound2 libatk1.0-0 libatk-bridge2.0-0 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 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 # fetch the headless chrome for the puppeteer, with all of its libraries. sudo npm install -g puppeteer cd /usr/lib/node_modules/puppeteer npm run postinstall # Install puppeteer globally and run the post install. # if issues persist with chromium, use the following command and repeat the postinstall. sudo npm install puppeteer --unsafe-perm=true --allow-root
Node modules might not detect the globally installed packages on windows, so we'll need a workaround.
npm install -g puppeteer # Install puppeteer cd %APPDATA%\npm\nodejs\node_modules\puppeteer # Find puppeteer folder npm run postinstall # Post install to ensure proper proper operation # In your project's folder, you'll need to link the Browsershot/bin dir to puppetteer cd ./vendor/spatie/browsershot/bin # or cd ./vendor/hemike1/browsershot-yii2/bin npm init -y npm link puppeteer # Like so.
-
Verify Chrome/Chromium Installation
Ensure Google Chrome or Chromium is installed on your system:- On Linux: Install Chromium:
sudo apt-get update sudo apt-get install -y chromium-browser
- On macOS: Download Chrome from the official website.
- on Windows:
- Either Edge is already preinstalled, you can use that in
(C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe) - Or you already have Google Chrome installed.
- Either Edge is already preinstalled, you can use that in
use hemike1\Browsershot\Browsershot;
$output_path = __DIR__ . '/example.pdf';
Browsershot::createPdfFromUrl('https://example.com/site/view-pdf?id=15', $output_path);
echo "PDF saved to: $outputPath";
//or
$pdf = Browsershot::createPdfFromUrl('https://example.com/site/viewpdf?id=15', $output_path);
echo "PDF saved to: $pdf"; //Returns the output path if ran successfully.
Suppose you have a file example.php
:
example.php:
<!DOCTYPE html>
<html>
<body>
<h1>Hello, <?php echo "World"; ?></h1>
<p>Current Time: <?php echo date('Y-m-d H:i:s'); ?></p>
<p> <?= $lorem_ipsum ?> </p>
</body>
</html>
You can render it and save it as a PDF:
$output_path = __DIR__ . '/php-output.pdf';
Browsershot::createPdfFromHtml(__DIR__ . '/example.php', $output_path, [
'lorem_ipsum' => $content,
]);
echo "PDF saved to: $outputPath"; // Will also return output_path
The library uses default paths for Node.js and Chrome:
(.exe paths on windows)
- Node.js binary:
/usr/bin/node
- Chrome binary:
/usr/bin/google-chrome
To customize paths, update the static properties in your code:
(preferably in the index.php if the given application)
Browsershot::$nodeBinary = '/custom/path/to/node';
Browsershot::$chromePath = '/custom/path/to/chrome';
If you’re running this library in a restricted environment (e.g., shared hosting, Docker containers), you might need to disable Chrome’s sandbox mode:
sudo npm install puppeteer --unsafe-perm=true --allow-root
Add --no-sandbox
when running Puppeteer in Browsershot:
Browsershot::url('https://example.com')
->setOption('args', ['--no-sandbox'])
->pdf()
->save('/path/to/file.pdf');
Ensure Chrome/Chromium is installed and accessible via the system path.
- Verify with:
which google-chrome
This library is released under the MIT License.
- Built on top of Spatie's Browsershot.
- Headless Chrome powered by Puppeteer.