ciltocruz / wkhtmltopdf-php-lib
HTML to PDF Converter based on wkhtmltopdf for PHP
Requires
- php: >=7.4
Requires (Dev)
- ekino/phpstan-banned-code: ^0.5.0
- friendsofphp/php-cs-fixer: ^3.0
- korbeil/phpstan-generic-rules: ^0.2.4
- php-mock/php-mock: ^2.3
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^0.12.96
- phpstan/phpstan-deprecation-rules: ^0.12.6
- phpstan/phpstan-phpunit: ^0.12.22
- phpunit/phpunit: ^9.5
README
HTML to PDF Converter based on wkhtmltopdf for PHP. This code is based on wkhtmltopdf-bindings.
This PHP library allows generating PDF Files from a url or a html page. It uses the excellent webkit-based wkhtmltopdf and wkhtmltoimage available on OSX, linux, windows.
This library contains windows and linux binaries. (0.12.6
version)
Linux binaries are based on the debian version. Verify that your system is compatible.
(N.B. These static binaries are extracted from Debian7 packages, so it might not be compatible with non-debian based linux distros)
If you detect problems you can open an issue, no problem.
Installation using Composer
$ composer require ciltocruz/wkhtmltopdf-php-lib
Usage
Initialization
<?php
require __DIR__ . '/vendor/autoload.php';
use Ciltocruz\WkhtmltopdfPhpLib\WkHtmlToPdf;
$wkhtmltopdf = new WkHtmlToPdf();
Download PDF as a file
$wkhtmltopdf = new WkHtmlToPdf();
$wkhtmltopdf->set_html("your_html");
$wkhtmltopdf->render();
$wkhtmltopdf->output(WkHtmlToPdf::$PDF_DOWNLOAD, $fileOutputName);
Generate local pdf file
$wkhtmltopdf = new WkHtmlToPdf();
$wkhtmltopdf->set_html("your_html");
$wkhtmltopdf->render();
$wkhtmltopdf->output(WkHtmlToPdf::$PDF_SAVEFILE, $fileOutputName);
You can use WkHtmlToPdf::$PDF_ASSTRING
for get the response as string.
Pass options to wkhtmlToPdf
// Type wkhtmltopdf -H to see the list of options
Use this wkhtmltopdf binary in other libraries
If you want to use wkhtmltopdf in other libraries you need to add these libraries in your composer.json
:
Some suggestions are:
$ composer require mikehaertl/phpwkhtmltopdf
$ composer require knplabs/knp-snappy
And then you can use it setting the correspondant binary
<?php
use Knp\Snappy\Pdf;
$myProjectDirectory = '/path/to/my/project';
$snappy = new Pdf($myProjectDirectory . '/vendor/bin/wkhtmltopdf');
Note: You must use the version that corresponds to you.
wkhtmltopdf
for unix systems
wkhtmltopdf64.exe
for windows.
Maintainers
If you are interested in maintaining, feel free to open a PR. This library is maintained by the following people (alphabetically sorted) :
@ciltocruz
Credits
This library has been originally developed by ciltocruz.