| 1: | <?php |
| 2: | namespace TIC\DpdfBundle\Base; |
| 3: | |
| 4: | use Twig\Environment; |
| 5: | use Symfony\Contracts\Translation\TranslatorInterface; |
| 6: | use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface; |
| 7: | |
| 8: | use Symfony\Component\HttpFoundation\Response; |
| 9: | use Symfony\Component\HttpFoundation\StreamedResponse; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | abstract class PDFService |
| 16: | { |
| 17: | protected $twig; |
| 18: | protected $encore; |
| 19: | protected $temp_files = array(); |
| 20: | protected $debug = false; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | const MIME_TYPES = array( |
| 26: | 'PDF' => [ ".pdf", "application/pdf" ], |
| 27: | 'PNG' => [ ".png", "image/png" ], |
| 28: | |
| 29: | ); |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | protected $confs = array( |
| 35: | 'debug' => false, |
| 36: | 'generator' => "auto", |
| 37: | 'pdflib_license' => null, |
| 38: | 'media_type' => "screen", |
| 39: | 'paper_size' => "A4", |
| 40: | 'orientation' => "portrait", |
| 41: | 'default_font' => "DejaVu Sans", |
| 42: | 'font_ratio' => 1.1, |
| 43: | 'dpi' => 96, |
| 44: | 'default_twigview' => "layout_print", |
| 45: | 'default_filename' => "document", |
| 46: | 'base_url' => null, |
| 47: | ); |
| 48: | protected $paths = array( |
| 49: | 'root_dir' => null, |
| 50: | 'path_source' => null, |
| 51: | 'path_output' => null, |
| 52: | 'path_logdir' => null, |
| 53: | 'path_tmpdir' => null, |
| 54: | 'wkhtml_pdf' => __DIR__ . '../../../../bin/wkhtmltopdf-amd64', |
| 55: | 'wkhtml_image' => __DIR__ . '../../../../bin/wkhtmltoimage-amd64', |
| 56: | ); |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public function __construct(?array $confs = [], ?array $paths = [], ?Environment $twig = null, ?TranslatorInterface $translator = null, ?EntrypointLookupInterface $encore = null) |
| 68: | { |
| 69: | $this->twig = $twig; |
| 70: | $this->trans = $translator; |
| 71: | $this->encore = $encore; |
| 72: | $this->confs = \array_merge($this->confs, $confs); |
| 73: | $this->paths = \array_merge($this->paths, $paths); |
| 74: | if (isset($this->confs['debug'])) $this->debug = (bool)$this->confs['debug']; |
| 75: | |
| 76: | } |
| 77: | |
| 78: | public function __destruct() |
| 79: | { |
| 80: | $this->purgeTempFiles(); |
| 81: | } |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | protected function purgeTempFiles() |
| 87: | { |
| 88: | if ($this->debug) return; |
| 89: | |
| 90: | foreach ($this->temp_files as $file_path) { |
| 91: | if (\file_exists($file_path)) \unlink($file_path); |
| 92: | } |
| 93: | $this->temp_files = array(); |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | protected function createTempFile(string $prefix = "", ?string $suffix = null, string $file_data = null): string |
| 105: | { |
| 106: | $folder = ""; |
| 107: | if (\strlen($prefix) && (\substr($prefix,0,1) === "/")) { |
| 108: | $folder = $prefix; |
| 109: | $prefix = ""; |
| 110: | } |
| 111: | |
| 112: | $tmpdir = \sys_get_temp_dir(); |
| 113: | if (! empty($this->paths['path_tmpdir'])) { |
| 114: | if (! \is_dir($this->paths['path_tmpdir'].$folder)) @\mkdir($this->paths['path_tmpdir'].$folder, 0750, true); |
| 115: | $tmpdir = $this->paths['path_tmpdir'].$folder; |
| 116: | } |
| 117: | |
| 118: | if (! \is_writable($tmpdir)) |
| 119: | throw new RuntimeException(\sprintf("Unable to write in directory: %s\n", $tmpdir)); |
| 120: | |
| 121: | $file_path = $tmpdir . "/" . \uniqid($prefix, true); |
| 122: | if (null !== $suffix) $file_path.= "." . $suffix; |
| 123: | |
| 124: | if (null !== $file_data) \file_put_contents($file_path, $file_data); |
| 125: | $this->temp_files[] = $file_path; |
| 126: | return $file_path; |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | abstract public function initEngine(array $pdf_options = []): PDFService; |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | abstract public function loadHtmlFile(string $html_file, array $pdf_options = []): PDFService; |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | abstract public function loadHtmlData(string $html_data, array $pdf_options = []): PDFService; |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | public function loadTwigView(?string $twig_view = null, array $view_params = [], array $pdf_options = []): PDFService |
| 169: | { |
| 170: | if (! $this->twig) throw new \Exception("Twig environment not found!"); |
| 171: | if ($this->encore) $this->encore->reset(); |
| 172: | |
| 173: | if ($this->trans && isset($view_params['locale']) && \strlen($view_params['locale'])) { |
| 174: | $orig_locale = $this->trans->getLocale(); |
| 175: | $this->trans->setLocale($view_params['locale']); |
| 176: | } |
| 177: | $html_data = $this->twig->render($this->fixTwigView($twig_view), $view_params); |
| 178: | if (isset($orig_locale)) $this->trans->setLocale($orig_locale); |
| 179: | |
| 180: | return $this->loadHtmlData($html_data, $pdf_options); |
| 181: | } |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | abstract public function renderData(bool $nocompress = false): string; |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | public function renderFile(?string $filepath = null, bool $nocompress = false): string |
| 201: | { |
| 202: | $filepath = $this->fixFilepath($filepath); |
| 203: | \file_put_contents($filepath, $this->renderData($nocompress)); |
| 204: | return $filepath; |
| 205: | } |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | public function renderResponse(?string $filename = null, bool $inline = false, bool $nocompress = false): Response |
| 216: | { |
| 217: | return new Response( |
| 218: | $this->renderData($nocompress), |
| 219: | 200, |
| 220: | $this->getHttpHeaders($filename, $inline) |
| 221: | ); |
| 222: | } |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | public function renderStream(?string $filename = null, bool $inline = false, bool $nocompress = false): StreamedResponse |
| 233: | { |
| 234: | return new StreamedResponse( |
| 235: | function() use ($nocompress) { echo $this->renderData($nocompress); }, |
| 236: | 200, |
| 237: | $this->getHttpHeaders($filename, $inline) |
| 238: | ); |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | public function view2data(?string $twig_view = null, array $view_params = [], array $pdf_options = []): string |
| 252: | { |
| 253: | return $this->loadTwigView($twig_view, $view_params, $pdf_options)->renderData(); |
| 254: | } |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | public function view2file(?string $filepath, ?string $twig_view = null, array $view_params = [], array $pdf_options = []): string |
| 266: | { |
| 267: | return $this->loadTwigView($twig_view, $view_params, $pdf_options)->renderFile($filepath); |
| 268: | } |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | public function view2http(?string $filename, ?string $twig_view = null, array $view_params = [], array $pdf_options = []): StreamedResponse |
| 280: | { |
| 281: | return $this->loadTwigView($twig_view, $view_params, $pdf_options)->renderStream($filename); |
| 282: | } |
| 283: | |
| 284: | |
| 285: | |
| 286: | protected function fixTwigView(?string $twig_view = null): string |
| 287: | { |
| 288: | if ($twig_view === null) $twig_view = $this->confs['default_twigview']; |
| 289: | return \preg_match('/[^\/]+\.[^\/]+$/', $twig_view) ? $twig_view : $twig_view . ".html.twig"; |
| 290: | } |
| 291: | |
| 292: | protected function fixHtmlFile(string $html_file): string |
| 293: | { |
| 294: | |
| 295: | return (\substr($html_file,0,1) !== '/') ? $this->paths['path_source'] . '/' . $html_file : $html_file; |
| 296: | } |
| 297: | |
| 298: | protected function fixFilepath(?string $filepath = null): string |
| 299: | { |
| 300: | list($extension) = $this->getFileType(); |
| 301: | |
| 302: | |
| 303: | $folder = \is_string($filepath) ? \dirname($filepath . "X") : "."; |
| 304: | |
| 305: | if (\substr($folder,0,1) !== '/') $folder = $this->paths['path_output'] . '/' . $folder; |
| 306: | |
| 307: | if (! \is_dir($folder)) @\mkdir($folder, 0750, true); |
| 308: | |
| 309: | |
| 310: | $filename = (\is_string($filepath) && \substr($filepath,-1) !== '/') ? \trim(\basename($filepath, $extension)) : ""; |
| 311: | |
| 312: | if (! \strlen($filename)) $filename = \sprintf('%s-%s', \date('Ymd_His'), \bin2hex(\random_bytes(8))); |
| 313: | |
| 314: | return \str_replace('/./', '/', $folder) . '/' . $filename . $extension; |
| 315: | } |
| 316: | |
| 317: | protected function fixFilename(?string $filename = null, ?string $extension = null): string |
| 318: | { |
| 319: | if ($extension === null) list($extension) = $this->getFileType(); |
| 320: | $filename = \is_string($filename) ? \trim(\basename($filename, $extension)) : ""; |
| 321: | if (! \strlen($filename)) $filename = \trim(\basename($this->confs['default_filename'], $extension)); |
| 322: | return \str_replace('"', '', $filename) . $extension; |
| 323: | } |
| 324: | |
| 325: | protected function getHttpHeaders(?string $filename = null, bool $inline = false): array |
| 326: | { |
| 327: | list($extension, $mime_type) = $this->getFileType(); |
| 328: | return array( |
| 329: | 'Content-Type' => $mime_type, |
| 330: | 'Content-Disposition' => \sprintf('%s; filename="%s"', |
| 331: | ($inline) ? "inline" : "attachment", |
| 332: | $this->fixFilename($filename, $extension) |
| 333: | ), |
| 334: | ); |
| 335: | } |
| 336: | |
| 337: | protected function getFileType(string $type = "PDF"): ?array |
| 338: | { |
| 339: | if (! isset(self::MIME_TYPES[$type])) $type = "PDF"; |
| 340: | return self::MIME_TYPES[$type]; |
| 341: | } |
| 342: | |
| 343: | } |
| 344: | |