/
Template.php
126 lines (105 loc) · 5.06 KB
/
Template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
/*
* This file is part of the ContaoIsotopePdfTemplatesBundle bundle.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/
namespace InspiredMinds\ContaoIsotopePdfTemplatesBundle\Isotope\Model\Document;
use Contao\Environment;
use Contao\FilesModel;
use Contao\StringUtil;
use Contao\System;
use InspiredMinds\ContaoIsotopePdfTemplatesBundle\Event\ModifyPdfEvent;
use Isotope\Interfaces\IsotopeProductCollection;
class Template extends \Isotope\Model\Document\Standard
{
protected function generatePDF(IsotopeProductCollection $objCollection, array $arrTokens)
{
// Get the project directory
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
// Include TCPDF config
if (file_exists($projectDir.'/system/config/tcpdf.php')) {
require_once $projectDir.'/system/config/tcpdf.php';
} elseif (file_exists($projectDir.'/vendor/contao/core-bundle/src/Resources/contao/config/tcpdf.php')) {
require_once $projectDir.'/vendor/contao/core-bundle/src/Resources/contao/config/tcpdf.php';
} elseif (file_exists($projectDir.'/vendor/contao/tcpdf-bundle/src/Resources/contao/config/tcpdf.php')) {
require_once $projectDir.'/vendor/contao/tcpdf-bundle/src/Resources/contao/config/tcpdf.php';
}
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
// Add custom fonts
if ($this->useCustomFonts) {
if (null !== ($folder = FilesModel::findByUuid($this->customFontsDirectory))) {
$fontDirs[] = $projectDir.'/'.$folder->path;
$config = StringUtil::deserialize($this->customFontsConfig, true);
if (!empty($config)) {
foreach ($config as $font) {
if (!empty($font['fontname']) && $font['enabled']) {
$fontData[$font['fontname']][$font['variant']] = $font['filename'];
}
}
}
}
}
$margin = StringUtil::deserialize($this->pdfMargin, true);
// Create new PDF document
$pdf = new \Mpdf\Mpdf([
'fontDir' => $fontDirs,
'fontdata' => $fontData,
'format' => $this->pdfFormat ?: (\defined('PDF_PAGE_FORMAT') ? PDF_PAGE_FORMAT : 'A4'),
'orientation' => $this->pdfOrientation ?: (\defined('PDF_PAGE_ORIENTATION') ? PDF_PAGE_ORIENTATION : 'P'),
'margin_left' => (int) ($margin['left'] ?? (\defined('PDF_MARGIN_LEFT') ? PDF_MARGIN_LEFT : 15)),
'margin_right' => (int) ($margin['right'] ?? (\defined('PDF_MARGIN_RIGHT') ? PDF_MARGIN_RIGHT : 15)),
'margin_top' => (int) ($margin['top'] ?? (\defined('PDF_MARGIN_TOP') ? PDF_MARGIN_TOP : 10)),
'margin_bottom' => (int) ($margin['bottom'] ?? (\defined('PDF_MARGIN_BOTTOM') ? PDF_MARGIN_BOTTOM : 10)),
'default_font_size' => (int) ($this->pdfDefaultFontSize ?: (\defined('PDF_FONT_SIZE_MAIN') ? PDF_FONT_SIZE_MAIN : 1)),
'default_font' => $this->pdfDefaultFont ?: (\defined('PDF_FONT_NAME_MAIN') ? PDF_FONT_NAME_MAIN : 'freeserif'),
]);
// Set document information
$pdf->SetCreator($this->pdfCreator);
$pdf->SetAuthor($this->pdfAuthor ?: Environment::get('url'));
$pdf->SetTitle(StringUtil::parseSimpleTokens($this->documentTitle, $arrTokens));
// Dispatch modify pdf event
System::getContainer()->get('event_dispatcher')->dispatch(
new ModifyPdfEvent($pdf, $this),
ModifyPdfEvent::EVENT_NAME
);
// Check to use template
if ($this->usePdfTemplate) {
// Find file in database
if (null !== ($file = FilesModel::findById($this->usePdfTemplateSRC))) {
// Check if file exists
if (file_exists($projectDir.'/'.$file->path)) {
$pdf->SetDocTemplate($projectDir.'/'.$file->path, true);
}
}
}
// Initialize document and add a page
$pdf->AddPage();
// Write the HTML content
$pdf->WriteHTML($this->generateTemplate($objCollection, $arrTokens));
// Reset template
$pdf->SetDocTemplate();
// Check to append PDF
if ($this->appendPdfTemplate) {
// Find file in database
if (null !== ($file = FilesModel::findById($this->appendPdfTemplateSRC))) {
// Check if file exists
if (file_exists($projectDir.'/'.$file->path)) {
$pagecount = $pdf->SetSourceFile($projectDir.'/'.$file->path);
for ($i = 1; $i <= $pagecount; ++$i) {
$pdf->AddPage();
$tpl = $pdf->ImportPage($i);
$pdf->UseTemplate($tpl);
}
}
}
}
return $pdf;
}
}