-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPDFResponseFormatter.php
208 lines (177 loc) · 4.82 KB
/
MPDFResponseFormatter.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace iutbay\yii2mpdf;
use Yii;
use yii\base\Component;
use yii\web\ResponseFormatterInterface;
use yii\helpers\ArrayHelper;
/**
* mPDF response formatter.
*
* @author Kevin LEVRON <[email protected]>
*/
class MPDFResponseFormatter extends Component implements ResponseFormatterInterface
{
/**
* mPDF construtor parameters
* @link http://mpdf1.com/manual/index.php?tid=184
*/
public $mPDFConstructorOptions = [
'mode' => '',
'format' => 'A4',
'defaultFontSize' => 0,
'defaultFont' => '',
'marginLeft' => 15,
'marginRight' => 15,
'marginTop' => 16,
'marginBottom' => 16,
'marginHeader' => 9,
'marginFooter' => 9,
'orientation' => 'P',
];
/**
* mPDF options
* @link http://mpdf1.com/manual/index.php?tid=273
* @var array
*/
public $mPDFOptions = [];
/**
* CSS files
* @var array
*/
public $cssFiles = [];
/**
* CSS
* @var string
*/
public $css;
/**
* mPDF header
* @see \mPDF::SetHeader()
* @link http://mpdf1.com/manual/index.php?tid=149
* @var array
*/
public $header;
/**
* mPDF footer
* @see \mPDF::SetFooter()
* @link http://mpdf1.com/manual/index.php?tid=151
* @var array
*/
public $footer;
/**
* mPDF output options
* @see \mPDF::Output()
* @link http://mpdf1.com/manual/index.php?tid=125
*/
public $outputName = '';
public $outputDest = 'I';
/**
* @var \Closure
*/
public $beforeWrite;
/**
* @inheritdoc
*/
public function format($response)
{
$this->formatMPDF($response);
}
/**
*
* @param \yii\web\Response $response
*/
protected function formatMPDF($response)
{
$mPDFCO = ArrayHelper::getValue($response->data, 'mPDFConstructorOptions', []);
$mPDFCO = array_merge($this->mPDFConstructorOptions, $mPDFCO);
$mpdf = new \mPDF(
$mPDFCO['mode'],
$mPDFCO['format'],
$mPDFCO['defaultFontSize'],
$mPDFCO['defaultFont'],
$mPDFCO['marginLeft'],
$mPDFCO['marginRight'],
$mPDFCO['marginTop'],
$mPDFCO['marginBottom'],
$mPDFCO['marginHeader'],
$mPDFCO['marginFooter'],
$mPDFCO['orientation']
);
$this->setMPDFOptions($mpdf, $this->mPDFOptions);
if (is_array($this->cssFiles)) {
foreach ($this->cssFiles as $file) {
$css = @file_get_contents(Yii::getAlias($file));
if (!empty($css))
$mpdf->WriteHTML($css, 1);
}
}
if (isset($this->css))
$mpdf->WriteHTML($this->css, 1);
$this->setMPDFHeader($mpdf, $this->header);
$this->setMPDFFooter($mpdf, $this->footer);
if (is_string($response->data)) {
if ($this->beforeWrite !== null)
call_user_func($this->beforeWrite, $mpdf);
$mpdf->WriteHTML($response->data, 2);
} else if (is_array($response->data)) {
$this->setOptions($mpdf, $response->data['options']);
$this->setMPDFOptions($mpdf, $response->data['mPDFOptions']);
if ($this->beforeWrite !== null)
call_user_func($this->beforeWrite, $mpdf);
if (isset($response->data['content']))
$mpdf->WriteHTML($response->data['content'], 2);
}
$mpdf->Output($this->outputName, $this->outputDest);
}
/**
* Set formatter options
*/
protected function setOptions($mpdf, $options)
{
if (!is_array($options))
return;
foreach ($options as $attribute => $value) {
$this->$attribute = $value;
if ($attribute === 'header')
$this->setMPDFHeader($mpdf, $value);
if ($attribute === 'footer')
$this->setMPDFFooter($mpdf, $value);
}
}
/**
* Set mPDF options
*/
protected function setMPDFOptions($mpdf, $options)
{
if (!is_array($options))
return;
foreach ($options as $attribute => $value) {
$mpdf->$attribute = $value;
}
}
/**
* Set mPDF header
*/
protected function setMPDFHeader($mpdf, $header)
{
if (empty($header))
return;
$mpdf->SetHeader($header);
}
/**
* Set mPDF footer
*/
protected function setMPDFFooter($mpdf, $footer)
{
if (empty($footer))
return;
$mpdf->SetFooter($footer);
}
/**
* Set DI container class configuration
*/
public static function setClassConfiguration($config)
{
Yii::$container->set(self::className(), $config);
}
}