vendor/knplabs/knp-snappy/src/Knp/Snappy/Pdf.php line 21

Open in your IDE?
  1. <?php
  2. namespace Knp\Snappy;
  3. /**
  4.  * Use this class to transform a html/a url to a pdf.
  5.  *
  6.  * @author  Matthieu Bontemps <matthieu.bontemps@knplabs.com>
  7.  * @author  Antoine Hérault <antoine.herault@knplabs.com>
  8.  */
  9. class Pdf extends AbstractGenerator
  10. {
  11.     /**
  12.      * @var array
  13.      */
  14.     protected $optionsWithContentCheck = [];
  15.     /**
  16.      * {@inheritdoc}
  17.      */
  18.     public function __construct($binary null, array $options = [], array $env null)
  19.     {
  20.         $this->setDefaultExtension('pdf');
  21.         $this->setOptionsWithContentCheck();
  22.         parent::__construct($binary$options$env);
  23.     }
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     public function generate($input$output, array $options = [], $overwrite false)
  28.     {
  29.         $options $this->handleOptions($this->mergeOptions($options));
  30.         parent::generate($input$output$options$overwrite);
  31.     }
  32.     /**
  33.      * Handle options to transform HTML strings into temporary files containing HTML.
  34.      *
  35.      * @param array $options
  36.      *
  37.      * @return array $options Transformed options
  38.      */
  39.     protected function handleOptions(array $options = [])
  40.     {
  41.         foreach ($options as $option => $value) {
  42.             if (null === $value) {
  43.                 unset($options[$option]);
  44.                 continue;
  45.             }
  46.             if (!empty($value) && \array_key_exists($option$this->optionsWithContentCheck)) {
  47.                 $saveToTempFile = !$this->isFile($value) && !$this->isOptionUrl($value);
  48.                 $fetchUrlContent $option === 'xsl-style-sheet' && $this->isOptionUrl($value);
  49.                 if ($saveToTempFile || $fetchUrlContent) {
  50.                     $fileContent $fetchUrlContent \file_get_contents($value) : $value;
  51.                     $options[$option] = $this->createTemporaryFile($fileContent$this->optionsWithContentCheck[$option]);
  52.                 }
  53.             }
  54.         }
  55.         return $options;
  56.     }
  57.     /**
  58.      * Convert option content or url to file if it is needed.
  59.      *
  60.      * @param mixed $option
  61.      *
  62.      * @return bool
  63.      */
  64.     protected function isOptionUrl($option)
  65.     {
  66.         return (bool) \filter_var($option\FILTER_VALIDATE_URL);
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     protected function configure()
  72.     {
  73.         $this->addOptions([
  74.             // Global options
  75.             'collate' => null,
  76.             'no-collate' => null,
  77.             'cookie-jar' => null,
  78.             'copies' => null,
  79.             'dpi' => null,
  80.             'extended-help' => null,
  81.             'grayscale' => null,
  82.             'help' => null,
  83.             'htmldoc' => null,
  84.             'ignore-load-errors' => null// old v0.9
  85.             'image-dpi' => null,
  86.             'image-quality' => null,
  87.             'license' => null,
  88.             'log-level' => null,
  89.             'lowquality' => true,
  90.             'manpage' => null,
  91.             'margin-bottom' => null,
  92.             'margin-left' => null,
  93.             'margin-right' => null,
  94.             'margin-top' => null,
  95.             'orientation' => null,
  96.             'page-height' => null,
  97.             'page-size' => null,
  98.             'page-width' => null,
  99.             'no-pdf-compression' => null,
  100.             'quiet' => null,
  101.             'read-args-from-stdin' => null,
  102.             'readme' => null,
  103.             'title' => null,
  104.             'use-xserver' => null,
  105.             'version' => null,
  106.             // Outline options
  107.             'dump-default-toc-xsl' => null,
  108.             'dump-outline' => null,
  109.             'outline' => null,
  110.             'no-outline' => null,
  111.             'outline-depth' => null,
  112.             'output-format' => null,
  113.             // Page options
  114.             'allow' => null,
  115.             'background' => null,
  116.             'no-background' => null,
  117.             'bypass-proxy-for' => null,
  118.             'cache-dir' => null,
  119.             'checkbox-checked-svg' => null,
  120.             'checkbox-svg' => null,
  121.             'cookie' => null,
  122.             'custom-header' => null,
  123.             'custom-header-propagation' => null,
  124.             'no-custom-header-propagation' => null,
  125.             'debug-javascript' => null,
  126.             'no-debug-javascript' => null,
  127.             'default-header' => null,
  128.             'encoding' => null,
  129.             'disable-external-links' => null,
  130.             'enable-external-links' => null,
  131.             'disable-forms' => null,
  132.             'enable-forms' => null,
  133.             'images' => null,
  134.             'no-images' => null,
  135.             'disable-internal-links' => null,
  136.             'enable-internal-links' => null,
  137.             'disable-javascript' => null,
  138.             'enable-javascript' => null,
  139.             'javascript-delay' => null,
  140.             'keep-relative-links' => null,
  141.             'load-error-handling' => null,
  142.             'load-media-error-handling' => null,
  143.             'disable-local-file-access' => null,
  144.             'enable-local-file-access' => null,
  145.             'minimum-font-size' => null,
  146.             'exclude-from-outline' => null,
  147.             'include-in-outline' => null,
  148.             'page-offset' => null,
  149.             'password' => null,
  150.             'disable-plugins' => null,
  151.             'enable-plugins' => null,
  152.             'post' => null,
  153.             'post-file' => null,
  154.             'print-media-type' => null,
  155.             'no-print-media-type' => null,
  156.             'proxy' => null,
  157.             'proxy-hostname-lookup' => null,
  158.             'radiobutton-checked-svg' => null,
  159.             'radiobutton-svg' => null,
  160.             'redirect-delay' => null// old v0.9
  161.             'resolve-relative-links' => null,
  162.             'run-script' => null,
  163.             'disable-smart-shrinking' => null,
  164.             'enable-smart-shrinking' => null,
  165.             'ssl-crt-path' => null,
  166.             'ssl-key-password' => null,
  167.             'ssl-key-path' => null,
  168.             'stop-slow-scripts' => null,
  169.             'no-stop-slow-scripts' => null,
  170.             'disable-toc-back-links' => null,
  171.             'enable-toc-back-links' => null,
  172.             'user-style-sheet' => null,
  173.             'username' => null,
  174.             'viewport-size' => null,
  175.             'window-status' => null,
  176.             'zoom' => null,
  177.             // Headers and footer options
  178.             'footer-center' => null,
  179.             'footer-font-name' => null,
  180.             'footer-font-size' => null,
  181.             'footer-html' => null,
  182.             'footer-left' => null,
  183.             'footer-line' => null,
  184.             'no-footer-line' => null,
  185.             'footer-right' => null,
  186.             'footer-spacing' => null,
  187.             'header-center' => null,
  188.             'header-font-name' => null,
  189.             'header-font-size' => null,
  190.             'header-html' => null,
  191.             'header-left' => null,
  192.             'header-line' => null,
  193.             'no-header-line' => null,
  194.             'header-right' => null,
  195.             'header-spacing' => null,
  196.             'replace' => null,
  197.             // Cover object
  198.             'cover' => null,
  199.             // TOC object
  200.             'toc' => null,
  201.             // TOC options
  202.             'disable-dotted-lines' => null,
  203.             'toc-depth' => null// old v0.9
  204.             'toc-font-name' => null// old v0.9
  205.             'toc-l1-font-size' => null// old v0.9
  206.             'toc-header-text' => null,
  207.             'toc-header-font-name' => null// old v0.9
  208.             'toc-header-font-size' => null// old v0.9
  209.             'toc-level-indentation' => null,
  210.             'disable-toc-links' => null,
  211.             'toc-text-size-shrink' => null,
  212.             'xsl-style-sheet' => null,
  213.         ]);
  214.     }
  215.     /**
  216.      * Array with options which require to store the content of the option before passing it to wkhtmltopdf.
  217.      *
  218.      * @return $this
  219.      */
  220.     protected function setOptionsWithContentCheck()
  221.     {
  222.         $this->optionsWithContentCheck = [
  223.             'header-html' => 'html',
  224.             'footer-html' => 'html',
  225.             'cover' => 'html',
  226.             'xsl-style-sheet' => 'xsl',
  227.         ];
  228.         return $this;
  229.     }
  230. }