Most modern Enterprise Systems require to generate reports, agreements, business plans or a variety of documents that can be downloaded in PDF format. Pdf is one of the most popular file formats used for document sharing as it has a small file size, can be viewed and edited on a computer and also it can be printed with great quality. Follow these steps to prepare your Symfony project for Pdf generation:
Prerequisites
Step 1. Install wkhtmltopdf1 package in your server:
### This is the suggested installation procedure in CentOS 7
$> cd /tmp
### Get most recent package
$> wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.centos7.x86_64.rpm
### Install it along with dependencies
$> sudo yum -y localinstall wkhtmltox-0.12.6-1.centos7.x86_64.rpm
Step 2. Install Symfony package2
$> cd /var/www/symfony-root
$> composer require mikehaertl/phpwkhtmltopdf
Step 3. Create a new action method in your Symfony Controller to download the pdf:
public function getCdpPdfForLineItemId($lineItemId, CdpService $cdpService): Response
{
$cdpData = $cdpService->getCdpData($lineItemId);//Gather all data
$htmlPages = $cdpService->getHtmlPages($cdpData);//Get html pages with this data
$pdf = $cdpService->getPdf($htmlPages);//Generate Pdf
if (!$pdf->send()) {
throw new PdfDownloadException($pdf->getError());
} else {
return new Response('ok');
}
}
Step 4. In this example $cdpData
variable is an array that will be passed to the getHtmlPages
method in this way:
public function getHtmlPages(array $cdpData): array
{
try {
$cdpData['images'] = $this->getEncodedImages($cdpData);
$htmlPages = [];
$htmlPages['cover'] = $this->getHtmlForTemplate(self::TITLE_PAGE_TEMPLATE, $cdpData);
$cdpData['pageNumber'] = 3;
$htmlPages['pages'][] = $this->getHtmlForTemplate(self::OVERVIEW_PAGE_TEMPLATE, $cdpData);
$cdpData['pageNumber'] += 1;
...
### Each page is using a twig template that is rendered this way
public function getHtmlForTemplate(string $templateName, array $cdpData = []): string
{
try {
return $this->twig->render(self::CDP_TEMPLATE_DIR . $templateName, $cdpData);
} catch (Throwable $e) {
throw new SystemException('Template Rendering failed');
}
}
In each Twig file you will use the $cdpData
keys that corresponds to the twig template variables defined
Step 5. In this example once we get the $htmlPages
we use the getPdf
method to generate the Pdf Document:
/**
* @param array $htmlPages
* @return Pdf
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function getPdf(array $htmlPages): Pdf
{
try {
$pdf = new Pdf(array(
'no-outline', // Make Chrome not complain
'margin-top' => '0',
'margin-left' => '0',
'margin-right' => '0',
'margin-bottom' => '0',
'ignoreWarnings' => true,
'footer-spacing' => '0',
'no-outline',
'commandOptions' => [
'useExec' => true,
'procEnv' => [
'LANG' => 'en_US.utf-8',
],
'escapeArgs' => false,
'procOptions' => [
'bypass_shell' => true,
'suppress_errors' => true,
],
],
// Default page options
'disable-smart-shrinking',
//'user-style-sheet' => '/path/to/pdf.css',
));
//Add cover
$pdf->addCover($htmlPages['cover']);
//Create Table of Contents
$pdf->addToc(['xsl-style-sheet' => $this->getHtmlForTemplate('pageHeaderGeneric.html.twig')]);
//Add Pages to Pdf
foreach ($htmlPages['pages'] as $htmlPage) {
$pdf->addPage($htmlPage, null, 'html');
}
return $pdf;
} catch (Throwable $e) {
throw new SystemException('Template Rendering failed');
}
}
With this code, the Pdf will also have a nice Cover Page and a Table of Contents Page along with all other required pages.
1 Refer to wkhtmltopdf website for more downloads
2 Refer to the packagist website for more information