src/Services/PdfService.php line 91

Open in your IDE?
  1. <?php 
  2. namespace EADPlataforma\Services;
  3. use Psr\Container\ContainerInterface;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  6. use \setasign\Fpdi\Fpdi;
  7. use \setasign\FpdiProtection\FpdiProtection;
  8. use EADPlataforma\Services\ConfigurationService;
  9. use EADPlataforma\Services\FileService;
  10. use EADPlataforma\Util\StringUtil;
  11. use EADPlataforma\Util\EADPdf;
  12. use EADPlataforma\Enum\ConfigurationEnum;
  13. use Symfony\Component\Filesystem\Filesystem;
  14. use EADPlataforma\Services\ConverterPDF\Converter\GhostscriptConverterCommand;
  15. use EADPlataforma\Services\ConverterPDF\Converter\GhostscriptConverter;
  16. use EADPlataforma\Services\ConverterPDF\Guesser\RegexGuesser;
  17. class PdfService
  18. {
  19.     /**
  20.      * @var string
  21.      */
  22.     private $title;
  23.     /**
  24.      * @var string
  25.      */
  26.     protected $fileName;
  27.     /**
  28.      * @var string
  29.      */
  30.     protected $templateBody;
  31.     /**
  32.      * @var array
  33.      */
  34.     protected $data;
  35.     /**
  36.      * @var Symfony\Component\DependencyInjection\ContainerInterface
  37.      *
  38.      */
  39.     protected $container;
  40.     /**
  41.      * @var ConfigurationService
  42.      */
  43.     protected $configuration;
  44.     /**
  45.      * @var FileService
  46.      */
  47.     protected $fileService;
  48.     /**
  49.      * @var StringUtil
  50.      */
  51.     protected $stringUtil;
  52.     /**
  53.      * @var \Client
  54.      */
  55.     protected $client;
  56.     /**
  57.      * @var \Twig
  58.      */
  59.     protected $twig;
  60.     /**
  61.      * @var boolean
  62.      */
  63.     private $addPageNumber false;
  64.     /**
  65.      * @var boolean
  66.      */
  67.     private $addFooter false;
  68.     /**
  69.      * Constructor
  70.      *
  71.      * @param ContainerInterface $container
  72.      * @param ConfigurationService $configurationService
  73.      * @param StringUtil $stringUtil
  74.      */
  75.     public function __construct(ContainerInterface $container
  76.                                 ConfigurationService $configurationService
  77.                                 FileService $fileServiceStringUtil $stringUtil)
  78.     {
  79.         $this->container $container;
  80.         $this->configuration $configurationService;
  81.         $this->fileService $fileService;
  82.         $this->stringUtil $stringUtil;
  83.         $this->twig $this->container->get('twig');
  84.         $this->client $this->configuration->getClient();
  85.     }
  86.     public function getTitle(): ?string
  87.     {
  88.         return $this->title;
  89.     }
  90.     public function setTitle(?string $title): self
  91.     {
  92.         $this->title $title;
  93.         return $this;
  94.     }
  95.     public function setFileName(?string $fileName): self
  96.     {
  97.         $this->fileName $fileName;
  98.         return $this;
  99.     }
  100.     public function getFileName(): ?string
  101.     {
  102.         return $this->fileName;
  103.     }
  104.     public function setTemplateBody(?string $templateBody): self
  105.     {
  106.         $this->templateBody $templateBody;
  107.         return $this;
  108.     }
  109.     public function getTemplateBody(): ?string
  110.     {
  111.         return $this->templateBody;
  112.     }
  113.     public function setData(?array $data): self
  114.     {
  115.         $this->data $data;
  116.         return $this;
  117.     }
  118.     public function getData(): ?array
  119.     {
  120.         return $this->data;
  121.     }
  122.     public function addPageNumber(): self
  123.     {
  124.         $this->addPageNumber true;
  125.         return $this;
  126.     }
  127.     public function addFooter(): self
  128.     {
  129.         $this->addFooter true;
  130.         return $this;
  131.     }
  132.     public function showWithDRM(string $filePath, ?array $options = [])
  133.     {
  134.         $options = (object)$options;
  135.         if(empty($filePath) || empty($options->fileName)){
  136.             return;
  137.         }
  138.         $pdf = new Fpdi();
  139.         if(!empty($options->password)){
  140.             $pdf = new FpdiProtection();
  141.             $pdf->setProtection(
  142.                 FpdiProtection::PERM_PRINT,
  143.                 $options->password,
  144.                 $options->password
  145.             );
  146.         }
  147.         $file file_get_contents($filePath);
  148.         $tmpFileName tempnam("/tmp"$options->fileName);
  149.         $handle fopen($tmpFileName"w");
  150.         fwrite($handle$file);
  151.         fclose($handle);
  152.         /*
  153.             $command = new GhostscriptConverterCommand();
  154.             $filesystem = new Filesystem();
  155.             $converter = new GhostscriptConverter($command, $filesystem);
  156.             $a = $converter->convert($tmpFileName, '1.4');
  157.             dd($a, $filesystem);
  158.         */
  159.         $pdf->SetAutoPageBreak(false);
  160.         $pdf->SetMargins(000);
  161.         $pdf->SetTitle($options->fileName);
  162.         if(!empty($options->author)){
  163.             $pdf->SetCreator($options->author);
  164.             $pdf->SetAuthor($options->author);
  165.         }
  166.         $font = (!empty($options->font) ? $options->font "Arial");
  167.         $fontSize = (!empty($options->fontSize) ? $options->fontSize 10);
  168.         $pdf->SetFont($font'B'$fontSize);
  169.         $pdf->SetTextColor(000);
  170.         if(!empty($options->fontColor)){
  171.             $color $this->stringUtil->colorHexToRgb($options->fontColor);
  172.             $pdf->SetTextColor($color["r"], $color["g"], $color["b"]);
  173.         }
  174.         $pagesCount $pdf->setSourceFile($tmpFileName);
  175.         $tplIdx $pdf->importPage(1);
  176.         $size $pdf->getTemplateSize($tplIdx);
  177.         $height = (!empty($size["height"]) ? $size["height"] : 297);
  178.         $width = (!empty($size["width"]) ? $size["width"] : 210);
  179.         $format = (!empty($size["orientation"]) ? $size["orientation"] : "P");
  180.         for ($i 1$i <= $pagesCount$i ++) {
  181.             
  182.             $pdf->addPage($format);
  183.             $tplIdx $pdf->importPage($i);
  184.             $pdf->useTemplate($tplIdxnullnull$size['width'], $size['height'], true);
  185.             
  186.             if(!empty($options->watermark)){
  187.                 $xCenter = ($width $pdf->GetStringWidth($options->watermark)) / 2;
  188.                 $half $height 2;
  189.                 $pdf->Text($xCenter 60$half 75$options->watermark);
  190.                 $pdf->Text($xCenter$half$options->watermark);
  191.                 $pdf->Text($xCenter 60$half 75$options->watermark);
  192.             }
  193.             if(!empty($options->header)){
  194.                 $pdf->Cell(
  195.                     0,
  196.                     10,
  197.                     $options->header,
  198.                     (!empty($options->headerBorder) ? $options->headerBorder 0), 
  199.                     0,
  200.                     (!empty($options->headerPosition) ? $options->headerPosition 'L')
  201.                 );
  202.             }
  203.             if(!empty($options->footer)){
  204.                 $pdf->SetY(-10);
  205.                 $pdf->Cell(
  206.                     0,
  207.                     10,
  208.                     $options->footer,
  209.                     (!empty($options->footerBorder) ? $options->footerBorder 0), 
  210.                     0,
  211.                     (!empty($options->footerPosition) ? $options->footerPosition 'L')
  212.                 );
  213.             }
  214.         }
  215.         if(!empty($options->download)){
  216.             $pdf->Output("D"$options->fileName);
  217.             exit;
  218.         }
  219.         $pdf->Output("I"$options->fileName);
  220.         exit;
  221.     }
  222.     protected function preparePdfContent(): string
  223.     {
  224.         $logo $this->fileService->getFilePathComplete(
  225.             $this->configuration->get("logo"), 
  226.             ConfigurationEnum::PATH_OTHERS
  227.             true
  228.             false
  229.             null
  230.             true
  231.         );
  232.         $clientData = [
  233.             "clientDomain" => $this->configuration->getActiveDomain(),
  234.             "clientBrand" => $this->client->getBrand(),
  235.             "clientLogo" => "https:{$logo}",
  236.             "clientPrimaryColor" => $this->configuration->get("primary_color"),
  237.             "clientSecondColor" => $this->configuration->get("second_color"),
  238.             "today" => date("d/m/Y"),
  239.             "year" => date("Y"),
  240.         ];
  241.         $languageArray = [];
  242.         $data array_merge($clientData$languageArray$this->getData());
  243.         $templateText $this->twig->render("pdf/{$this->getTemplateBody()}.html.twig"$data);
  244.         
  245.         $template $this->twig->createTemplate($templateText);
  246.         $templateText $template->render($data);
  247.         return $templateText;
  248.     }
  249.     public function generate(?bool $isCertificate falsebool $download false)
  250.     {
  251.         if(empty($this->getFileName())){
  252.             return;
  253.         }
  254.         $content $this->preparePdfContent();
  255.         $eadPdf = new EADPdf();
  256.         $eadPdf->setTitle($this->getTitle());
  257.         if($this->addPageNumber){
  258.             $eadPdf->addPageNumber();
  259.         }
  260.         if($this->addFooter){
  261.             $eadPdf->addFooter();
  262.         }
  263.         $eadPdf->setBrand($this->client->getBrand());
  264.         $eadPdf->setHtml($content);
  265.         $eadPdf->setFileName($this->getFileName());
  266.         $eadPdf->setPaper($isCertificate);
  267.         
  268.         if($download){
  269.             return $eadPdf->download();    
  270.         }
  271.         
  272.         return $eadPdf->view();
  273.     }
  274. }