src/Util/EADPdf.php line 209

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Util;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  5. use function Symfony\Component\String\u;
  6. use Dompdf\Dompdf;
  7. use Dompdf\Options;
  8. class EADPdf {
  9.     /**
  10.      * @var string
  11.      */
  12.     private $title;
  13.     /**
  14.      * @var string
  15.      */
  16.     private $brand;
  17.     /**
  18.      * @var string
  19.      */
  20.     private $html;
  21.     /**
  22.      * @var string
  23.      */
  24.     private $fileName;
  25.     /**
  26.      * @var string
  27.      */
  28.     private $path;
  29.     /**
  30.      * @var \Dompdf
  31.      */
  32.     private $dompdf;
  33.     /**
  34.      * @var boolean
  35.      */
  36.     private $isRender false;
  37.     /**
  38.      * @var boolean
  39.      */
  40.     private $addPageNumber false;
  41.     /**
  42.      * @var boolean
  43.      */
  44.     private $addFooter false;
  45.     
  46.     /**
  47.      * Constructor
  48.      */
  49.     public function __construct()
  50.     {
  51.         $option = new Options();
  52.         $option->set([
  53.             "isPhpEnabled" => true,
  54.             "isRemoteEnabled" => true,
  55.             "isHtml5ParserEnabled" => true
  56.         ]);
  57.         $this->dompdf = new Dompdf($option);
  58.     }
  59.     public function getTitle(): ?string
  60.     {
  61.         return $this->title;
  62.     }
  63.     public function setTitle(?string $title): self
  64.     {
  65.         $this->title $title;
  66.         return $this;
  67.     }
  68.     public function getBrand(): ?string
  69.     {
  70.         return $this->brand;
  71.     }
  72.     public function setBrand(?string $brand): self
  73.     {
  74.         $this->brand $brand;
  75.         return $this;
  76.     }
  77.     public function getHtml(): ?string
  78.     {
  79.         return $this->html;
  80.     }
  81.     public function setHtml(?string $html): self
  82.     {
  83.         $this->html $html;
  84.         return $this;
  85.     }
  86.     public function getFileName(): ?string
  87.     {
  88.         return $this->fileName;
  89.     }
  90.     public function setFileName(?string $fileName): self
  91.     {
  92.         $this->fileName $fileName;
  93.         return $this;
  94.     }
  95.     public function getPath(): ?string
  96.     {
  97.         return $this->path;
  98.     }
  99.     public function setPath(?string $path): self
  100.     {
  101.         $this->path $path;
  102.         return $this;
  103.     }
  104.     public function addPageNumber(): self
  105.     {
  106.         $this->addPageNumber true;
  107.         return $this;
  108.     }
  109.     public function addFooter(): self
  110.     {
  111.         $this->addFooter true;
  112.         return $this;
  113.     }
  114.     public function setPaper(?bool $isCertificate false): self
  115.     {
  116.         if($isCertificate){
  117.             $this->dompdf->setPaper([ 00850610 ]);
  118.         }else{
  119.             $this->dompdf->setPaper('A4''portrait');
  120.         }
  121.         return $this;
  122.     }
  123.     protected function render(): self
  124.     {
  125.         if(!$this->isRender){
  126.             $this->isRender true;
  127.             $this->dompdf->render();
  128.             $canvas $this->dompdf->getCanvas();
  129.             $font $this->dompdf->getFontMetrics()->get_font("helvetica""bold");
  130.             if($this->getTitle()){
  131.                 $canvas->page_text(22510$this->getTitle(), $font25, [ 0,0,]);
  132.             }
  133.             
  134.             if($this->addPageNumber){
  135.                 $canvas->page_text(270780"{PAGE_NUM}"$font10, [ 0,0,]);
  136.             }
  137.             if($this->getBrand() && $this->addFooter){
  138.                 $today date("Y");
  139.                 $canvas->page_text(225800"© {$today} - {$this->getBrand()}"$font8, [ 0,0,]);
  140.             }
  141.         }
  142.         return $this;
  143.     }
  144.     protected function loadHtml(): self
  145.     {
  146.         $this->dompdf->loadHtml($this->html);
  147.         return $this;
  148.     }
  149.     public function save(): self
  150.     {
  151.         $this->loadHtml()->render();
  152.         file_put_contents("{$this->path}{$this->fileName}.pdf"$this->dompdf->output());
  153.         return $this;
  154.     }
  155.     public function download()
  156.     {
  157.         $this->loadHtml()->render();
  158.         $response = new Response($this->dompdf->output());
  159.         $disposition $response->headers->makeDisposition(
  160.             ResponseHeaderBag::DISPOSITION_ATTACHMENT
  161.             u("{$this->fileName}.pdf")->ascii()
  162.         );
  163.         // Set the content disposition
  164.         $response->headers->set('Content-Disposition'$disposition);
  165.         $response->headers->set('Content-Type''application/pdf');
  166.         
  167.         return $response;
  168.     }
  169.     public function view()
  170.     {
  171.         $this->loadHtml()->render();
  172.         $response = new Response($this->dompdf->output());
  173.         $disposition $response->headers->makeDisposition(
  174.             ResponseHeaderBag::DISPOSITION_INLINE
  175.             u("{$this->fileName}.pdf")->ascii()
  176.         );
  177.         // Set the content disposition
  178.         $response->headers->set('Content-Disposition'$disposition);
  179.         $response->headers->set('Content-Type''application/pdf');
  180.         return $response;
  181.     }
  182. }