<?php
namespace EADPlataforma\Util;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use function Symfony\Component\String\u;
use Dompdf\Dompdf;
use Dompdf\Options;
class EADPdf {
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $brand;
/**
* @var string
*/
private $html;
/**
* @var string
*/
private $fileName;
/**
* @var string
*/
private $path;
/**
* @var \Dompdf
*/
private $dompdf;
/**
* @var boolean
*/
private $isRender = false;
/**
* @var boolean
*/
private $addPageNumber = false;
/**
* @var boolean
*/
private $addFooter = false;
/**
* Constructor
*/
public function __construct()
{
$option = new Options();
$option->set([
"isPhpEnabled" => true,
"isRemoteEnabled" => true,
"isHtml5ParserEnabled" => true
]);
$this->dompdf = new Dompdf($option);
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getBrand(): ?string
{
return $this->brand;
}
public function setBrand(?string $brand): self
{
$this->brand = $brand;
return $this;
}
public function getHtml(): ?string
{
return $this->html;
}
public function setHtml(?string $html): self
{
$this->html = $html;
return $this;
}
public function getFileName(): ?string
{
return $this->fileName;
}
public function setFileName(?string $fileName): self
{
$this->fileName = $fileName;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): self
{
$this->path = $path;
return $this;
}
public function addPageNumber(): self
{
$this->addPageNumber = true;
return $this;
}
public function addFooter(): self
{
$this->addFooter = true;
return $this;
}
public function setPaper(?bool $isCertificate = false): self
{
if($isCertificate){
$this->dompdf->setPaper([ 0, 0, 850, 610 ]);
}else{
$this->dompdf->setPaper('A4', 'portrait');
}
return $this;
}
protected function render(): self
{
if(!$this->isRender){
$this->isRender = true;
$this->dompdf->render();
$canvas = $this->dompdf->getCanvas();
$font = $this->dompdf->getFontMetrics()->get_font("helvetica", "bold");
if($this->getTitle()){
$canvas->page_text(225, 10, $this->getTitle(), $font, 25, [ 0,0,0 ]);
}
if($this->addPageNumber){
$canvas->page_text(270, 780, "{PAGE_NUM}", $font, 10, [ 0,0,0 ]);
}
if($this->getBrand() && $this->addFooter){
$today = date("Y");
$canvas->page_text(225, 800, "© {$today} - {$this->getBrand()}", $font, 8, [ 0,0,0 ]);
}
}
return $this;
}
protected function loadHtml(): self
{
$this->dompdf->loadHtml($this->html);
return $this;
}
public function save(): self
{
$this->loadHtml()->render();
file_put_contents("{$this->path}{$this->fileName}.pdf", $this->dompdf->output());
return $this;
}
public function download()
{
$this->loadHtml()->render();
$response = new Response($this->dompdf->output());
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
u("{$this->fileName}.pdf")->ascii()
);
// Set the content disposition
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'application/pdf');
return $response;
}
public function view()
{
$this->loadHtml()->render();
$response = new Response($this->dompdf->output());
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
u("{$this->fileName}.pdf")->ascii()
);
// Set the content disposition
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'application/pdf');
return $response;
}
}