-
Notifications
You must be signed in to change notification settings - Fork 0
/
MpdfService.php
133 lines (113 loc) · 3.65 KB
/
MpdfService.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
<?php
namespace Symfgenus\MpdfWrapper;
use Mpdf\Mpdf;
use Symfony\Component\HttpFoundation\Response;
class MpdfService
{
private $tmp_dir;
public $pdf;
public function __construct(string $cache_dir)
{
$this->tmp_dir = $cache_dir . '/mpdf/';
if (!is_dir($this->tmp_dir)) {
mkdir($this->tmp_dir, 0777);
}
if (!is_writable($this->tmp_dir)) {
throw new \Exception("Temp directory not writeable: " . $this->tmp_dir);
}
return $this;
}
public function init(array $constructorArgs = [])
{
$this->getMpdf($constructorArgs);
return $this;
}
/**
* Get an instance of mPDF class
* @param array $constructorArgs arguments for mPDF constructor
* @return \mPDF
*/
public function getMpdf(array $constructorArgs = []): Mpdf
{
list(
$mode,
$format,
$default_font_size,
$default_font,
$mgl,
$mgr,
$mgt,
$mgb,
$mgh,
$mgf,
$orientation
) = $constructorArgs;
$defaultArgs = [
'tempDir' => $this->tmp_dir,
'showImageErrors' => true,
'debug' => true,
'mode' => $mode ?? 'utf-8',
'format' => $format ?? 'A4',
'default_font_size' => $default_font_size,
'default_font' => $default_font,
'margin_left' => $mgl,
'margin_right' => $mgr,
'margin_top' => $mgt,
'margin_bottom' => $mgb,
'margin_header' => $mgh,
'margin_footer' => $mgf,
'orientation' => $orientation ?? 'P',
];
$args = array_merge($defaultArgs, $constructorArgs);
if (!$this->pdf || !empty($args)) {
$this->pdf = new Mpdf($args);
}
return $this->pdf;
}
/**
* Returns a string which content is a PDF document
*/
public function generatePdf(string $html, array $argOptions = [])
{
//Calculate arguments
$defaultOptions = [
'constructorArgs' => ['', 'A4', 5, '', 15, 15, 10, 30, 10, 10, 'P'],
'outputFilename' => '',
'outputDest' => 'S',
];
$options = array_merge($defaultOptions, $argOptions);
//Add arguments to Output function
try {
if ($html) {
$mpdf = $this->getMpdf($options['constructorArgs']);
$mpdf->WriteHTML($html);
$content = $mpdf->Output($options['outputFilename'], $options['outputDest']);
} elseif ($this->pdf) {
$content = $this->pdf->Output($options['outputFilename'], $options['outputDest']);
}
} catch (\Mpdf\MpdfException $e) {
//// Note: safer fully qualified exception name used for catch
// Process the exception, log, print etc.
return $e->getMessage();
} catch (\Exception $e) {
// Note: safer fully qualified exception name used for catch
// Process the exception, log, print etc.
return $e->getMessage();
}
return $content;
}
/**
* Generates an instance of Response class with PDF document
*
* @param unknown $html
* @param array $argOptions
*/
public function generatePdfResponse($html = '', array $argOptions = []): Response
{
$response = new Response();
$response->headers->set('Content-Type', 'application/pdf');
$content = $this->generatePdf($html, $argOptions);
$response->setContent($content);
return $response;
}
}