Blame view
public/vendor/barryvdh/laravel-dompdf/src/PDF.php
8.32 KB
86143e36f Коммит вторник |
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
<?php namespace Barryvdh\DomPDF; use Dompdf\Adapter\CPDF; use Dompdf\Dompdf; use Dompdf\Options; use Exception; use Illuminate\Filesystem\Filesystem; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Http\Response; use Illuminate\Support\Facades\Storage; /** * A Laravel wrapper for Dompdf * * @package laravel-dompdf * @author Barry vd. Heuvel * * @method PDF setBaseHost(string $baseHost) * @method PDF setBasePath(string $basePath) * @method PDF setCanvas(\Dompdf\Canvas $canvas) * @method PDF setCallbacks(array $callbacks) * @method PDF setCss(\Dompdf\Css\Stylesheet $css) * @method PDF setDefaultView(string $defaultView, array $options) * @method PDF setDom(\DOMDocument $dom) * @method PDF setFontMetrics(\Dompdf\FontMetrics $fontMetrics) * @method PDF setHttpContext(resource|array $httpContext) * @method PDF setPaper(string|float[] $paper, string $orientation = 'portrait') * @method PDF setProtocol(string $protocol) * @method PDF setTree(\Dompdf\Frame\FrameTree $tree) * @method string getBaseHost() * @method string getBasePath() * @method \Dompdf\Canvas getCanvas() * @method array getCallbacks() * @method \Dompdf\Css\Stylesheet getCss() * @method \DOMDocument getDom() * @method \Dompdf\FontMetrics getFontMetrics() * @method resource getHttpContext() * @method Options getOptions() * @method \Dompdf\Frame\FrameTree getTree() * @method string getPaperOrientation() * @method float[] getPaperSize() * @method string getProtocol() */ class PDF { /** @var Dompdf */ protected $dompdf; /** @var \Illuminate\Contracts\Config\Repository */ protected $config; /** @var \Illuminate\Filesystem\Filesystem */ protected $files; /** @var \Illuminate\Contracts\View\Factory */ protected $view; /** @var bool */ protected $rendered = false; /** @var bool */ protected $showWarnings; /** @var string */ protected $public_path; public function __construct(Dompdf $dompdf, ConfigRepository $config, Filesystem $files, ViewFactory $view) { $this->dompdf = $dompdf; $this->config = $config; $this->files = $files; $this->view = $view; $this->showWarnings = $this->config->get('dompdf.show_warnings', false); } /** * Get the DomPDF instance */ public function getDomPDF(): Dompdf { return $this->dompdf; } /** * Show or hide warnings */ public function setWarnings(bool $warnings): self { $this->showWarnings = $warnings; return $this; } /** * Load a HTML string * * @param string|null $encoding Not used yet */ public function loadHTML(string $string, ?string $encoding = null): self { $string = $this->convertEntities($string); $this->dompdf->loadHtml($string, $encoding); $this->rendered = false; return $this; } /** * Load a HTML file */ public function loadFile(string $file): self { $this->dompdf->loadHtmlFile($file); $this->rendered = false; return $this; } /** * Add metadata info * @param array<string, string> $info */ public function addInfo(array $info): self { foreach ($info as $name => $value) { $this->dompdf->add_info($name, $value); } return $this; } /** * Load a View and convert to HTML * @param array<string, mixed> $data * @param array<string, mixed> $mergeData * @param string|null $encoding Not used yet */ public function loadView(string $view, array $data = [], array $mergeData = [], ?string $encoding = null): self { $html = $this->view->make($view, $data, $mergeData)->render(); return $this->loadHTML($html, $encoding); } /** * Set/Change an option (or array of options) in Dompdf * * @param array<string, mixed>|string $attribute * @param null|mixed $value */ public function setOption($attribute, $value = null): self { $this->dompdf->getOptions()->set($attribute, $value); return $this; } /** * Replace all the Options from DomPDF * * @param array<string, mixed> $options */ public function setOptions(array $options): self { $dompdfOptions = new Options(app()->make('dompdf.options')); $dompdfOptions->set($options); $this->dompdf->setOptions($dompdfOptions); return $this; } /** * Output the PDF as a string. * * The options parameter controls the output. Accepted options are: * * 'compress' = > 1 or 0 - apply content stream compression, this is * on (1) by default * * @param array<string, int> $options * * @return string The rendered PDF as string */ public function output(array $options = []): string { if (!$this->rendered) { $this->render(); } return (string) $this->dompdf->output($options); } /** * Save the PDF to a file */ public function save(string $filename, string $disk = null): self { $disk = $disk ?: $this->config->get('dompdf.disk'); if (! is_null($disk)) { Storage::disk($disk)->put($filename, $this->output()); return $this; } $this->files->put($filename, $this->output()); return $this; } /** * Make the PDF downloadable by the user */ public function download(string $filename = 'document.pdf'): Response { $output = $this->output(); return new Response($output, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="' . $filename . '"', 'Content-Length' => strlen($output), ]); } /** * Return a response with the PDF to show in the browser */ public function stream(string $filename = 'document.pdf'): Response { $output = $this->output(); return new Response($output, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename="' . $filename . '"', ]); } /** * Render the PDF */ public function render(): void { $this->dompdf->render(); if ($this->showWarnings) { global $_dompdf_warnings; if (!empty($_dompdf_warnings) && count($_dompdf_warnings)) { $warnings = ''; foreach ($_dompdf_warnings as $msg) { $warnings .= $msg . " "; } // $warnings .= $this->dompdf->get_canvas()->get_cpdf()->messages; if (!empty($warnings)) { throw new Exception($warnings); } } } $this->rendered = true; } /** @param array<string> $pc */ public function setEncryption(string $password, string $ownerpassword = '', array $pc = []): void { $this->render(); $canvas = $this->dompdf->getCanvas(); if (! $canvas instanceof CPDF) { throw new \RuntimeException('Encryption is only supported when using CPDF'); } $canvas->get_cpdf()->setEncryption($password, $ownerpassword, $pc); } protected function convertEntities(string $subject): string { if (false === $this->config->get('dompdf.convert_entities', true)) { return $subject; } $entities = [ '€' => '€', '£' => '£', ]; foreach ($entities as $search => $replace) { $subject = str_replace($search, $replace, $subject); } return $subject; } /** * Dynamically handle calls into the dompdf instance. * * @param string $method * @param array<mixed> $parameters * @return $this|mixed */ public function __call($method, $parameters) { if (method_exists($this, $method)) { return $this->$method(...$parameters); } if (method_exists($this->dompdf, $method)) { $return = $this->dompdf->$method(...$parameters); return $return == $this->dompdf ? $this : $return; } throw new \UnexpectedValueException("Method [{$method}] does not exist on PDF instance."); } } |