</> Code The Pixel

CakePHP 4 Print PDF Using CakePDF

Asyraf Wahi Anuar - June 17, 2020

Estimated reading time: 2 minutes, 40 seconds

This tutorial will show how to use the CakePDF plugin to generate a PDF file. This tutorial will use DOMPDF as an example of the generator engine. Basically, generating PDF is useful for those who are working with a system that prints or produce correspondence or document related task. 

Download and Load CakePDF Plugin
Download CakePDF plugin via composer:

composer require friendsofcake/cakepdf

Load the plugin using the console:

bin/cake plugin load CakePdf

or manually add the following code to ...\src\Application.php


PDF Engine
The plugin does not include the PDF generator engine. You need to download the engine. Depending on what engine you want to use, you need to download them to your project. DomPdf, Mpdf and Tcpdf can be installed via composer using one of the following commands:

composer require dompdf/dompdf
composer require tecnickcom/tcpdf
composer require mpdf/mpdf

You also can use WkHtmlToPdf which can be downloaded from the webpage.

Engine Configuration
File Location: ...\config\bootstrap.php
At the end of the file, load the following codes. Replace the "Dompdf" engine with the one you intend to use yourself.

Configure::write('CakePdf', [
    'engine' => [
        'className' => 'CakePdf.DomPdf',
        'options' => [
            'isRemoteEnabled' => true
    'margin' => [
        'bottom' => 10,
        'left' => 10,
        'right' => 10,
        'top' => 10
    'orientation' => 'portrait',
    'download' => true

If use WkHtmlToPdf, specify the binary full path:

'binary' => 'C:\wkhtmltopdf\bin\wkhtmltopdf.exe'

Depending on which controller you need to generate the PDF, create a new public function pdf:

public function pdf($id = null)
    $report = $this->Reports->get($id);
            'orientation' => 'portrait',
            'download' => true, // This can be omitted if "filename" is specified.
            'filename' => 'Report_' . $id . '.pdf' //// This can be omitted if you want file name based on URL.
    $this->set('report', $report);

If you want to use the title as the filename, use the following codes:

'filename' => \Cake\Utility\Text::slug($report->title) . '-' . __('Report') . '.pdf'

Create the view templates in a 'pdf' subdir, for instance, templates/Reports/pdf/pdf.php and add the following codes into pdf.php file

<!DOCTYPE html>
@page {
    margin: 0px 0px 0px 0px !important;
    padding: 0px 0px 0px 0px !important;
<?= h($report->title) ?>

Download Link
In your view.php file, add the following code to call the pdf page:

<?= $this->Html->link(__('Download PDF'), ['action' => 'pdf', $report->id ]) ?>

Insert Image in PDF
It required fullbase set to true as follows:

<?= $this->Html->image('myImage.png', ['fullBase' => true]); ?>

Insert CSS in PDF
It required fullbase set to true as follows:

<?= $this->Html->css('pdf.css', ['fullBase' => true]) ?>

Now you should be able to generate the pdf file and download it. For CSS, just create and link it to the pdf template or just add it to the style.
That all. Happy coding :)

Cite this article (APA 6th Edition)

CakePHP 4 Print PDF Using CakePDF
June, 17 2020
CakePHP 4 jQuery Date Time Picker
October, 01 2018
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 Export To CSV
May, 29 2020
CakePHP 4 Authentication Using...
May, 11 2020
CakePHP 4 File Upload Using Proffer Plugin
May, 15 2020
CakePHP 4 Find, Sort & Count
June, 02 2020
Sharing Link
Click the icon to share