ivanpepelko / php-wkhtmltopdf
wkhtmltopdf php bindings
Installs: 2 187
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^5.4 | ^7.0
- guzzlehttp/guzzle: ^6.2
- symfony/css-selector: ^3.1
- symfony/dom-crawler: ^3.1
- symfony/process: ^3.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.12
- symfony/var-dumper: ^3.1
Suggests
- h4cc/wkhtmltopdf-amd64: Convert html to pdf using webkit (qtwebkit). Static linked linux binary for amd64 systems.
- h4cc/wkhtmltopdf-i386: Convert html to pdf using webkit (qtwebkit). Static linked linux binary for i386 systems.
This package is auto-updated.
Last update: 2024-11-26 22:15:24 UTC
README
Basically: wkhtmltopdf php bindings.
Installation?
Straightforward:
composer require ivanpepelko/php-wkhtmltopdf
Get it running...
...easily:
<?php use WkHtmlToPdf\WkHtmlToPdf; require_once 'vendor/autoload.php'; $pdf = new WkHtmlToPdf('/usr/local/bin/wkhtmltopdf'); $pdf->setInputPath('html/invoice.html') ->setOutputPath('pdf/invoice.pdf') ->useRecommendedOptions() ->convert() ;
or:
<?php use WkHtmlToPdf\WkHtmlToPdf; require_once 'vendor/autoload.php'; $pdf = new WkHtmlToPdf('/usr/local/bin/wkhtmltopdf'); header("Content-type:application/pdf"); header("Content-Disposition:attachment;filename='hello.pdf'"); echo $pdf->setInputHtml(' <!DOCTYPE html> <html> <head><title>Hello world!</title></head> <body><h1>Hello</h1><p>world!</p></body> </html> </html> ') ->useRecommendedOptions() ->convert() ->output() ;
or something like that.
The first parameter passed to constructor is path to wkhtmltopdf binary. I recommend these (wkhtmltopdf with patched qt):
Other stuff
Headers & Footers
<?php $pdf->setFooterCenterText($text) ->setHeaderLeftText($text) ->setFooterSpacing($spacing) ->setHeaderFontName($name) ->setFooterHtml($html); // etc, you get the point
In header and footer text string the following variables will be substituted:
[page]
Replaced by the number of the pages currently being printed[frompage]
Replaced by the number of the first page to be printed[topage]
Replaced by the number of the last page to be printed[webpage]
Replaced by the URL of the page being printed[section]
Replaced by the name of the current section[subsection]
Replaced by the name of the current subsection[date]
Replaced by the current date in system local format[isodate]
Replaced by the current date in ISO 8601 extended format[time]
Replaced by the current time in system local format[title]
Replaced by the title of the of the current page object[doctitle]
Replaced by the title of the output document[sitepage]
Replaced by the number of the page in the current site being converted[sitepages]
Replaced by the number of pages in the current site being converted
Also, you are able to define additional variables with addHeaderFooterVar()
or addHeaderFooterVars()
methods:
<?php $pdf->addHeaderFooterVar('var1', 'value2') ->addHeaderFooterVars(['var3' => 'value4']);
Note: this will not work with html (setHeaderHtml()
or setFooterHtml()
).
Note 2: wkhtmltopdf has an issue with html headers and footer where it adds them gray background (issue#2416 - to be fixed in 0.12.4).
Caching:
<?php $pdf->setCacheDir($dir);