This repository was archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMpdf.php
77 lines (68 loc) · 2.19 KB
/
Mpdf.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
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\html2pdf\converters;
use Yii;
use yii\helpers\ArrayHelper;
use yii2tech\html2pdf\BaseConverter;
/**
* Mpdf converts file using [mpdf](https://github.com/mpdf/mpdf) library.
*
* This converter requires `mpdf` library to be installed. This can be done via composer:
*
* ```
* composer require --prefer-dist "mpdf/mpdf:^6.0.0|^7.0.0"
* ```
*
* @see http://mpdf.github.io
* @see https://github.com/mpdf/mpdf
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class Mpdf extends BaseConverter
{
/**
* {@inheritdoc}
*/
protected function convertInternal($html, $outputFileName, $options)
{
$charset = ArrayHelper::remove($options, 'charset', Yii::$app->charset);
$pageSize = ArrayHelper::remove($options, 'pageSize', 'A4');
if (class_exists('Mpdf\Mpdf')) {
$config = [
'mode' => $charset,
'format' => $pageSize,
'tempDir' => Yii::getAlias(ArrayHelper::remove($options, 'tempDir', '@runtime')),
];
if (isset($options['fontDir'])) {
if (is_array($options['fontDir'])) {
$config['fontDir'] = array_map(['Yii', 'getAlias'], $config['fontDir']);
unset($options['fontDir']);
} else {
$options['fontDir'] = Yii::getAlias($options['fontDir']);
}
}
$pdf = new \Mpdf\Mpdf($config);
if (isset($options['fontDir'])) {
$pdf->AddFontDirectory($options['fontDir']);
unset($options['fontDir']);
}
} else {
$pdf = new \mPDF($charset, $pageSize);
}
foreach ($options as $name => $value) {
$setter = 'Set' . $name;
if (method_exists($pdf, $setter)) {
$pdf->$setter($value);
} else {
$pdf->$name = $value;
}
}
$pdf->WriteHTML($html);
$pdf->Output($outputFileName, 'F');
}
}