This repository has been 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 pathWkhtmltopdf.php
256 lines (227 loc) · 7.98 KB
/
Wkhtmltopdf.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?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\base\Exception;
use yii\helpers\FileHelper;
use yii\helpers\Inflector;
use yii2tech\html2pdf\BaseConverter;
/**
* Wkhtmltopdf converts file using [wkhtmltopdf](http://wkhtmltopdf.org/) utility.
*
* This converter requires `wkhtmltopdf` utility installed and being available via OS shell.
*
* Conversion options are converted into command line options, using {@see Inflector::camel2id()}.
* Available conversion options:
*
* - `pageSize`: string, page size, e.g. 'A4', 'Letter', etc.
* - `orientation`: string, page orientation: 'Portrait' or 'Landscape'.
* - `grayscale`: bool, whether PDF will be generated in grayscale.
* - `cover`: string, filename or URL which holds content for cover page.
* - `headerHtml`: string, filename or URL which holds content for pages header.
* - `footerHtml`: string, filename or URL which holds content for pages footer.
* - `coverContent`: string, HTML content for cover page.
* - `headerHtmlContent`: string, header HTML content.
* - `footerHtmlContent`: string, footer HTML content.
*
* Note: actual options list may vary depending on the version of 'wkhtmltopdf' you are using.
*
* @see http://wkhtmltopdf.org/
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class Wkhtmltopdf extends BaseConverter
{
/**
* @var string path to the 'wkhtmltopdf' command, for example: '/usr/local/bin/wkhtmltopdf'.
* Since 1.0.7 Yii alias can be used here, for example: '@app/bin/wkhtmltopdf'.
* Default is 'wkhtmltopdf' assuming 'wkhtmltopdf' command is available in OS shell.
*/
public $binPath = 'wkhtmltopdf';
/**
* @var string[] list of created temporary file names.
* @since 1.0.3
*/
private $tmpFiles = [];
/**
* Destructor.
* Ensures temporary files are deleted.
* @since 1.0.3
*/
public function __destruct()
{
$this->clearTmpFiles();
}
/**
* {@inheritdoc}
*/
public function convertFile($sourceFileName, $outputFileName, $options = [])
{
$options = array_merge($this->defaultOptions, $options);
$this->convertFileInternal($sourceFileName, $outputFileName, $options);
}
/**
* Converts given HTML file into PDF file.
* @param string $sourceFileName source HTML file.
* @param string $outputFileName output PDF file name.
* @param array $options conversion options.
* @throws Exception on failure.
*/
protected function convertFileInternal($sourceFileName, $outputFileName, $options)
{
try {
$command = Yii::getAlias($this->binPath);
foreach ($this->normalizeOptions($options) as $name => $value) {
$command .= $this->buildCommandOption($name, $value);
}
$command .= ' ' . escapeshellarg($sourceFileName) . ' ' . escapeshellarg($outputFileName);
$command .= ' 2>&1';
$outputLines = [];
exec($command, $outputLines, $exitCode);
if ($exitCode !== 0) {
throw new Exception("Unable to convert file '{$sourceFileName}': " . implode("\n", $outputLines));
}
} catch (\Exception $e) {
$this->clearTmpFiles();
throw $e;
} catch (\Throwable $e) {
$this->clearTmpFiles();
throw $e;
}
$this->clearTmpFiles();
}
/**
* {@inheritdoc}
*/
protected function convertInternal($html, $outputFileName, $options)
{
$sourceFileName = $this->createTmpFile($html, 'html'); // enforce '.html' extension to avoid 'Failed loading page' error
$this->convertFileInternal($sourceFileName, $outputFileName, $options);
}
/**
* Normalizes raw conversion options for the shell command composition.
* @param array $options raw conversion options
* @return array normalized options.
*/
protected function normalizeOptions($options)
{
$result = [];
foreach ($options as $name => $value) {
if (is_null($value) || $value === false) {
continue;
}
$normalizedName = Inflector::camel2id($name);
$result[$normalizedName] = $value;
}
$fileOptions = [
'header-html',
'footer-html',
'cover',
];
foreach ($fileOptions as $fileOption) {
$contentOption = $fileOption . '-content';
if (isset($result[$contentOption])) {
// enforce '.html' extension to avoid 'Failed loading page' error
$result[$fileOption] = $this->createTmpFile($result[$contentOption], 'html');
unset($result[$contentOption]);
}
}
// make sure 'toc' and 'cover' options to be last, so global options will not mix with them
uksort($result, function ($a, $b) {
if ($a === 'toc') {
return 1;
}
if ($b === 'toc') {
return -1;
}
if ($a === 'cover') {
return 1;
}
if ($b === 'cover') {
return -1;
}
return 0;
});
return $result;
}
/**
* Builds option for the shell command composition.
* @param string $name option name.
* @param mixed $value option value.
* @return string option shell representation.
* @since 1.0.2
*/
protected function buildCommandOption($name, $value)
{
$prefix = '--';
if (in_array($name, ['toc', 'cover'])) { // Don't add '--' in these options
$prefix = '';
}
$option = ' ' . $prefix . $name;
if ($value === true) {
return $option;
}
if (is_array($value)) { // Support repeatable options
$repeatableOptions = [];
foreach ($value as $k => $v) {
$repeatableOptions[] = $option . (is_string($k) ? ' ' . escapeshellarg($k) : '') . ' ' .escapeshellarg($v);
}
return implode(' ', $repeatableOptions);
}
return $option . ' ' . escapeshellarg($value);
}
/**
* Returns path to directory for the temporary files storage.
* Directory will be created if it does not yet exist.
* @return string file path.
* @throws \yii\base\Exception if the directory could not be created.
* @since 1.0.3
*/
protected function getTmpFilePath()
{
$tempPath = Yii::getAlias('@runtime/html2pdf');
FileHelper::createDirectory($tempPath);
return $tempPath;
}
/**
* @param string $content file content.
* @param null $extension file extension to be enforced.
* @return string generated file name.
* @throws Exception on failure.
* @since 1.0.3
*/
protected function createTmpFile($content, $extension = null)
{
$tempFileName = tempnam($this->getTmpFilePath(), 'wkhtmltopdf');
if ($tempFileName === false) {
throw new Exception('Unable to create temporary file.');
}
if ($extension !== null) {
// sometimes enforcing of file extension, like '.html', is needed since 'wkhtmltopdf' is sensitive to it.
$tempFileNameWithExtension = $tempFileName . '.' . $extension;
rename($tempFileName, $tempFileNameWithExtension);
$tempFileName = $tempFileNameWithExtension;
}
file_put_contents($tempFileName, $content);
$this->tmpFiles[] = $tempFileName;
return $tempFileName;
}
/**
* Removes temporary files.
* @since 1.0.3
*/
protected function clearTmpFiles()
{
foreach ($this->tmpFiles as $tmpFile) {
if (file_exists($tmpFile)) {
unlink($tmpFile);
}
}
$this->tmpFiles = [];
}
}