← Back
Editing: PdfService.php
<?php namespace App\Service; use Dompdf\Dompdf; use Dompdf\Options; use HeadlessChromium\BrowserFactory; use HeadlessChromium\Exception\CommunicationException; use HeadlessChromium\Exception\NoResponseAvailable; use HeadlessChromium\Exception\OperationTimedOut; use Psr\Log\LoggerInterface; use Symfony\Component\Filesystem\Filesystem; use Twig\Environment; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; class PdfService { public const REQUEST_TIMEOUT = 10000; private ?int $startTime = null; public function __construct( private readonly Environment $twig, private readonly LoggerInterface $logger, private string $chromeBinary, private readonly bool $debugMode = false, ) { } private function initDebug(): void { if (true === $this->debugMode) { $this->startTime = intval(microtime(true) * 1000); $this->logger->notice('Starting debug for PdfService'); } } private function d(string $message): void { $this->logger->debug( sprintf('[PdfService] %s', $message), ['elapsed_time' => intval(microtime(true) * 1000) - $this->startTime] ); } /** * @throws CommunicationException * @throws OperationTimedOut * @throws NoResponseAvailable * @throws SyntaxError * @throws RuntimeError * @throws LoaderError * @throws \Exception */ public function export(string $html, ?string $title) { $this->initDebug(); $this->d('Init Browser Factory'); $browserFactory = new BrowserFactory($this->chromeBinary); $browserFactory->addOptions([ 'noSandbox' => true, 'customFlags' => [ '--privileged', '--cap-add=SYS_ADMIN', ], ]); $this->d('Starting browser'); $browser = $browserFactory->createBrowser(); try { $this->d('Create page'); $page = $browser->createPage(); $this->d('Fetch HTML from Twig'); $html = $this->twig->render('base.pdf.html.twig', ['body' => $html, 'title' => $title]); $this->d('Set HTML to created page'); $page->setHtml( $html, self::REQUEST_TIMEOUT ); $this->d('Generate PDF'); $pdf = $page->pdf( [ 'printBackground' => true, 'displayHeaderFooter' => true, // 'mediaType' => 'print', 'scale' => 0.7, ] ); $this->d('Generate base64 result'); $base64Result = $pdf->getBase64(self::REQUEST_TIMEOUT); } catch (\Exception $e) { $this->d('An error occured'); $browser->close(); throw $e; } $browser->close(); $this->d('End of process, return result'); return $base64Result; } public function generatePdf(string $html, string $pdfFilepath): string { $pdfOptions = new Options(); $pdfOptions->setDefaultFont('Arial'); $pdfOptions->setIsRemoteEnabled(true); $dompdf = new Dompdf($pdfOptions); // Load HTML to Dompdf $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation 'portrait' or 'portrait' $dompdf->setPaper('A4'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser (force download) $output = $dompdf->output(); $filesystem = new Filesystem(); if ($filesystem->exists($pdfFilepath)) { $filesystem->remove($pdfFilepath); } // Write file to the desired path file_put_contents($pdfFilepath, $output); return $pdfFilepath; } }
Save File
Cancel