</> Code The Pixel

CakePHP 4 Print PDF Using CakePDF

Asyraf Wahi Anuar - May 17, 2020

Estimated reading time: 5 minutes, 0 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

$this->addPlugin('CakePdf');


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'


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

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

Save PDF to Server
To generate and save the PDF into a webserver, use the following code:

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.
        ]
    );
	//Write and Save PDF to folder
	$CakePdf = new \CakePdf\Pdf\CakePdf();
	$CakePdf->template('report', 'default');
	$CakePdf->viewVars([
		'title' => $report->title, //variables eg
		'body' => $report->body,
	]);
	// Get the PDF string returned
	$pdf = $CakePdf->output();
	// Or write it to file directly
	$pdf = $CakePdf->write('files' . DS . 'pdf' . DS . $id . '.pdf'); //stored in ...webroot/files/pdf/1.pdf
	
    $this->set('report', $report, 'title', 'body');
}

Create default template at ...\templates\layout\pdf\default.php and use the viewVars eg:

<!DOCTYPE html>
<html>
<head>
<title>PDF</title>
<?php
use Cake\Routing\Router;
?>
<style>
@page {
	margin-top: 50px !important;
	margin-bottom: 50px !important;
	margin-right: 50px !important;
	margin-left: 50px !important;
    padding: 0px 0px 0px 0px !important;
}
body{
	font-family: 'Roboto', sans-serif;
}
</style>
</head>
<body>
Hello<br/>
<?= $title; ?>
<br/>
<?= $body; ?>
</body>
</html>


Email Saved PDF as Email Attachment
To email the saved PDF as an attachment, use the following code. Read more about CakePHP 4 Email.

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.
        ]
    );
	//Write and Save PDF to folder
	$CakePdf = new \CakePdf\Pdf\CakePdf();
	$CakePdf->template('report', 'default');
	$CakePdf->viewVars([
		'title' => $report->title, //variables eg
		'body' => $report->body,
	]);
	// Get the PDF string returned
	$pdf = $CakePdf->output();
	// Or write it to file directly
	$pdf = $CakePdf->write('files' . DS . 'pdf' . DS . $id . '.pdf');
	
	//Email with attachment
	$mailer = new Mailer('default');
	$mailer
		->setTransport('smtp');
		->setAttachments(['files' . DS . 'pdf' . DS . $id . '.pdf']); //get saved PDF file
		->setFrom(['[email protected]!xel.com' => 'Code The Pixel'])
		->setTo('[email protected]') //receiver email
		->setEmailFormat('html')
		->setSubject('Report') //email subject
		->deliver('Report in PDF'); //content

		return $this->redirect($this->referer());
	
    $this->set('report', $report, 'title', 'body');
}


That all. Happy coding :)


Cite this article (APA 6th Edition)

Popular
CakePHP 4 Print PDF Using CakePDF
May, 17 2020
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 jQuery Date Time Picker
October, 01 2018
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
Share
Sharing Link
Click the icon to share