1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\Snappy; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Use this class to transform a html/a url to a pdf. |
7
|
|
|
* |
8
|
|
|
* @author Matthieu Bontemps <[email protected]> |
9
|
|
|
* @author Antoine Hérault <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Pdf extends AbstractGenerator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $optionsWithContentCheck = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function __construct(string $binary = null, array $options = [], array $env = null) |
22
|
|
|
{ |
23
|
|
|
$this->setDefaultExtension('pdf'); |
24
|
|
|
$this->setOptionsWithContentCheck(); |
25
|
|
|
|
26
|
|
|
parent::__construct($binary, $options, $env); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function generate($input, $output, array $options = [], $overwrite = false) |
33
|
|
|
{ |
34
|
|
|
$options = $this->handleOptions($this->mergeOptions($options)); |
|
|
|
|
35
|
|
|
|
36
|
|
|
parent::generate($input, $output, $options, $overwrite); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Handle options to transform HTML strings into temporary files containing HTML. |
41
|
|
|
* |
42
|
|
|
* @param array $options |
43
|
|
|
* |
44
|
|
|
* @return array $options Transformed options |
45
|
|
|
*/ |
46
|
|
|
protected function handleOptions(array $options = []): array |
47
|
|
|
{ |
48
|
|
|
foreach ($options as $option => $value) { |
49
|
|
|
if (null === $value) { |
50
|
|
|
unset($options[$option]); |
51
|
|
|
|
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!empty($value) && \array_key_exists($option, $this->optionsWithContentCheck)) { |
56
|
|
|
$saveToTempFile = !$this->isFile($value) && !$this->isOptionUrl($value); |
57
|
|
|
$fetchUrlContent = $option === 'xsl-style-sheet' && $this->isOptionUrl($value); |
58
|
|
|
|
59
|
|
|
if ($saveToTempFile || $fetchUrlContent) { |
60
|
|
|
$fileContent = $fetchUrlContent ? \file_get_contents($value) : $value; |
61
|
|
|
$options[$option] = $this->createTemporaryFile($fileContent, $this->optionsWithContentCheck[$option]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $options; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Convert option content or url to file if it is needed. |
71
|
|
|
* |
72
|
|
|
* @param mixed $option |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function isOptionUrl($option): bool |
77
|
|
|
{ |
78
|
|
|
return (bool) \filter_var($option, \FILTER_VALIDATE_URL); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
protected function configure(): void |
85
|
|
|
{ |
86
|
|
|
$this->addOptions([ |
87
|
|
|
// Global options |
88
|
|
|
'collate' => null, |
89
|
|
|
'no-collate' => null, |
90
|
|
|
'cookie-jar' => null, |
91
|
|
|
'copies' => null, |
92
|
|
|
'dpi' => null, |
93
|
|
|
'extended-help' => null, |
94
|
|
|
'grayscale' => null, |
95
|
|
|
'help' => null, |
96
|
|
|
'htmldoc' => null, |
97
|
|
|
'ignore-load-errors' => null, // old v0.9 |
98
|
|
|
'image-dpi' => null, |
99
|
|
|
'image-quality' => null, |
100
|
|
|
'license' => null, |
101
|
|
|
'log-level' => null, |
102
|
|
|
'lowquality' => true, |
103
|
|
|
'manpage' => null, |
104
|
|
|
'margin-bottom' => null, |
105
|
|
|
'margin-left' => null, |
106
|
|
|
'margin-right' => null, |
107
|
|
|
'margin-top' => null, |
108
|
|
|
'orientation' => null, |
109
|
|
|
'page-height' => null, |
110
|
|
|
'page-size' => null, |
111
|
|
|
'page-width' => null, |
112
|
|
|
'no-pdf-compression' => null, |
113
|
|
|
'quiet' => null, |
114
|
|
|
'read-args-from-stdin' => null, |
115
|
|
|
'readme' => null, |
116
|
|
|
'title' => null, |
117
|
|
|
'use-xserver' => null, |
118
|
|
|
'version' => null, |
119
|
|
|
// Outline options |
120
|
|
|
'dump-default-toc-xsl' => null, |
121
|
|
|
'dump-outline' => null, |
122
|
|
|
'outline' => null, |
123
|
|
|
'no-outline' => null, |
124
|
|
|
'outline-depth' => null, |
125
|
|
|
'output-format' => null, |
126
|
|
|
// Page options |
127
|
|
|
'allow' => null, |
128
|
|
|
'background' => null, |
129
|
|
|
'no-background' => null, |
130
|
|
|
'bypass-proxy-for' => null, |
131
|
|
|
'cache-dir' => null, |
132
|
|
|
'checkbox-checked-svg' => null, |
133
|
|
|
'checkbox-svg' => null, |
134
|
|
|
'cookie' => null, |
135
|
|
|
'custom-header' => null, |
136
|
|
|
'custom-header-propagation' => null, |
137
|
|
|
'no-custom-header-propagation' => null, |
138
|
|
|
'debug-javascript' => null, |
139
|
|
|
'no-debug-javascript' => null, |
140
|
|
|
'default-header' => null, |
141
|
|
|
'encoding' => null, |
142
|
|
|
'disable-external-links' => null, |
143
|
|
|
'enable-external-links' => null, |
144
|
|
|
'disable-forms' => null, |
145
|
|
|
'enable-forms' => null, |
146
|
|
|
'images' => null, |
147
|
|
|
'no-images' => null, |
148
|
|
|
'disable-internal-links' => null, |
149
|
|
|
'enable-internal-links' => null, |
150
|
|
|
'disable-javascript' => null, |
151
|
|
|
'enable-javascript' => null, |
152
|
|
|
'javascript-delay' => null, |
153
|
|
|
'keep-relative-links' => null, |
154
|
|
|
'load-error-handling' => null, |
155
|
|
|
'load-media-error-handling' => null, |
156
|
|
|
'disable-local-file-access' => null, |
157
|
|
|
'enable-local-file-access' => null, |
158
|
|
|
'minimum-font-size' => null, |
159
|
|
|
'exclude-from-outline' => null, |
160
|
|
|
'include-in-outline' => null, |
161
|
|
|
'page-offset' => null, |
162
|
|
|
'password' => null, |
163
|
|
|
'disable-plugins' => null, |
164
|
|
|
'enable-plugins' => null, |
165
|
|
|
'post' => null, |
166
|
|
|
'post-file' => null, |
167
|
|
|
'print-media-type' => null, |
168
|
|
|
'no-print-media-type' => null, |
169
|
|
|
'proxy' => null, |
170
|
|
|
'proxy-hostname-lookup' => null, |
171
|
|
|
'radiobutton-checked-svg' => null, |
172
|
|
|
'radiobutton-svg' => null, |
173
|
|
|
'redirect-delay' => null, // old v0.9 |
174
|
|
|
'resolve-relative-links' => null, |
175
|
|
|
'run-script' => null, |
176
|
|
|
'disable-smart-shrinking' => null, |
177
|
|
|
'enable-smart-shrinking' => null, |
178
|
|
|
'ssl-crt-path' => null, |
179
|
|
|
'ssl-key-password' => null, |
180
|
|
|
'ssl-key-path' => null, |
181
|
|
|
'stop-slow-scripts' => null, |
182
|
|
|
'no-stop-slow-scripts' => null, |
183
|
|
|
'disable-toc-back-links' => null, |
184
|
|
|
'enable-toc-back-links' => null, |
185
|
|
|
'user-style-sheet' => null, |
186
|
|
|
'username' => null, |
187
|
|
|
'viewport-size' => null, |
188
|
|
|
'window-status' => null, |
189
|
|
|
'zoom' => null, |
190
|
|
|
// Headers and footer options |
191
|
|
|
'footer-center' => null, |
192
|
|
|
'footer-font-name' => null, |
193
|
|
|
'footer-font-size' => null, |
194
|
|
|
'footer-html' => null, |
195
|
|
|
'footer-left' => null, |
196
|
|
|
'footer-line' => null, |
197
|
|
|
'no-footer-line' => null, |
198
|
|
|
'footer-right' => null, |
199
|
|
|
'footer-spacing' => null, |
200
|
|
|
'header-center' => null, |
201
|
|
|
'header-font-name' => null, |
202
|
|
|
'header-font-size' => null, |
203
|
|
|
'header-html' => null, |
204
|
|
|
'header-left' => null, |
205
|
|
|
'header-line' => null, |
206
|
|
|
'no-header-line' => null, |
207
|
|
|
'header-right' => null, |
208
|
|
|
'header-spacing' => null, |
209
|
|
|
'replace' => null, |
210
|
|
|
// Cover object |
211
|
|
|
'cover' => null, |
212
|
|
|
// TOC object |
213
|
|
|
'toc' => null, |
214
|
|
|
// TOC options |
215
|
|
|
'disable-dotted-lines' => null, |
216
|
|
|
'toc-depth' => null, // old v0.9 |
217
|
|
|
'toc-font-name' => null, // old v0.9 |
218
|
|
|
'toc-l1-font-size' => null, // old v0.9 |
219
|
|
|
'toc-header-text' => null, |
220
|
|
|
'toc-header-font-name' => null, // old v0.9 |
221
|
|
|
'toc-header-font-size' => null, // old v0.9 |
222
|
|
|
'toc-level-indentation' => null, |
223
|
|
|
'disable-toc-links' => null, |
224
|
|
|
'toc-text-size-shrink' => null, |
225
|
|
|
'xsl-style-sheet' => null, |
226
|
|
|
]); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Array with options which require to store the content of the option before passing it to wkhtmltopdf. |
231
|
|
|
*/ |
232
|
|
|
protected function setOptionsWithContentCheck(): self |
233
|
|
|
{ |
234
|
|
|
$this->optionsWithContentCheck = [ |
235
|
|
|
'header-html' => 'html', |
236
|
|
|
'footer-html' => 'html', |
237
|
|
|
'cover' => 'html', |
238
|
|
|
'xsl-style-sheet' => 'xsl', |
239
|
|
|
]; |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|