Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/nucleos/dompdf-bundle/src/Wrapper/DompdfWrapper.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) Christian Gripp <mail@core23.de>
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Nucleos\DompdfBundle\Wrapper;
  10. use Nucleos\DompdfBundle\DompdfEvents;
  11. use Nucleos\DompdfBundle\Event\OutputEvent;
  12. use Nucleos\DompdfBundle\Event\StreamEvent;
  13. use Nucleos\DompdfBundle\Exception\PdfException;
  14. use Nucleos\DompdfBundle\Factory\DompdfFactoryInterface;
  15. use Symfony\Component\HttpFoundation\StreamedResponse;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. final class DompdfWrapper implements DompdfWrapperInterface
  18. {
  19.     /**
  20.      * @var DompdfFactoryInterface
  21.      */
  22.     private $dompdfFactory;
  23.     /**
  24.      * @var EventDispatcherInterface|null
  25.      */
  26.     private $eventDispatcher;
  27.     public function __construct(DompdfFactoryInterface $dompdfFactoryEventDispatcherInterface $eventDispatcher null)
  28.     {
  29.         $this->dompdfFactory   $dompdfFactory;
  30.         $this->eventDispatcher $eventDispatcher;
  31.     }
  32.     public function streamHtml(string $htmlstring $filename, array $options = []): void
  33.     {
  34.         $pdf $this->dompdfFactory->create($options);
  35.         $pdf->loadHtml($html);
  36.         $pdf->render();
  37.         if ($this->eventDispatcher instanceof EventDispatcherInterface) {
  38.             $event = new StreamEvent($pdf$filename$html);
  39.             $this->eventDispatcher->dispatch($eventDompdfEvents::STREAM);
  40.         }
  41.         $pdf->stream($filename$options);
  42.     }
  43.     /**
  44.      * @param array<string, mixed> $options
  45.      */
  46.     public function getStreamResponse(string $htmlstring $filename, array $options = []): StreamedResponse
  47.     {
  48.         $response = new StreamedResponse();
  49.         $response->setCallback(function () use ($html$filename$options): void {
  50.             $this->streamHtml($html$filename$options);
  51.         });
  52.         return $response;
  53.     }
  54.     public function getPdf(string $html, array $options = []): string
  55.     {
  56.         $pdf $this->dompdfFactory->create($options);
  57.         $pdf->loadHtml($html);
  58.         $pdf->render();
  59.         if ($this->eventDispatcher instanceof EventDispatcherInterface) {
  60.             $event = new OutputEvent($pdf$html);
  61.             $this->eventDispatcher->dispatch($eventDompdfEvents::OUTPUT);
  62.         }
  63.         $out $pdf->output();
  64.         if (null === $out) {
  65.             throw new PdfException('Error creating PDF document');
  66.         }
  67.         return $out;
  68.     }
  69. }