Commit d5f6bbf7 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Bascule vers chromium pour l'ancienne méthode pour garantir la récompatibilité...

Bascule vers chromium pour l'ancienne méthode pour garantir la récompatibilité (mais à supprimer ensuite)
parent badb3416
Loading
Loading
Loading
Loading
Loading
+3 −1
Changes for module/Oscar/src/Oscar/Formatter/File/HtmlToPdfChromiumFormatter.php: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ class HtmlToPdfChromiumFormatter implements IHtmlToPdfFormatter
                'XDG_CACHE_HOME=' . $cachePath,
                $this->chromiumBinary,
                '--headless',
                '--no-sandbox',
                '--disable-gpu',
                '--disable-dev-shm-usage',
                '--no-pdf-header-footer',
@@ -83,6 +82,9 @@ class HtmlToPdfChromiumFormatter implements IHtmlToPdfFormatter
                '--print-to-pdf=' . $pdfPath,
                'file://' . $htmlPath,
            ];
            if (filter_var(getenv('OSCAR_CHROMIUM_NO_SANDBOX') ?: '0', FILTER_VALIDATE_BOOLEAN)) {
                array_splice($command, 5, 0, ['--no-sandbox']);
            }
            $descriptors = [
                0 => ['pipe', 'r'],
                1 => ['pipe', 'w'],
+13 −50
Changes for module/Oscar/src/Oscar/Formatter/File/HtmlToPdfWkhtmltopdfFormatter.php: 13 added lines, 50 removed lines.
Original line number Diff line number Diff line
<?php


namespace Oscar\Formatter\File;


class HtmlToPdfWkhtmltopdfFormatter implements IHtmlToPdfFormatter
{
    private string $orientation = 'portrait';
    private string $fontPath;

/**
     * HtmlToPdfWkhtmltopdfFormatter constructor.
     * @param string $fontPath
 * Backward-compatible alias for installations still using the old formatter
 * class name in their local configuration.
 *
 * @deprecated Use HtmlToPdfChromiumFormatter instead.
 */
    public function __construct(string $fontPath = "")
class HtmlToPdfWkhtmltopdfFormatter implements IHtmlToPdfFormatter
{
        $this->fontPath = $fontPath;
    }
    private HtmlToPdfChromiumFormatter $formatter;

    /**
     * @param string $html
     * @param string|null $baseFilename
     * @param bool $download
     * @return string|null
     */
    public function convert(string $html, string $baseFilename = null, bool $download = true): ?string
    public function __construct(string $fontPath = '')
    {
        if ($baseFilename == null) {
            $baseFilename = "document";
        }

        $html_tmp = '/tmp' . DIRECTORY_SEPARATOR . uniqid('html_tmp_') . '.html';
        $pdf_tmp = '/tmp' . DIRECTORY_SEPARATOR . uniqid('pdf_tmp_') . '.pdf';
        file_put_contents($html_tmp, $html);
        $cmd = sprintf(
            'export QT_QPA_FONTDIR=%s && export QT_QPA_PLATFORM=offscreen && wkhtmltopdf -O %s %s %s',
            $this->fontPath,
            $this->orientation,
            $html_tmp,
            $pdf_tmp
        );
        $result = shell_exec($cmd);
        if( $download ) {
            header("Content-type:application/pdf");
            header("Content-Disposition:attachment;filename=$baseFilename.pdf");
            readfile("$pdf_tmp");
        } else {
            $content = file_get_contents($pdf_tmp);
        $this->formatter = new HtmlToPdfChromiumFormatter();
    }

        @unlink($html_tmp);
        @unlink($pdf_tmp);

        if( $download ){
            die();
        } else {
            return $content;
        }
    public function convert(string $html, string $baseFilename, bool $download = true): ?string
    {
        return $this->formatter->convert($html, $baseFilename, $download);
    }

    public function setOrientation($orientation): void
    public function setOrientation(string $orientation): void
    {
        $this->orientation = $orientation;
        $this->formatter->setOrientation($orientation);
    }
}
+11 −28
Changes for module/Oscar/src/Oscar/Formatter/Output/OutputWkhtmltopdfStrategy.php: 11 added lines, 28 removed lines.
Original line number Diff line number Diff line
@@ -3,11 +3,10 @@
namespace Oscar\Formatter\Output;

use Oscar\Exception\OscarException;
use Oscar\Formatter\File\HtmlToPdfChromiumFormatter;

class OutputWkhtmltopdfStrategy
{
    private string $fontPath;

    const ORIENTATION_PORTAIT = 'portrait';
    const ORIENTATION_LANDSCAPE = 'landscape';

@@ -17,7 +16,6 @@ class OutputWkhtmltopdfStrategy
     */
    public function __construct($fontPath = "")
    {
        $this->fontPath = $fontPath;
    }

    /**
@@ -33,32 +31,17 @@ class OutputWkhtmltopdfStrategy
            $filename = "document";
        }

        $html_tmp = '/tmp' . DIRECTORY_SEPARATOR . uniqid('htmltmp_') . '.html';
        $pdf_tmp = '/tmp' . DIRECTORY_SEPARATOR . uniqid('pdftmp_') . '.pdf';

        if (file_put_contents($html_tmp, $html)) {
            $cmd = sprintf(
                'export QT_QPA_FONTDIR=%s && export QT_QPA_PLATFORM=offscreen && wkhtmltopdf -O %s %s %s',
                $this->fontPath,
                $orientation,
                $html_tmp,
                $pdf_tmp
            );
            $done = false;
            if (shell_exec($cmd) !== false) {
                header("Content-type:application/pdf");
                header("Content-Disposition:attachment;filename=$filename");
                readfile("$pdf_tmp");
                $done = true;
            }
            unlink($html_tmp);
            unlink($pdf_tmp);
            if (!$done) {
                throw new OscarException("Impossible de générer le fichier PDF");
            }
        } else {
            throw new OscarException("Impossible de créer le fichier temporaire '$html_tmp'");
        $formatter = new HtmlToPdfChromiumFormatter();
        $formatter->setOrientation($orientation);
        $content = $formatter->convert($html, $filename, false);
        if ($content === null) {
            throw new OscarException('Impossible de générer le fichier PDF avec Chromium');
        }

        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename="' . str_replace(["\r", "\n", '"'], '', $filename) . '"');
        header('Content-Length: ' . strlen($content));
        echo $content;
        die();
    }
}