vendor/pimcore/pimcore/lib/Web2Print/Processor/WkHtmlToPdf.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Web2Print\Processor;
  15. use Pimcore\Config;
  16. use Pimcore\Event\DocumentEvents;
  17. use Pimcore\Event\Model\PrintConfigEvent;
  18. use Pimcore\Model\Document;
  19. use Pimcore\Tool\Console;
  20. use Pimcore\Web2Print\Processor;
  21. /**
  22.  * @deprecated and will be removed in Pimcore 11. Use either PdfReactor or HeadlessChrome instead.
  23.  */
  24. class WkHtmlToPdf extends Processor
  25. {
  26.     /**
  27.      * @var string
  28.      */
  29.     private $wkhtmltopdfBin;
  30.     /**
  31.      * @var string
  32.      */
  33.     private $options '';
  34.     /**
  35.      * @var \stdClass|null
  36.      */
  37.     protected $config;
  38.     /**
  39.      * @param string $wkhtmltopdfBin
  40.      * @param array $options key => value
  41.      */
  42.     public function __construct($wkhtmltopdfBin null$options null)
  43.     {
  44.         trigger_deprecation(
  45.             'pimcore/pimcore',
  46.             '10.5',
  47.             sprintf('%s has been deprecated and will be removed in Pimcore 11. Use %s or %s instead.',
  48.                 __CLASS__PdfReactor::class, HeadlessChrome::class)
  49.         );
  50.         $web2printConfig Config::getWeb2PrintConfig();
  51.         if (!empty($wkhtmltopdfBin)) {
  52.             $this->wkhtmltopdfBin $wkhtmltopdfBin;
  53.         } elseif ($web2printConfig->get('wkhtmltopdfBin')) {
  54.             $this->wkhtmltopdfBin $web2printConfig->get('wkhtmltopdfBin');
  55.         } elseif ($determined Console::getExecutable('wkhtmltopdf')) {
  56.             $this->wkhtmltopdfBin $determined;
  57.         }
  58.         if (empty($options)) {
  59.             if ($web2printConfig->get('wkhtml2pdfOptions')) {
  60.                 $options $web2printConfig->get('wkhtml2pdfOptions')->toArray();
  61.             }
  62.         }
  63.         if ($options) {
  64.             foreach ($options as $key => $value) {
  65.                 $this->options .= ' --' . (string)$key;
  66.                 if ($value !== null && $value !== '') {
  67.                     $this->options .= ' ' . (string)$value;
  68.                 }
  69.             }
  70.         } else {
  71.             $this->options '';
  72.         }
  73.     }
  74.     /**
  75.      * @internal
  76.      */
  77.     protected function buildPdf(Document\PrintAbstract $document$config)
  78.     {
  79.         $this->config $config;
  80.         $web2printConfig Config::getWeb2PrintConfig();
  81.         $params = ['document' => $document];
  82.         $this->updateStatus($document->getId(), 10'start_html_rendering');
  83.         $html $document->renderDocument($params);
  84.         $params['hostUrl'] = $config->protocol '://' $config->hostName;
  85.         if ($web2printConfig->get('wkhtml2pdfHostname')) {
  86.             $params['hostUrl'] = $config->protocol '://' $web2printConfig->get('wkhtml2pdfHostname');
  87.         }
  88.         $html $this->processHtml($html$params);
  89.         $this->updateStatus($document->getId(), 40'finished_html_rendering');
  90.         $this->updateStatus($document->getId(), 50'pdf_conversion');
  91.         $pdf $this->fromStringToStream($html);
  92.         $this->updateStatus($document->getId(), 100'saving_pdf_document');
  93.         return $pdf;
  94.     }
  95.     /**
  96.      * @internal
  97.      */
  98.     public function getProcessingOptions()
  99.     {
  100.         $event = new PrintConfigEvent($this, [
  101.             'options' => [],
  102.         ]);
  103.         \Pimcore::getEventDispatcher()->dispatch($eventDocumentEvents::PRINT_MODIFY_PROCESSING_OPTIONS);
  104.         return (array)$event->getArgument('options');
  105.     }
  106.     /**
  107.      * @param string $options
  108.      */
  109.     public function setOptions($options)
  110.     {
  111.         $this->options $options;
  112.     }
  113.     /**
  114.      * @return string
  115.      */
  116.     public function getOptions()
  117.     {
  118.         return $this->options;
  119.     }
  120.     /**
  121.      * @internal
  122.      */
  123.     public function getPdfFromString($html$params = [], $returnFilePath false)
  124.     {
  125.         if (!empty($params['adapterConfig'])) {
  126.             $this->setOptions($params['adapterConfig']);
  127.         }
  128.         $html $this->processHtml($html$params);
  129.         if ($returnFilePath) {
  130.             return $this->fromStringToFile($html$params['dstFile']);
  131.         } else {
  132.             return $this->fromStringToStream($html);
  133.         }
  134.     }
  135.     /**
  136.      * @param string $htmlString
  137.      * @param string $dstFile
  138.      *
  139.      * @return string
  140.      */
  141.     protected function fromStringToFile($htmlString$dstFile null)
  142.     {
  143.         $id uniqid('web2print_');
  144.         $tmpHtmlFile PIMCORE_SYSTEM_TEMP_DIRECTORY DIRECTORY_SEPARATOR $id '.htm';
  145.         file_put_contents($tmpHtmlFile$htmlString);
  146.         $pdfFile $this->convert($tmpHtmlFile$dstFile);
  147.         @unlink($tmpHtmlFile);
  148.         return $pdfFile;
  149.     }
  150.     /**
  151.      * @param string $htmlString
  152.      *
  153.      * @return string
  154.      */
  155.     protected function fromStringToStream($htmlString)
  156.     {
  157.         $tmpFile $this->fromStringToFile($htmlString);
  158.         $stream file_get_contents($tmpFile);
  159.         @unlink($tmpFile);
  160.         return $stream;
  161.     }
  162.     /**
  163.      * @param string $srcUrl
  164.      * @param string|null $dstFile
  165.      *
  166.      * @return string
  167.      *
  168.      * @throws \Exception
  169.      */
  170.     protected function convert($srcUrl$dstFile null)
  171.     {
  172.         if (empty($dstFile)) {
  173.             $dstFile PIMCORE_SYSTEM_TEMP_DIRECTORY DIRECTORY_SEPARATOR uniqid('web2print_') . '.pdf';
  174.         }
  175.         if (empty($srcUrl) || empty($this->wkhtmltopdfBin)) {
  176.             throw new \Exception('srcUrl || wkhtmltopdfBin is empty!');
  177.         }
  178.         $retVal 0;
  179.         $event = new PrintConfigEvent($this, [
  180.             'wkhtmltopdfBin' => $this->wkhtmltopdfBin,
  181.             'options' => $this->options,
  182.             'srcUrl' => $srcUrl,
  183.             'dstFile' => $dstFile,
  184.             'config' => $this->config,
  185.         ]);
  186.         \Pimcore::getEventDispatcher()->dispatch($eventDocumentEvents::PRINT_MODIFY_PROCESSING_CONFIG);
  187.         $params $event->getArguments();
  188.         $cmd $params['cmd'] ?? null;
  189.         if (!$cmd) {
  190.             $cmd $params['wkhtmltopdfBin'] . ' ' $params['options'] . ' ' escapeshellarg($params['srcUrl']) . ' ' escapeshellarg($params['dstFile']);
  191.         }
  192.         exec($cmd$output$retVal);
  193.         if ($retVal != && $retVal != 1) {
  194.             throw new \Exception('wkhtmltopdf reported error (' $retVal "):\n" implode("\n"$output) . "\ncommand was: " $cmd);
  195.         }
  196.         return $dstFile;
  197.     }
  198. }