andrew72ru/web-to-print

The package for generating PDF pages from HTML with headless Chrome and Chrome-driver

v1.0.0 2022-11-20 08:31 UTC

This package is auto-updated.

Last update: 2024-04-20 13:50:09 UTC


README

The package for generating PDF pages from HTML with headless Chrome and Chrome driver.

Requirements

This package requires PHP >= 8.0 with ctype and iconv extensions. You must also have installed Chrome (chromium) and cromedriver executables in your working environment.

Usage

First, create an instance of the Chrome Driver runner:

$logger = new \Psr\Log\NullLogger(); // Choose your Psr\Log\LoggerInterface implementation
$runner = new \Andrew72ru\Web2print\DriverRunner($logger, '/path/to/chromedriver');

After that, you can call the PrintToPdf class:

$printer = new \Andrew72ru\Web2print\PrintToPdf($runner, $logger);
$result = $printer('https://google.com', asBas64: true); // You can get Base64 or binary string
\file_put_contents('google.pdf', \base64_decode($result));

As a result, you will have a 'google.pdf' file in your working directory.

Default options

The PrintToPdf class has a set of default options to run the Chrome and the print command.

Chrome options

$defaultArguments = ['--headless', '--run-all-compositor-stages-before-draw'];

Print options

$defaultParams = [
    'displayHeaderFooter' => false,
    'printBackground' => true,
    'marginTop' => 0,
    'marginBottom' => 0,
    'marginLeft' => 0,
    'marginRight' => 0,
];

See references in Chrome manual.

Testing

Before tests, you must put the chromedriver executable into the project root.

Run tests:

vendor/bin/phpunit