0

I try for several days to display images in my PDF generated with domPDF on symfony 4.

In the pdf, I would like to insert an image from an entity, which I manage with VichUploader.

So I proceeded like this:

In my controller, I added :

// Configure Dompdf according to your needs
        $pdfOptions = new Options();
        $pdfOptions->set('isRemoteEnabled', true);

In my Twig, I display my image like this :

<img style="width:90px;" src="{{absolute_url(vich_uploader_asset(parametresAdmin, 'logoFile'))}}">

But when I start the action, there is a loop and I end up having the following error:

Error: Maximum execution time of 120 seconds exceeded

However, if I cancel adding the image, the PDF loads well (but without the image since I do not display it)

I checked on the url of the image was well an absolute way and that it was correct, doing this in my Twig:

{% set var = absolute_url(vich_uploader_asset(parametresAdmin, 'logoFile')) %}
            <pre>
            {{ dump(var) }}

And it is !

enter image description here

And the path is good, because I can open the file in a new tab.

I tried to display my image with a split in my twig :

{% set var = absolute_url(vich_uploader_asset(parametresAdmin, 'logoFile')) %}
<img style="width:90px;" src="{{var}}">

Or pass my url from my controller to my view in parameter :

Controller:

$html = $this->renderView('absence/pdf.html.twig', [
            'title' => "Welcome to our PDF Test",
            'parametresAdmin' => $parametresAdmin,
            'url' => "http://127.0.0.1:8000/admin/logo/avatar2.jpg",
        ]);

Twig :

<img style="width:90px;" src="{{url}}">

But same problem. Looping for 120 seconds and error :

Error: Maximum execution time of 120 seconds exceeded

So I really do not understand why he does not want to post it in the PDF and that it loops. The path is correct, and works on normal views.

Here is my whole code for an overview:

Controller:

/* Uses */
use Dompdf\Dompdf;
use Dompdf\Options;

/*Fonction*/
    /**
     * Générer un PDF
     * @Route("/absence/pdf", name="absence_pdf")
     *
     * @return void
     */
    public function createPDF(ParametresAdminRepository $repoParametres)
    {
        $parametresAdmin = $repoParametres->findAll();

        if (isset($parametresAdmin[0])) {
            $parametresAdmin = $parametresAdmin[0];
        } else {
            $parametresAdmin = new ParametresAdmin();
        }
        // Configure Dompdf according to your needs
        $pdfOptions = new Options();
        $pdfOptions->set('isRemoteEnabled', true);



        // Instantiate Dompdf with our options
        $dompdf = new Dompdf($pdfOptions);
        $dompdf->set_option('isHtml5ParserEnabled', true);

        // Retrieve the HTML generated in our twig file
        $html = $this->renderView('absence/pdf.html.twig', [
            'title' => "Welcome to our PDF Test",
            'parametresAdmin' => $parametresAdmin
        ]);

        // Load HTML to Dompdf
        $dompdf->loadHtml($html);

        // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
        $dompdf->setPaper('A4', 'portrait');

        // Render the HTML as PDF
        $dompdf->render();

        // Output the generated PDF to Browser (inline view)
        $dompdf->stream("mypdf.pdf", [
            "Attachment" => false,
        ]);
    }

And my Twig:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Title of the PDF</title>
    </head>
    <body>
        <h4>{{ title }}</h4>
        <p>Lorem Ipsum</p>
        {% if parametresAdmin.logoName %}
            <img style="width:90px;" src="{{absolute_url(vich_uploader_asset(parametresAdmin, 'logoFile'))}}">
        {% endif %}
    </body>
</html>

EDIT: When I use an absolute path directly from my files, like :

"C:\Users\user\Desktop\congesTest2\public\admin\logo\avatar2.jpg" It works.

But it seems that if the absolute path is :

http://........

In this case:

http://127.0.0.1:8000/admin/logo/avatar2.jpg

It doesn't work. But I need this path

Thanks for your help !

eronn
  • 1,690
  • 3
  • 21
  • 53

0 Answers0