What is this?
Basically: wkhtmltopdf php bindings., (*1)
Installation?
Straightforward:, (*2)
composer require ivanpepelko/php-wkhtmltopdf
Get it running...
...easily:, (*3)
<?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:, (*4)
<?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., (*5)
The first parameter passed to constructor is path to wkhtmltopdf binary. I recommend these (wkhtmltopdf with patched qt):
* h4cc/wkhtmltopdf-amd64
* h4cc/wkhtmltopdf-i386, (*6)
Other stuff
Full docs, (*7)
<?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, (*8)
Also, you are able to define additional variables with addHeaderFooterVar() or addHeaderFooterVars() methods:, (*9)
<?php
$pdf->addHeaderFooterVar('var1', 'value2')
->addHeaderFooterVars(['var3' => 'value4']);
Note: this will not work with html (setHeaderHtml() or setFooterHtml())., (*10)
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)., (*11)
Caching:
<?php
$pdf->setCacheDir($dir);