<?php
namespace EADPlataforma\Services;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use \setasign\Fpdi\Fpdi;
use \setasign\FpdiProtection\FpdiProtection;
use EADPlataforma\Services\ConfigurationService;
use EADPlataforma\Services\FileService;
use EADPlataforma\Util\StringUtil;
use EADPlataforma\Util\EADPdf;
use EADPlataforma\Enum\ConfigurationEnum;
use Symfony\Component\Filesystem\Filesystem;
use EADPlataforma\Services\ConverterPDF\Converter\GhostscriptConverterCommand;
use EADPlataforma\Services\ConverterPDF\Converter\GhostscriptConverter;
use EADPlataforma\Services\ConverterPDF\Guesser\RegexGuesser;
class PdfService
{
/**
* @var string
*/
private $title;
/**
* @var string
*/
protected $fileName;
/**
* @var string
*/
protected $templateBody;
/**
* @var array
*/
protected $data;
/**
* @var Symfony\Component\DependencyInjection\ContainerInterface
*
*/
protected $container;
/**
* @var ConfigurationService
*/
protected $configuration;
/**
* @var FileService
*/
protected $fileService;
/**
* @var StringUtil
*/
protected $stringUtil;
/**
* @var \Client
*/
protected $client;
/**
* @var \Twig
*/
protected $twig;
/**
* @var boolean
*/
private $addPageNumber = false;
/**
* @var boolean
*/
private $addFooter = false;
/**
* Constructor
*
* @param ContainerInterface $container
* @param ConfigurationService $configurationService
* @param StringUtil $stringUtil
*/
public function __construct(ContainerInterface $container,
ConfigurationService $configurationService,
FileService $fileService, StringUtil $stringUtil)
{
$this->container = $container;
$this->configuration = $configurationService;
$this->fileService = $fileService;
$this->stringUtil = $stringUtil;
$this->twig = $this->container->get('twig');
$this->client = $this->configuration->getClient();
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function setFileName(?string $fileName): self
{
$this->fileName = $fileName;
return $this;
}
public function getFileName(): ?string
{
return $this->fileName;
}
public function setTemplateBody(?string $templateBody): self
{
$this->templateBody = $templateBody;
return $this;
}
public function getTemplateBody(): ?string
{
return $this->templateBody;
}
public function setData(?array $data): self
{
$this->data = $data;
return $this;
}
public function getData(): ?array
{
return $this->data;
}
public function addPageNumber(): self
{
$this->addPageNumber = true;
return $this;
}
public function addFooter(): self
{
$this->addFooter = true;
return $this;
}
public function showWithDRM(string $filePath, ?array $options = [])
{
$options = (object)$options;
if(empty($filePath) || empty($options->fileName)){
return;
}
$pdf = new Fpdi();
if(!empty($options->password)){
$pdf = new FpdiProtection();
$pdf->setProtection(
FpdiProtection::PERM_PRINT,
$options->password,
$options->password
);
}
$file = file_get_contents($filePath);
$tmpFileName = tempnam("/tmp", $options->fileName);
$handle = fopen($tmpFileName, "w");
fwrite($handle, $file);
fclose($handle);
/*
$command = new GhostscriptConverterCommand();
$filesystem = new Filesystem();
$converter = new GhostscriptConverter($command, $filesystem);
$a = $converter->convert($tmpFileName, '1.4');
dd($a, $filesystem);
*/
$pdf->SetAutoPageBreak(false);
$pdf->SetMargins(0, 0, 0);
$pdf->SetTitle($options->fileName);
if(!empty($options->author)){
$pdf->SetCreator($options->author);
$pdf->SetAuthor($options->author);
}
$font = (!empty($options->font) ? $options->font : "Arial");
$fontSize = (!empty($options->fontSize) ? $options->fontSize : 10);
$pdf->SetFont($font, 'B', $fontSize);
$pdf->SetTextColor(0, 0, 0);
if(!empty($options->fontColor)){
$color = $this->stringUtil->colorHexToRgb($options->fontColor);
$pdf->SetTextColor($color["r"], $color["g"], $color["b"]);
}
$pagesCount = $pdf->setSourceFile($tmpFileName);
$tplIdx = $pdf->importPage(1);
$size = $pdf->getTemplateSize($tplIdx);
$height = (!empty($size["height"]) ? $size["height"] : 297);
$width = (!empty($size["width"]) ? $size["width"] : 210);
$format = (!empty($size["orientation"]) ? $size["orientation"] : "P");
for ($i = 1; $i <= $pagesCount; $i ++) {
$pdf->addPage($format);
$tplIdx = $pdf->importPage($i);
$pdf->useTemplate($tplIdx, null, null, $size['width'], $size['height'], true);
if(!empty($options->watermark)){
$xCenter = ($width - $pdf->GetStringWidth($options->watermark)) / 2;
$half = $height / 2;
$pdf->Text($xCenter + 60, $half - 75, $options->watermark);
$pdf->Text($xCenter, $half, $options->watermark);
$pdf->Text($xCenter - 60, $half + 75, $options->watermark);
}
if(!empty($options->header)){
$pdf->Cell(
0,
10,
$options->header,
(!empty($options->headerBorder) ? $options->headerBorder : 0),
0,
(!empty($options->headerPosition) ? $options->headerPosition : 'L')
);
}
if(!empty($options->footer)){
$pdf->SetY(-10);
$pdf->Cell(
0,
10,
$options->footer,
(!empty($options->footerBorder) ? $options->footerBorder : 0),
0,
(!empty($options->footerPosition) ? $options->footerPosition : 'L')
);
}
}
if(!empty($options->download)){
$pdf->Output("D", $options->fileName);
exit;
}
$pdf->Output("I", $options->fileName);
exit;
}
protected function preparePdfContent(): string
{
$logo = $this->fileService->getFilePathComplete(
$this->configuration->get("logo"),
ConfigurationEnum::PATH_OTHERS,
true,
false,
null,
true
);
$clientData = [
"clientDomain" => $this->configuration->getActiveDomain(),
"clientBrand" => $this->client->getBrand(),
"clientLogo" => "https:{$logo}",
"clientPrimaryColor" => $this->configuration->get("primary_color"),
"clientSecondColor" => $this->configuration->get("second_color"),
"today" => date("d/m/Y"),
"year" => date("Y"),
];
$languageArray = [];
$data = array_merge($clientData, $languageArray, $this->getData());
$templateText = $this->twig->render("pdf/{$this->getTemplateBody()}.html.twig", $data);
$template = $this->twig->createTemplate($templateText);
$templateText = $template->render($data);
return $templateText;
}
public function generate(?bool $isCertificate = false, bool $download = false)
{
if(empty($this->getFileName())){
return;
}
$content = $this->preparePdfContent();
$eadPdf = new EADPdf();
$eadPdf->setTitle($this->getTitle());
if($this->addPageNumber){
$eadPdf->addPageNumber();
}
if($this->addFooter){
$eadPdf->addFooter();
}
$eadPdf->setBrand($this->client->getBrand());
$eadPdf->setHtml($content);
$eadPdf->setFileName($this->getFileName());
$eadPdf->setPaper($isCertificate);
if($download){
return $eadPdf->download();
}
return $eadPdf->view();
}
}