pieceofcake2 / cakepdf
CakePHP plugin for creating and/or rendering Pdf, several Pdf engines supported.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 182
Type:cakephp-plugin
pkg:composer/pieceofcake2/cakepdf
Requires
- php: ^8.0
- composer/installers: *
- pieceofcake2/cakephp: ^2.10
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- dompdf/dompdf: ^3.1
- mpdf/mpdf: ^8.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- pieceofcake2/app: ^2.1
- pieceofcake2/phpstan-cakephp2: ^0.2.1
- tecnickcom/tcpdf: ^6.10
Suggests
- dompdf/dompdf: When using DomPDF as engine.
Replaces
- ceeram/cakepdf: ^1.0
- friendsofcake/cakepdf: ^1.0
README
This is forked for CakePHP2.
Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF.
Current engines:
- DomPdf
- Mpdf
- Tcpdf
- WkHtmlToPdf (requires additional installation) RECOMMENDED ENGINE
Requirements
- PHP 8.0+
- CakePHP 2.10+
- wkhtmltopdf (optional) See: http://wkhtmltopdf.org/
- pdftk (optional) See: http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
Installation
In your app directory type
composer require pieceofcake2/cakepdf
Setup
In app/Config/bootstrap.php
add:
CakePlugin::load('CakePdf', ['bootstrap' => true, 'routes' => true]);
Configuration
Use Configure::write('CakePdf', $config);
or set Controller property $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:
- engine: Engine to be used (required)
- options: Engine options, this may vary between Engines
- pageSize: Change the default size, defaults to A4
- orientation: Change the default orientation, defaults to potrait
- margin: Array or margins with the keys: bottom, left, right, top and their values
- title: Title of the document
- encoding: Change the encoding, defaults to UTF-8
- binary: Path to binary (WkHtmlToPdfEngine only), defaults to /usr/bin/wkhtmltopdf
- download: Set to true to force a download, only when using PdfView
- filename: Filename for the document when using forced download
Example:
Configure::write('CakePdf', [ 'engine' => 'CakePdf.WkHtmlToPdf', 'options' => [ 'print-media-type' => false, 'outline' => true, 'dpi' => 96, ], 'margin' => [ 'bottom' => 15, 'left' => 50, 'right' => 30, 'top' => 45, ], 'orientation' => 'landscape', 'download' => true ]);
class InvoicesController extends AppController { //in your Invoices controller you could set additional configs, or override the global ones: public function view($id = null) { $this->Invoice->id = $id; if (!$this->Invoice->exists()) { throw new NotFoundException(__('Invalid invoice')); } $this->pdfConfig = [ 'orientation' => 'portrait', 'filename' => 'Invoice_' . $id, ]; $this->set('invoice', $this->Invoice->read(null, $id)); } }
Usage
You can use CakePdf in 2 ways, read carefully which one you actually need. Many people mix both ways and dont get the expected results.
1: Render as PDF (including forced download) in the browser with PdfView
You can create PDF view and layout files for your controller actions and have them automatically rendered.
Place the view templates in a 'pdf' subdir, for instance app/View/Invoices/pdf/view.ctp
Layouts will be in app/View/Layouts/pdf/default.ctp
Make sure your InvoicesController has RequestHandler Component in the $components
array.
Browse to http://localhost/invoices/view/1.pdf
Additionally you can map resources by adding Router::mapResources(array('Invoices'));
to your routes
file and you can access the same document at http://localhost/invoices/1.pdf
2: Create PDF for email attachment, file storage etc.
You can use CakePdf lib to create raw PDF data with a view template.
The view file path would look like app/View/Pdf/newsletter.ctp
.
Layout file path would be like app/View/Layouts/pdf/default.ctp
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(); $cakePdf->template('newsletter', 'default'); //get the pdf string returned $pdf = $cakePdf->output(); //or write it to file directly $pdf = $cakePdf->write(APP . 'files' . DS . 'newsletter.pdf');
Encryption
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:
- Pdftk
Usage
Add the following in your bootstrap.
Configure::write('CakePdf.crypto', 'CakePdf.Pdftk');
Options in pdfConfig:
- protect: Set to true to enable encryption
- userPassword (optional): Set a password to open the PDF file
- ownerPassword (optional): Set the password to unlock the locked permissions
- one of the above must be present, either userPassword or ownerPassword
- permissions (optional): Define the permissions
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:
- degraded_print
- modify,
- assembly,
- copy_contents,
- screen_readers,
- annotate,
- fill_in
Note about static assets
Use absolute URLs for static assets in your view templates for PDFs.
If you use HtmlHelper::image()
, HtmlHelper::script()
or HtmlHelper::css()
make sure you have $options['fullBase'] = true
Another solution would be to create a AppHelper
of which it would force $options['fullBase'] = true
for PDF requests. e.g:
class AppHelper extends Helper { public function assetUrl($path, $options = []) { if (!empty($this->request->params['ext']) && $this->request->params['ext'] === 'pdf') { $options['fullBase'] = true; } return parent::assetUrl($path, $options); } }
Thanks
Many thanks to Kim Biesbjerg and Jelle Henkens for their contributions. Want your name here as well? Create a pull request for improvements/other PDF engines.