-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDF.php
171 lines (152 loc) · 6.52 KB
/
PDF.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5
*/
class PDFCore
{
public $filename;
public $pdf_renderer;
public $objects;
public $template;
public $send_bulk_flag = false;
const TEMPLATE_INVOICE = 'Invoice';
const TEMPLATE_ORDER_RETURN = 'OrderReturn';
const TEMPLATE_ORDER_SLIP = 'OrderSlip';
const TEMPLATE_DELIVERY_SLIP = 'DeliverySlip';
const TEMPLATE_SUPPLY_ORDER_FORM = 'SupplyOrderForm';
/**
* @param $objects
* @param $template
* @param $smarty
* @param string $orientation
*/
public function __construct($objects, $template, $smarty, $orientation = 'P')
{
$this->pdf_renderer = new PDFGenerator((bool) Configuration::get('PS_PDF_USE_CACHE'), $orientation);
$this->template = $template;
/*
* We need a Smarty instance that does NOT escape HTML.
* Since in BO Smarty does not autoescape
* and in FO Smarty does autoescape, we use
* a new Smarty of which we're sure it does not escape
* the HTML.
*/
$this->smarty = clone $smarty;
$this->smarty->escape_html = false;
/* We need to get the old instance of the LazyRegister
* because some of the functions are already defined
* and we need to check in the old one first
*/
$original_lazy_register = SmartyLazyRegister::getInstance($smarty);
/* For PDF we restore some functions from Smarty
* they've been removed in PrestaShop 1.7 so
* new themes don't use them. Although PDF haven't been
* reworked so every PDF controller must extend this class.
*/
smartyRegisterFunction($this->smarty, 'function', 'convertPrice', array('Product', 'convertPrice'), true, $original_lazy_register);
smartyRegisterFunction($this->smarty, 'function', 'convertPriceWithCurrency', array('Product', 'convertPriceWithCurrency'), true, $original_lazy_register);
smartyRegisterFunction($this->smarty, 'function', 'displayWtPrice', array('Product', 'displayWtPrice'), true, $original_lazy_register);
smartyRegisterFunction($this->smarty, 'function', 'displayWtPriceWithCurrency', array('Product', 'displayWtPriceWithCurrency'), true, $original_lazy_register);
smartyRegisterFunction($this->smarty, 'function', 'displayPrice', array('Tools', 'displayPriceSmarty'), true, $original_lazy_register);
smartyRegisterFunction($this->smarty, 'modifier', 'convertAndFormatPrice', array('Product', 'convertAndFormatPrice'), true, $original_lazy_register); // used twice
smartyRegisterFunction($this->smarty, 'function', 'displayAddressDetail', array('AddressFormat', 'generateAddressSmarty'), true, $original_lazy_register);
smartyRegisterFunction($this->smarty, 'function', 'getWidthSize', array('Image', 'getWidth'), true, $original_lazy_register);
smartyRegisterFunction($this->smarty, 'function', 'getHeightSize', array('Image', 'getHeight'), true, $original_lazy_register);
$this->objects = $objects;
if (!($objects instanceof Iterator) && !is_array($objects)) {
$this->objects = array($objects);
}
if (count($this->objects) > 1) { // when bulk mode only
$this->send_bulk_flag = true;
}
}
/**
* Render PDF.
*
* @param bool $display
*
* @return mixed
*
* @throws PrestaShopException
*/
public function render($display = true)
{
$render = false;
$this->pdf_renderer->setFontForLang(Context::getContext()->language->iso_code);
foreach ($this->objects as $object) {
$this->pdf_renderer->startPageGroup();
$template = $this->getTemplateObject($object);
if (!$template) {
continue;
}
if (empty($this->filename)) {
$this->filename = $template->getFilename();
if (count($this->objects) > 1) {
$this->filename = $template->getBulkFilename();
}
}
$template->assignHookData($object);
$this->pdf_renderer->createHeader($template->getHeader());
$this->pdf_renderer->createFooter($template->getFooter());
$this->pdf_renderer->createPagination($template->getPagination());
$this->pdf_renderer->createContent($template->getContent());
$this->pdf_renderer->writePage();
$render = true;
unset($template);
}
if ($render) {
// clean the output buffer
if (ob_get_level() && ob_get_length() > 0) {
ob_clean();
}
return $this->pdf_renderer->render($this->filename, $display);
}
}
/**
* Get correct PDF template classes.
*
* @param mixed $object
*
* @return HTMLTemplate|false
*
* @throws PrestaShopException
*/
public function getTemplateObject($object)
{
$class = false;
$class_name = 'HTMLTemplate' . $this->template;
if (class_exists($class_name)) {
// Some HTMLTemplateXYZ implementations won't use the third param but this is not a problem (no warning in PHP),
// the third param is then ignored if not added to the method signature.
$class = new $class_name($object, $this->smarty, $this->send_bulk_flag);
if (!($class instanceof HTMLTemplate)) {
throw new PrestaShopException('Invalid class. It should be an instance of HTMLTemplate');
}
}
return $class;
}
}