Public Member Functions | |
__construct ($options=array()) | |
__destruct () | |
addPage ($input, $options=array()) | |
addCover ($input, $options=array()) | |
addToc ($options=array()) | |
saveAs ($filename) | |
send ($filename=null) | |
setOptions ($options) | |
setPageOptions ($options=array()) | |
getError () | |
getTmpDir () |
Public Attributes | |
const | REGEX_HTML = '/<.*html.*>/i' |
Protected Member Functions | |
getPdfFilename () | |
getCommand ($filename) | |
createPdf ($fileName) | |
createTmpFile ($content) | |
renderOptions ($options) |
Protected Attributes | |
$bin = '/usr/bin/wkhtmltopdf' | |
$options = array() | |
$pageOptions = array() | |
$objects = array() | |
$tmp | |
$tmpFile | |
$tmpFiles = array() | |
$error |
This class is a slim wrapper around wkhtmltopdf.
It provides a simple and clean interface to ease PDF creation with wkhtmltopdf. The wkhtmltopdf binary must be installed and working on your system. The static binary is preferred but this class should also work with the non static version, even though a lot of features will be missing.
$pdf = new WkHtmlToPdf; // Add a HTML file, a HTML string, a page from URL or a PDF file $pdf->addPage('/home/joe/page.html'); $pdf->addPage('<html>....</html>'); $pdf->addPage('http://google.com'); $pdf->addPage('/home/joe/my.pdf'); // Add a cover (same sources as above are possible) $pdf->addCover('mycover.pdf'); // Add a Table of contents $pdf->addToc(); // Save the PDF, or ... $pdf->saveAs('/tmp/new.pdf'); // ... send to client for inline display or ... $pdf->send(); // ... send to client as file download $pdf->send('test.pdf');
The wkhtmltopdf binary has some global options (e.g. to set the document's DPI) and options for each PDF page (e.g. to supply a custom CSS file). Please see "wkhtmltopdf -H" to get a list of all available options.
In addition this class also supports global page options: You can set default page options that will be applied to every page you add. You can also override these defaults per page:
$pdf = new WkHtmlToPdf($options); // Set global PDF options $pdf->setOptions($options); // Set global PDF options (alternative) $pdf->setPageOptions($options); // Set default page options $pdf->addPage($page, $options); // Add page with options (overrides default page options)
You can use these special global options to set up some file paths:
bin: path to the wkhtmltopdf binary. Defaults to /usr/bin/wkhtmltopdf. tmp: path to tmp directory. Defaults to PHP temp dir.
saveAs() and save() will return false on error. In this case the detailed error message from wkhtmltopdf can be obtained through getError().
Definition at line 74 of file WkHtmlToPdf.php.
WkHtmlToPdf::__construct | ( | $options = array() | ) |
array | $options | global options for wkhtmltopdf (optional) |
Definition at line 94 of file WkHtmlToPdf.php.
References setOptions().
WkHtmlToPdf::__destruct | ( | ) |
Remove temporary PDF file and pages when script completes
Definition at line 103 of file WkHtmlToPdf.php.
WkHtmlToPdf::addCover | ( | $input, | |
$options = array() |
|||
) |
Add a cover page object to the output
string | $input | either a URL or a PDF filename |
array | $options | optional options for this page |
Definition at line 130 of file WkHtmlToPdf.php.
WkHtmlToPdf::addPage | ( | $input, | |
$options = array() |
|||
) |
Add a page object to the output
string | $input | either a URL, a HTML string or a PDF/HTML filename |
array | $options | optional options for this page |
Definition at line 118 of file WkHtmlToPdf.php.
References createTmpFile().
WkHtmlToPdf::addToc | ( | $options = array() | ) |
Add a TOC object to the output
array | $options | optional options for the table of contents |
Definition at line 141 of file WkHtmlToPdf.php.
|
protected |
Create the temporary PDF file
Definition at line 273 of file WkHtmlToPdf.php.
References getCommand().
Referenced by getPdfFilename().
|
protected |
Create a tmp file with given content
string | $content | the file content |
Definition at line 307 of file WkHtmlToPdf.php.
References getTmpDir().
Referenced by addPage().
|
protected |
string | $filename | the filename of the output file |
Definition at line 254 of file WkHtmlToPdf.php.
References renderOptions().
Referenced by createPdf().
WkHtmlToPdf::getError | ( | ) |
Definition at line 216 of file WkHtmlToPdf.php.
|
protected |
Definition at line 235 of file WkHtmlToPdf.php.
References createPdf(), and getTmpDir().
WkHtmlToPdf::getTmpDir | ( | ) |
Definition at line 224 of file WkHtmlToPdf.php.
Referenced by createTmpFile(), and getPdfFilename().
|
protected |
array | $options | for a wkhtml, either global or for an object |
Definition at line 322 of file WkHtmlToPdf.php.
Referenced by getCommand().
WkHtmlToPdf::saveAs | ( | $filename | ) |
Save the PDF to given filename (triggers PDF creation)
string | $filename | to save PDF as |
Definition at line 153 of file WkHtmlToPdf.php.
References getPdfFilename().
WkHtmlToPdf::send | ( | $filename = null | ) |
Send PDF to client, either inline or as download (triggers PDF creation)
mixed | $filename | the filename to send. If empty, the PDF is streamed. |
Definition at line 168 of file WkHtmlToPdf.php.
References getPdfFilename().
WkHtmlToPdf::setOptions | ( | $options | ) |
Set global option(s)
array | $options | list of global options to set as name/value pairs |
Definition at line 192 of file WkHtmlToPdf.php.
Referenced by __construct().
WkHtmlToPdf::setPageOptions | ( | $options = array() | ) |
array | $options | that should be applied to all pages as name/value pairs |
Definition at line 208 of file WkHtmlToPdf.php.