Search code examples
symfonydockerhtml2pdf

Html2Pdf + Symfony + Docker : Unable to get the size of the image


I have got a problem for generate a Pdf with HMTL2Pdf. I use :

  • Symfony framework
  • spipu/html2Pdf
  • Docker with a container for the app (8080) and a Minio container (9001) for files

I try to display an image on my pdf but i have the error : Unable to get the size of the image [http://127.0.0.1:9001/events/photos/37652647-203b-4196-8792-6b5da989eddc.jpeg].

My twig template

{% for page in pages %}
<page backtop="20mm" backright="10mm" backbottom="20mm" backleft="10mm">
    <img src="{{ page.background|storage_url }}">
</page
{% endfor %}

My controller

public function handle(): string
{
    $template = $this->renderer->render('pdf/index.html.twig', [
        'pages' => $this->pageManager->findAll(),
    ]);

    $this->pdfCreator->create('L', 'A4', 'fr', true, 'UTF-8', array(10, 15, 10, 15));
    return $this->pdfCreator->generatePdf($template, "test");
}

My Service pdfCreator

use Spipu\Html2Pdf\Html2Pdf as Html2Pdf;

final class PdfCreator implements PdfCreatorInterface
{
    /** @var Html2Pdf $pdf */
    private $pdf;

    public function create($orientation = null, $format = null, $lang = null, $unicode = null, $encoding = null, $margin = null): void
    {
        $this->pdf = new Html2Pdf($orientation, $format, $lang, $unicode, $encoding, $margin);
    }

    /**
     * @param $template
     * @param $name
     * @return string
     * @throws \Spipu\Html2Pdf\Exception\Html2PdfException
     */
    public function generatePdf($template, $name): string
    {
        $this->pdf->writeHTML($template);
        return $this->pdf->Output($name.'.pdf');
    }
}

Note :

  • If i delete <img>, my PDF is properly display
  • My image path is correct because i can display it with the same way on a different page of my application

Can you please help me to display my image ? I can't use absolute path because my image come from the other container Minio (9001) so i don't know how to do


Solution

  • My problem was to try to use 9001 URL to access to my image so i made this that solved my problem :

    • Get the content of my image
    • Create temporary file with that content
    • Display this temporary file in the pdf

    My template

    {% for page in pages %}
        <page backtop="20mm" backright="10mm" backbottom="20mm" backleft="10mm">
            <img src="/tmp/{{ page.background }}" style="width: 100px; height: 100px">
        </page>
    {% endfor %}
    

    My controller

    public function handle(FileUploader $fileUploader): string
    {
        $pages = $this->pageManager->findAll();
        foreach ($pages as $page) {
            $fileUploader->makeTempFile($page->getBackground());
        }
        $template = $this->renderer->render('pdf/index.html.twig', [
            'pages' => $pages,
        ]);
    
        $this->pdfCreator->create('L', 'A4', 'fr', true, 'UTF-8', array(10, 15, 10, 15));
        return $this->pdfCreator->generatePdf($template, "test");
    }
    

    My service FileUploader

    use Gaufrette\FilesystemInterface;
    use Ramsey\Uuid\Uuid;
    use Symfony\Component\HttpFoundation\File\File;
    
    final class FileUploader implements FileUploaderInterface
    {
        private $filesystem;
    
        public function __construct(FilesystemInterface $filesystem)
        {
            $this->filesystem = $filesystem;
        }
    
        public function upload(File $file): string
        {
            $filename = Uuid::uuid4()->toString().'.'.$file->guessExtension();
    
            $this->filesystem->write($filename, file_get_contents($file->getPathname()));
    
            return $filename;
        }
    
        public function get(string $fileName)
        {
            return $this->filesystem->read($fileName);
        }
    
        public function makeTempFile(string $fileName)
        {
            $content = $this->get($fileName);
            file_put_contents(sys_get_temp_dir().'/'.$fileName, $content);
        }
    }
    

    And i add GD to my DockerFile php-fpm

    FROM php:7.2-fpm
    
    RUN apt-get update
    
    RUN apt-get install -y libpq-dev git libicu-dev libpng-dev libjpeg62-turbo-dev \
        && docker-php-ext-configure intl \
        && docker-php-ext-install intl pdo pdo_pgsql pgsql gd
    
    WORKDIR /var/www/symfony
    

    And it works ! thanks for your help.