-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeasyPrintWrapper.php
280 lines (251 loc) · 6.16 KB
/
WeasyPrintWrapper.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace Fruitcake\WeasyPrint;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\View;
use Illuminate\Contracts\Support\Renderable;
use Pontedilana\PhpWeasyPrint\Pdf;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* A Laravel wrapper for WeasyPrint PDF
*
* @package laravel-weasyprint
* @author Fruitcake
*/
class WeasyPrintWrapper
{
/**
* @var Pdf
*/
protected $weasy;
/**
* @var array
*/
protected $options = array();
/**
* @var string
*/
protected $html;
/**
* @var string
*/
protected $file;
/**
* @param Pdf $weasy
*/
public function __construct(Pdf $weasy)
{
$this->weasy = $weasy;
}
/**
* Get the WeasyPrint instance.
*
* @return Pdf
*/
public function weasy()
{
return $this->weasy;
}
/**
* Set temporary folder
*
* @param string $path
*/
public function setTemporaryFolder($path)
{
$this->weasy->setTemporaryFolder($path);
return $this;
}
/**
* Set the paper size (default A4)
*
* @param string $paper
* @param string $orientation
* @return $this
*/
public function setPaper($paper, $orientation = null)
{
$this->weasy->setOption('page-size', $paper);
if ($orientation) {
$this->weasy->setOption('orientation', $orientation);
}
return $this;
}
/**
* Set the orientation (default portrait)
*
* @param string $orientation
* @return $this
*/
public function setOrientation($orientation)
{
$this->weasy->setOption('orientation', $orientation);
return $this;
}
/**
* Show or hide warnings
*
* @param bool $warnings
* @return $this
* @deprecated
*/
public function setWarnings($warnings)
{
//Doesn't do anything
return $this;
}
/**
* @param string $name
* @param mixed $value
* @return $this
*/
public function setOption($name, $value)
{
if ($value instanceof Renderable) {
$value = $value->render();
}
$this->weasy->setOption($name, $value);
return $this;
}
/**
* @param array $options
* @return $this
*/
public function setOptions($options)
{
$this->weasy->setOptions($options);
return $this;
}
/**
* Load a HTML string
*
* @param Array|string|Renderable $html
* @return $this
*/
public function loadHTML($html)
{
if ($html instanceof Renderable) {
$html = $html->render();
}
$this->html = $html;
$this->file = null;
return $this;
}
/**
* Load a HTML file
*
* @param string $file
* @return $this
*/
public function loadFile($file)
{
$this->html = null;
$this->file = $file;
return $this;
}
/**
* Load a View and convert to HTML
*
* @param string $view
* @param array $data
* @param array $mergeData
* @return $this
*/
public function loadView($view, $data = array(), $mergeData = array())
{
$view = View::make($view, $data, $mergeData);
return $this->loadHTML($view);
}
/**
* Output the PDF as a string.
*
* @return string The rendered PDF as string
* @throws \InvalidArgumentException
*/
public function output()
{
if ($this->html) {
return $this->weasy->getOutputFromHtml($this->html, $this->options);
}
if ($this->file) {
return $this->weasy->getOutput($this->file, $this->options);
}
throw new \InvalidArgumentException('PDF Generator requires a html or file in order to produce output.');
}
/**
* Save the PDF to a file
*
* @param $filename
* @return $this
*/
public function save($filename, $overwrite = false)
{
if ($this->html) {
$this->weasy->generateFromHtml($this->html, $filename, $this->options, $overwrite);
} elseif ($this->file) {
$this->weasy->generate($this->file, $filename, $this->options, $overwrite);
}
return $this;
}
/**
* Make the PDF downloadable by the user
*
* @param string $filename
* @return \Illuminate\Http\Response
*/
public function download($filename = 'document.pdf')
{
return new Response($this->output(), 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="' . $filename . '"'
));
}
/**
* Return a response with the PDF to show in the browser
*
* @param string $filename
* @return \Illuminate\Http\Response
*/
public function inline($filename = 'document.pdf')
{
return new Response($this->output(), 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . $filename . '"',
));
}
/**
* Return a response with the PDF to show in the browser
*
* @param string $filename
* @return \Symfony\Component\HttpFoundation\StreamedResponse
* @deprecated use inline() instead
*/
public function stream($filename = 'document.pdf')
{
return new StreamedResponse(function () {
echo $this->output();
}, 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . $filename . '"',
));
}
/**
* Call WeasyPrint instance.
*
* Also shortcut's
* ->html => loadHtml
* ->view => loadView
* ->file => loadFile
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, array $arguments)
{
$method = 'load' . ucfirst($name);
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $arguments);
}
return call_user_func_array(array($this->weasy, $name), $arguments);
}
}