1
1
<?php
2
2
3
3
/**
4
- * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2018
4
+ * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2020
5
5
* @package yii2-mpdf
6
6
* @version 1.0.6
7
7
*/
@@ -198,32 +198,6 @@ class Pdf extends Component
198
198
*/
199
199
protected $ _pdfAttachments ;
200
200
201
- /**
202
- * Defines a Mpdf temporary path if not set.
203
- *
204
- * @param string $prop the Mpdf constant to define
205
- * @param string $dir the directory to create
206
- *
207
- * @throws InvalidConfigException
208
- */
209
- protected static function definePath ($ prop , $ dir )
210
- {
211
- if (defined ($ prop )) {
212
- $ propDir = constant ($ prop );
213
- if (is_writable ($ propDir )) {
214
- return ;
215
- }
216
- }
217
- $ status = true ;
218
- if (!is_dir ($ dir )) {
219
- $ status = mkdir ($ dir , 0777 , true );
220
- }
221
- if (!$ status ) {
222
- throw new InvalidConfigException ("Could not create the folder ' {$ dir }' in ' \$tempPath' set. " );
223
- }
224
- define ($ prop , $ dir );
225
- }
226
-
227
201
/**
228
202
* @inheritdoc
229
203
*/
@@ -236,25 +210,26 @@ public function init()
236
210
237
211
/**
238
212
* Initialize folder paths to allow [[Mpdf]] to write temporary data.
239
- *
240
- * @throws InvalidConfigException
241
213
*/
242
214
public function initTempPaths ()
243
215
{
244
216
if (empty ($ this ->tempPath )) {
245
217
$ this ->tempPath = Yii::getAlias ('@runtime/mpdf ' );
246
218
}
247
- $ s = DIRECTORY_SEPARATOR ;
248
- $ prefix = $ this ->tempPath . $ s ;
249
- static ::definePath ('_MPDF_TEMP_PATH ' , "{$ prefix }tmp {$ s }" );
250
- static ::definePath ('_MPDF_TTFONTDATAPATH ' , "{$ prefix }ttfontdata {$ s }" );
219
+ if (!file_exists ($ this ->tempPath )) {
220
+ mkdir ($ this ->tempPath );
221
+ }
251
222
}
252
223
253
224
/**
254
225
* Renders and returns the PDF output. Uses the class level property settings.
255
226
*
256
227
* @return mixed
257
228
* @throws InvalidConfigException
229
+ * @throws \Mpdf\MpdfException
230
+ * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
231
+ * @throws \setasign\Fpdi\PdfParser\PdfParserException
232
+ * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
258
233
*/
259
234
public function render ()
260
235
{
@@ -300,26 +275,26 @@ public function setApi()
300
275
* Fetches the content of the CSS file if supplied
301
276
*
302
277
* @return string
278
+ * @throws InvalidConfigException
303
279
*/
304
280
public function getCss ()
305
281
{
306
282
if (!empty ($ this ->_css )) {
307
283
return $ this ->_css ;
308
284
}
309
- $ this ->_css = '' ;
285
+ $ this ->_css = '' ;
310
286
if (!empty ($ this ->cssFile )) {
311
287
$ cssFiles = is_array ($ this ->cssFile ) ? $ this ->cssFile : [$ this ->cssFile ];
312
288
foreach ($ cssFiles as $ cssFile ) {
313
289
$ cssFile = Yii::getAlias ($ cssFile );
314
290
if (!empty ($ cssFile ) && file_exists ($ cssFile )) {
315
291
$ this ->_css .= file_get_contents ($ cssFile );
316
- }
317
- else {
292
+ } else {
318
293
throw new InvalidConfigException ("CSS File not found: ' {$ cssFile }'. " );
319
294
}
320
295
}
321
296
}
322
- $ this ->_css .= $ this ->cssInline ;
297
+ $ this ->_css .= $ this ->cssInline ;
323
298
return $ this ->_css ;
324
299
}
325
300
@@ -374,6 +349,10 @@ public function execute($method, $params = [])
374
349
*
375
350
* @return mixed
376
351
* @throws InvalidConfigException
352
+ * @throws \Mpdf\MpdfException
353
+ * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
354
+ * @throws \setasign\Fpdi\PdfParser\PdfParserException
355
+ * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
377
356
*/
378
357
public function output ($ content = '' , $ file = '' , $ dest = self ::DEST_BROWSER )
379
358
{
@@ -392,7 +371,6 @@ public function output($content = '', $file = '', $dest = self::DEST_BROWSER)
392
371
$ api ->WriteHTML ($ content );
393
372
}
394
373
if ($ pdfAttachments ) {
395
- $ api ->SetImportUse ();
396
374
$ api ->SetHeader (null );
397
375
$ api ->SetFooter (null );
398
376
foreach ($ pdfAttachments as $ attachment ) {
@@ -430,7 +408,7 @@ public function output($content = '', $file = '', $dest = self::DEST_BROWSER)
430
408
/**
431
409
* Parse the format automatically based on the orientation
432
410
*/
433
- protected function parseFormat ()
411
+ public function parseFormat ()
434
412
{
435
413
$ landscape = self ::ORIENT_LANDSCAPE ;
436
414
$ tag = '- ' . $ landscape ;
@@ -444,9 +422,18 @@ protected function parseFormat()
444
422
*
445
423
* @param Mpdf $api the Mpdf API instance
446
424
* @param string $attachment the attachment name
425
+ * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
426
+ * @throws \setasign\Fpdi\PdfParser\PdfParserException
427
+ * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
447
428
*/
448
- private function writePdfAttachment ($ api , $ attachment )
429
+ public function writePdfAttachment ($ api = null , $ attachment = null )
449
430
{
431
+ if ($ attachment === null ) {
432
+ return ;
433
+ }
434
+ if ($ api === null ) {
435
+ $ api = $ this ->getApi ();
436
+ }
450
437
$ pageCount = $ api ->SetSourceFile ($ attachment );
451
438
for ($ i = 1 ; $ i <= $ pageCount ; $ i ++) {
452
439
$ api ->AddPage ();
0 commit comments