CakePHP 4 Print PDF Using CakePDF

CakePHP 4 Print PDF Using CakePDF

This tutorial will show how to use 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

$this->addPlugin('CakePdf');


PDF Engine

The plugin does not include the PDF generator engine. You need to download the engine. Depend 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 download 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'


Controller

Depend on which controller you need to generate the PDF, create a new public function eg (Report):

public function pdf($id = null)
{
	$this->viewBuilder()->enableAutoLayout(false); 
	$report = $this->Reports->get($id);
	$this->viewBuilder()->setClassName('CakePdf.pdf');
	$this->viewBuilder()->setOption(
		'pdfConfig',
		[
			'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'


Template

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>
<html>
<head>
<title>PDF</title>
<style>
@page {
	margin: 0px 0px 0px 0px !important;
	padding: 0px 0px 0px 0px !important;
}
</style>
</head>
<body>
<?= h($report->title) ?>
</body>
</html>


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 to the style.

That all. Happy coding :)