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
Requires
- php: >=8.0
- ext-ctype: *
- ext-iconv: *
- php-webdriver/webdriver: ^1.13
- psr/log: ^2.0
- symfony/http-client: ^5.4
- symfony/mime: ^5.4
- symfony/process: ^5.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.4
- vimeo/psalm: ^4.30
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