1

I use Dompdf in my symfony 4 project to generate pdf. For rendering the pdf, I make a render on a file twig. And in this twig file, I need to display images that I previously uploaded through VichUploaderBundle.

So, in my controller, I enabled this option :

    $pdfOptions->setIsRemoteEnabled(true);

And in my twig, I checked the absolute path of my image :

   {{ dump(absolute_url(vich_uploader_asset(parametresAdmin, 'logoFile'))) }}

And I've this :

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

This link makes me feel good about my image.

Now if I try to insert it in my pdf like this:

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

And that I restart the url to generate the pdf, it will turn in a loop and stop saying that the waiting time is exceeded.

So how can I insert my link to generate my pdf?

My controller :

/**
     * 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->setIsRemoteEnabled(true);



        // Instantiate Dompdf with our options
        $dompdf = new Dompdf();
        $dompdf->setOptions($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,
        ]);
    }

My Twig :

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

0 Answers0