CakePHP plugin for creating and/or rendering PDFs, supporting several popular PDF engines.
CakePHP plugin for creating and/or rendering PDFs, supporting several popular PDF engines.
Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF.
Engines included in the plugin:
tecnickcom/tcpdf package is deprecated upstream; use TcLibPdf insteadCommunity maintained engines:
Using Composer:
composer require friendsofcake/cakepdfCakePdf does not include any of the supported PDF engines, you need to install the ones you intend to use yourself.
Check WeasyPrint's installation guide to install it on your system. DomPdf, Mpdf, TcLibPdf and Tcpdf can be installed via composer using one of the following commands:
composer require dompdf/dompdf
composer require tecnickcom/tc-lib-pdf
composer require tecnickcom/tcpdf
composer require mpdf/mpdfUnlike the deprecated Tcpdf engine, tecnickcom/tc-lib-pdf does not bundle any
fonts. It loads fonts from generated *.json font files located in a directory
referenced by the K_PATH_FONTS constant. Define it once during your
application bootstrap, before any PDF is rendered:
define('K_PATH_FONTS', '/absolute/path/to/your/fonts');The directory must contain at least the core fonts you reference (e.g.
helvetica.json). See the
tc-lib-pdf-font documentation
for how to generate font files.
Loading the plugin using CakePHP's console:
./bin/cake plugin load CakePdfIf you plan to use the PDF view functionality
that automatically renders and returns the PDF for sending it to the browser,
you should also register the pdf extension in your config/routes.php file:
$routes->scope('/', function (\Cake\Routing\RouteBuilder $routes) {
$routes->setExtensions(['pdf']);
// ...
});Further setup information can be found in the usage section.
Use Configure::write('CakePdf', $config); or in controller use view builder to
set view option named pdfConfig (only when used with PdfView). You need to
define at least $config['engine']. When using CakePdf directly you can also
pass the config array to constructor. The value for engine should have the
Plugin.ClassName format without the Engine suffix.
Configuration options:
WkHtmlToPdfEngine: The options are passed as CLI argumentsWeasyPrintEngine: The options are passed as CLI argumentsTexToPdfEngine: The options are passed as CLI argumentsDomPdfEngine: The options are passed to constructor of Dompdf classMpdfEngine: The options are passed to constructor of Mpdf classTcLibPdfEngine: Supports unit, unicode, subsetFont, compress, mode, font (['family', 'style', 'size']), margins (['left', 'top', 'right', 'bottom']) and metadata (['author', 'creator', 'subject', 'keywords'])Example:
Configure::write('CakePdf', [
'engine' => 'CakePdf.WeasyPrint',
'download' => true,
]);…The engine and crypto config options can also be arrays with configuration
options for the relevant class. For example:
…You can use CakePdf in two ways, read carefully which one you actually need. Many people mix both ways and don't get the expected results.
You can create PDF template and layout files for your controller actions and have
them automatically rendered. Place the templates in a pdf subdir, for
instance templates/Invoices/pdf/view.php, layouts will be in
templates/layout/pdf/default.php.
Then for e.g. accessing the URL http://localhost/invoices/view/1.pdf would
give you the generated PDF.
Additionally you can map resources by adding Router::mapResources(['Invoices']);
to your routes file and you can access the same document at
http://localhost/invoices/1.pdf.
In case you don't want to use the pdf extension in your URLs, you can omit
registering it in your routes configuration. You can then set the Accept
request header to application/pdf to make CakePHP automatically switch to PdfView
(required the viewClasses() method to be set as shown above), or explicity switch
the view class in your controller action:
$this->viewBuilder()->setClassName('CakePdf.Pdf');Instead of having the pdf rendered in the browser itself, you can force it to be
downloaded by using download option. Additionally you can specify custom filename
using filename options.
$this->viewBuilder()->setOption(
'pdfConfig',
[
'download' => true, // This can be omitted if "filename" is specified.
'filename' => 'Invoice_' . $id, // This can be omitted if you want a file name based on URL.
]
);You can use CakePdf to create raw PDF data with a view template.
The view file path would look like templates/pdf/newsletter.php.
Layout file path would be like templates/layout/pdf/default.php
Note that layouts for both usage types are within same directory, but the view
templates use different file paths Optionally you can also write the raw data to
file.
Example:
$CakePdf = new \CakePdf\Pdf\CakePdf();
$CakePdf->template('newsletter', 'default');
$CakePdf->viewVars(['key' => 'value']);
// Get the PDF string returned
$pdf = $CakePdf->output();
// Or write it to file directly
$pdf = $CakePdf->write(APP . 'files' . DS . 'newsletter.pdf');You can optionally encrypt the PDF with permissions
To use encryption you first need to select a crypto engine. Currently we support the following crypto engines:
Add the following in your bootstrap.
Configure::write('CakePdf.crypto', 'CakePdf.Pdftk');Options in pdfConfig:
Permissions:
By default, we deny all permissions.
To allow all permissions:
Set 'permission' to true
To allow specific permissions:
Set 'permissions' to an array with a combination of the following available permissions:
Use absolute URLs for static assets in your view templates for PDFs.
If you use HtmlHelper::image(), or HtmlHelper::css()
make sure you have set fullBase option to true.
For example
echo $this->Html->image('logo.png', ['fullBase' => true]);
echo $this->Html->css('bootstrap.css', ['fullBase' => true]);If you are enable to get URLs for assets working properly, you can try using file system paths instead for the assets.
img/logo.png" />Note: Since v0.12.16 wkhtmltopdf requires the option enable-local-file-access
to be able to use local filesytem paths for assets. You can enable it by setting
'enable-local-file-access' => true in the engine config array.
Here are a couple of CSS based solutions you can refer to for easily getting header footer on all PDF pages.
No open issues yet, or sync has not completed.