| 1: | <?php | 
| 2: | namespace TIC\DpdfBundle\Service; | 
| 3: |  | 
| 4: | use TIC\DpdfBundle\Base\PDFService as BaseService; | 
| 5: |  | 
| 6: | use Symfony\Component\Process\Process; | 
| 7: | use Symfony\Component\Process\Exception\ProcessFailedException; | 
| 8: |  | 
| 9: |  | 
| 10: |  | 
| 11: |  | 
| 12: |  | 
| 13: | class WkhtmlService extends BaseService | 
| 14: | { | 
| 15: | protected $cmd_bin; | 
| 16: | protected $cmd_opt = array(); | 
| 17: | protected $cmd_src; | 
| 18: | protected $cmd_out; | 
| 19: | protected $timeout = 60; | 
| 20: |  | 
| 21: |  | 
| 22: |  | 
| 23: |  | 
| 24: |  | 
| 25: |  | 
| 26: |  | 
| 27: |  | 
| 28: |  | 
| 29: | public function initEngine(array $pdf_options = []): WkhtmlService | 
| 30: | { | 
| 31: | $opts = array(); | 
| 32: |  | 
| 33: |  | 
| 34: | $pdf_options+= $this->confs; | 
| 35: | if (isset($pdf_options['debug'])) $this->debug = (bool)$pdf_options['debug']; | 
| 36: |  | 
| 37: |  | 
| 38: | switch (\strtolower($pdf_options['generator'])) { | 
| 39: | case "image": | 
| 40: | case "png": | 
| 41: | case "gd": | 
| 42: | $isPDF = false; | 
| 43: | $this->cmd_bin = $this->paths['wkhtml_image']; break; | 
| 44: | default: | 
| 45: | $isPDF = true; | 
| 46: | $this->cmd_bin = $this->paths['wkhtml_pdf']; break; | 
| 47: | } | 
| 48: |  | 
| 49: |  | 
| 50: | if ($isPDF) { | 
| 51: |  | 
| 52: | if ($pdf_options['paper_size'] && $isPDF)   { | 
| 53: | $opts[] = "--page-size"; | 
| 54: | $opts[] = $pdf_options['paper_size']; | 
| 55: | } | 
| 56: |  | 
| 57: | if ($pdf_options['orientation']) { | 
| 58: | $opts[] = "--orientation"; | 
| 59: | $opts[] = $pdf_options['orientation']; | 
| 60: | } | 
| 61: |  | 
| 62: |  | 
| 63: | if ($pdf_options['media_type'] === 'print') { | 
| 64: | $opts[] = "--print-media-type"; | 
| 65: | } else { | 
| 66: | $opts[] = "--no-print-media-type"; | 
| 67: | } | 
| 68: |  | 
| 69: |  | 
| 70: | if (! empty($pdf_options['pdi']) && \is_numeric($pdf_options['pdi'])) { | 
| 71: | $opts[] = "--dpi"; | 
| 72: | $opts[] = \intval($pdf_options['pdi']); | 
| 73: | $opts[] = "--image-dpi"; | 
| 74: | $opts[] = \intval($pdf_options['pdi']); | 
| 75: | } | 
| 76: |  | 
| 77: | if (isset($pdf_options['background']) && empty($pdf_options['background'])) { | 
| 78: | $opts[] = "--no-background"; | 
| 79: | } | 
| 80: |  | 
| 81: | if (isset($pdf_options['smart_shrinking']) && empty($pdf_options['smart_shrinking'])) { | 
| 82: | $opts[] = "--disable-smart-shrinking"; | 
| 83: | } | 
| 84: |  | 
| 85: | $opts[] = "--lowquality"; | 
| 86: |  | 
| 87: |  | 
| 88: |  | 
| 89: |  | 
| 90: |  | 
| 91: |  | 
| 92: |  | 
| 93: |  | 
| 94: |  | 
| 95: |  | 
| 96: |  | 
| 97: |  | 
| 98: |  | 
| 99: |  | 
| 100: | } | 
| 101: |  | 
| 102: |  | 
| 103: | if (! $isPDF) { | 
| 104: |  | 
| 105: | $opts[] = "--format"; | 
| 106: | $opts[] = "PNG"; | 
| 107: |  | 
| 108: |  | 
| 109: |  | 
| 110: |  | 
| 111: |  | 
| 112: |  | 
| 113: |  | 
| 114: |  | 
| 115: | } | 
| 116: |  | 
| 117: |  | 
| 118: | if (! empty($this->paths['path_tmpdir'])) { | 
| 119: | if (! \is_dir($this->paths['path_tmpdir']."/wkweb")) @\mkdir($this->paths['path_tmpdir']."/wkweb", 0750, true); | 
| 120: | $opts[] = "--cache-dir"; | 
| 121: | $opts[] = $this->paths['path_tmpdir']."/wkweb"; | 
| 122: | } | 
| 123: |  | 
| 124: | $opts[] = "--disable-local-file-access"; | 
| 125: |  | 
| 126: | if (! empty($this->paths['path_source'])) { | 
| 127: | $opts[] = "--allow"; | 
| 128: | $opts[] = $this->paths['path_source']; | 
| 129: | } | 
| 130: | if (! empty($this->paths['root_dir'])) { | 
| 131: | $opts[] = "--allow"; | 
| 132: | $opts[] = $this->paths['root_dir']; | 
| 133: | } | 
| 134: |  | 
| 135: |  | 
| 136: | if (isset($pdf_options['javascript']) && empty($pdf_options['javascript'])) { | 
| 137: | $opts[] = "--disable-javascript"; | 
| 138: | } | 
| 139: |  | 
| 140: | $opts[] = "--load-error-handling"; | 
| 141: | $opts[] = "ignore"; | 
| 142: |  | 
| 143: | $opts[] = "--load-media-error-handling"; | 
| 144: | $opts[] = "ignore"; | 
| 145: |  | 
| 146: | $opts[] = "--quiet"; | 
| 147: |  | 
| 148: |  | 
| 149: |  | 
| 150: |  | 
| 151: |  | 
| 152: |  | 
| 153: | if ($this->debug) dump($opts); | 
| 154: |  | 
| 155: | $this->cmd_opt = $opts; | 
| 156: | return $this; | 
| 157: | } | 
| 158: |  | 
| 159: |  | 
| 160: |  | 
| 161: |  | 
| 162: |  | 
| 163: |  | 
| 164: |  | 
| 165: |  | 
| 166: |  | 
| 167: |  | 
| 168: | public function loadHtmlFile(string $html_file, array $pdf_options = []): WkhtmlService | 
| 169: | { | 
| 170: | $this->initEngine($pdf_options); | 
| 171: | $this->cmd_src = $this->fixHtmlFile($html_file); | 
| 172: | $this->cmd_out = $this->createTempFile("/wktmp", "out"); | 
| 173: | return $this; | 
| 174: | } | 
| 175: |  | 
| 176: |  | 
| 177: |  | 
| 178: |  | 
| 179: |  | 
| 180: |  | 
| 181: |  | 
| 182: |  | 
| 183: | public function loadHtmlData(string $html_data, array $pdf_options = []): WkhtmlService | 
| 184: | { | 
| 185: | $html_file = $this->createTempFile("/wktmp", "html", $html_data); | 
| 186: | return $this->loadHtmlFile($html_file, $pdf_options); | 
| 187: | } | 
| 188: |  | 
| 189: |  | 
| 190: |  | 
| 191: |  | 
| 192: |  | 
| 193: |  | 
| 194: |  | 
| 195: |  | 
| 196: |  | 
| 197: | public function renderData(bool $nocompress = false): string | 
| 198: | { | 
| 199: | list($status, $stdout, $stderr) = $this->executeCommand($nocompress); | 
| 200: | $this->checkCommandOutput($status, $stdout, $stderr, $this->cmd_out); | 
| 201: | return \file_get_contents($this->cmd_out); | 
| 202: | } | 
| 203: |  | 
| 204: |  | 
| 205: |  | 
| 206: |  | 
| 207: |  | 
| 208: |  | 
| 209: |  | 
| 210: |  | 
| 211: |  | 
| 212: | protected function executeCommand(bool $nocompress = false): array | 
| 213: | { | 
| 214: | if (! isset($this->cmd_bin)) | 
| 215: | throw new \LogicException("Initialisation error: wkhtml command is not defined!"); | 
| 216: |  | 
| 217: | $cmd_args = array(); | 
| 218: | if ($nocompress) $cmd_args[] = "--no-pdf-compression"; | 
| 219: | $cmd_args[] = $this->cmd_src; | 
| 220: | $cmd_args[] = $this->cmd_out; | 
| 221: |  | 
| 222: | $command = \array_merge([ $this->cmd_bin ], $this->cmd_opt, $cmd_args); | 
| 223: | $process = new Process($command); | 
| 224: | if ($this->debug) dump($command, $process->getCommandLine()); | 
| 225: |  | 
| 226: | if (isset($this->timeout)) $process->setTimeout($this->timeout); | 
| 227: | $process->run(); | 
| 228: |  | 
| 229: |  | 
| 230: | return [ | 
| 231: | $process->getExitCode(), | 
| 232: | $process->getOutput(), | 
| 233: | $process->getErrorOutput(), | 
| 234: | ]; | 
| 235: | } | 
| 236: |  | 
| 237: |  | 
| 238: |  | 
| 239: |  | 
| 240: |  | 
| 241: |  | 
| 242: |  | 
| 243: |  | 
| 244: |  | 
| 245: |  | 
| 246: |  | 
| 247: |  | 
| 248: |  | 
| 249: | protected function checkCommandOutput(int $status, string $stdout, string $stderr, string $output) | 
| 250: | { | 
| 251: | if ($this->debug) dump($status, $stdout, $stderr, $output); | 
| 252: | $dbg = "\n*** stderr:\n%s\n*** stdout:\n%s\n"; | 
| 253: |  | 
| 254: |  | 
| 255: | if (! \file_exists($output)) | 
| 256: | throw new \RuntimeException(\sprintf("Process failed: output not exists (%s)".$dbg, $output, $stderr, $stdout)); | 
| 257: | if (! \filesize($output)) | 
| 258: | throw new \RuntimeException(\sprintf("Process failed: output is empty (%s)".$dbg, $output, $stderr, $stdout)); | 
| 259: | } | 
| 260: |  | 
| 261: | protected function getFileType(string $type = "PDF"): ?array | 
| 262: | { | 
| 263: | if (isset($this->cmd_bin) && (\strpos($this->cmd_bin, "image") !== false)) $type = "PNG"; | 
| 264: | return parent::getFileType($type); | 
| 265: | } | 
| 266: |  | 
| 267: | } | 
| 268: |  |