1 | <?php |
||
13 | class PDF |
||
14 | { |
||
15 | private $httpClient; |
||
16 | private $view; |
||
17 | private $files; |
||
18 | private $config; |
||
19 | private $params; |
||
20 | private $pdf; |
||
21 | |||
22 | 14 | public function __construct(Client $httpClient, ConfigRepository $config, Filesystem $files, ViewFactory $view) |
|
30 | |||
31 | /** |
||
32 | * @param \Illuminate\Contracts\Config\Repository $config |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | 14 | private function prepareConfig(ConfigRepository $config) |
|
40 | |||
41 | /** |
||
42 | * @return \Illuminate\Support\Collection |
||
43 | */ |
||
44 | 14 | private function prepareParams() |
|
45 | { |
||
46 | 14 | $params = collect($this->config['default_params']); |
|
47 | 14 | if ($this->config['sandbox']) { |
|
48 | 14 | $params['test'] = 1; |
|
49 | 14 | } |
|
50 | |||
51 | 14 | return $params; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Load HTML and encoding from an URL. |
||
56 | * |
||
57 | * @param $url |
||
58 | * |
||
59 | * @return $this |
||
60 | */ |
||
61 | 1 | public function loadUrl($url) |
|
62 | { |
||
63 | 1 | $this->params['document_url'] = urlencode($url); |
|
64 | |||
65 | 1 | if (!empty($this->config['secret_keyword'])) { |
|
66 | 1 | $this->params['secret_key'] = md5($url . $this->config['secret_keyword']); |
|
67 | 1 | } |
|
68 | |||
69 | 1 | return $this; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Load a View and convert it to HTML. |
||
74 | * |
||
75 | * @param string $view |
||
76 | * @param array $data |
||
77 | * @param array $mergeData |
||
78 | * @param string $encoding |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | 1 | public function loadView($view, $data = [], $mergeData = [], $encoding = null) |
|
88 | |||
89 | /** |
||
90 | * Load a HTML string. |
||
91 | * |
||
92 | * @param string $html |
||
93 | * @param string $encoding |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | 3 | public function loadHTML($html, $encoding = null) |
|
98 | { |
||
99 | 3 | $this->params['document_html'] = $html; |
|
100 | |||
101 | 3 | if ($encoding) { |
|
102 | 2 | $this->params['text_encoding'] = $encoding; |
|
103 | 2 | } |
|
104 | |||
105 | 3 | return $this; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Load a HTML file. |
||
110 | * |
||
111 | * @param string $file |
||
112 | */ |
||
113 | 1 | public function loadFile($file) |
|
114 | { |
||
115 | 1 | $html = file_get_contents($file); |
|
116 | 1 | $encoding = null; |
|
117 | 1 | if (isset($http_response_header)) { |
|
118 | 1 | foreach ($http_response_header as $_header) { |
|
119 | 1 | if (preg_match("@Content-Type:\s*[\w/]+;\s*?charset=([\S]+)@i", $_header, $matches)) { |
|
120 | 1 | $encoding = strtoupper($matches[1]); |
|
121 | 1 | break; |
|
122 | } |
||
123 | 1 | } |
|
124 | 1 | } |
|
125 | 1 | $this->loadHTML($html, $encoding); |
|
126 | 1 | } |
|
127 | |||
128 | /** |
||
129 | * Set the paper size and orientation. |
||
130 | * |
||
131 | * @param string $layout |
||
132 | * @param string $orientation |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | 1 | public function setPaper($layout, $orientation = 'portrait') |
|
143 | |||
144 | /** |
||
145 | * Return a response with the PDF to show in the browser. |
||
146 | * |
||
147 | * @param string $filename |
||
148 | * |
||
149 | * @throws \InvalidArgumentException |
||
150 | * |
||
151 | * @return \Illuminate\Http\Response |
||
152 | */ |
||
153 | 1 | public function stream($filename = 'document.pdf') |
|
157 | |||
158 | /** |
||
159 | * @param $filename |
||
160 | * @param $contentDisposition |
||
161 | * |
||
162 | * @throws \InvalidArgumentException |
||
163 | * |
||
164 | * @return \Illuminate\Http\Response |
||
165 | */ |
||
166 | 2 | private function makeResponse($filename, $contentDisposition) |
|
173 | |||
174 | /** |
||
175 | * Output the PDF as a string. |
||
176 | * |
||
177 | * @throws \Exception |
||
178 | * |
||
179 | * @return string The rendered PDF as string |
||
180 | */ |
||
181 | 4 | public function output() |
|
189 | |||
190 | /** |
||
191 | * @throws \Exception |
||
192 | */ |
||
193 | 4 | private function render() |
|
198 | |||
199 | /** |
||
200 | * @return array |
||
201 | */ |
||
202 | 12 | private function prepareRequest() |
|
214 | |||
215 | /** |
||
216 | * @param $uri |
||
217 | * @param $postParams |
||
218 | * |
||
219 | * @throws \RuntimeException |
||
220 | * @throws \MathieuTu\PDFLayer\Exceptions\PDFLayerException |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 4 | private function doRequest($uri, $postParams) |
|
239 | |||
240 | /** |
||
241 | * Make the PDF downloadable by the user. |
||
242 | * |
||
243 | * @param string $filename |
||
244 | * |
||
245 | * @throws \InvalidArgumentException |
||
246 | * @throws \Exception |
||
247 | * |
||
248 | * @return \Illuminate\Http\Response |
||
249 | */ |
||
250 | 1 | public function download($filename = 'document.pdf') |
|
254 | |||
255 | /** |
||
256 | * Save the PDF to a file. |
||
257 | * |
||
258 | * @param $filename |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | 1 | public function save($filename) |
|
268 | |||
269 | /** |
||
270 | * @param $name |
||
271 | * @param $arguments |
||
272 | * |
||
273 | * @throws \InvalidArgumentException |
||
274 | * @throws \BadMethodCallException |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | 1 | public function __call($name, $arguments) |
|
291 | |||
292 | /** |
||
293 | * @param $name |
||
294 | * @param $value |
||
295 | * |
||
296 | * @return mixed |
||
297 | */ |
||
298 | 1 | public function __set($name, $value) |
|
302 | |||
303 | /** |
||
304 | * Add some parameters. |
||
305 | * |
||
306 | * @param array $params |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | 1 | public function addParams(array $params) |
|
314 | |||
315 | /** |
||
316 | * Add/Replace some parameters. |
||
317 | * |
||
318 | * @param array $params |
||
319 | * @param bool $replace |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | 2 | public function setParams(array $params, $replace = false) |
|
333 | |||
334 | /** |
||
335 | * Replace some parameters. |
||
336 | * |
||
337 | * @param array $params |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | 1 | public function replaceParams(array $params) |
|
345 | |||
346 | /** |
||
347 | * See all the current parameters. |
||
348 | * |
||
349 | * @return array |
||
350 | */ |
||
351 | 1 | public function seeParams() |
|
355 | |||
356 | /** |
||
357 | * See the arguments which will be sent during the request. |
||
358 | * |
||
359 | * @param string $key |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | 8 | public function seeRequestArgs($key = null) |
|
373 | } |
||
374 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.