1

I am looking to create a PDF with DomPDF in my Symfony 4 project. I wish I could insert images from an entity. Images are managed with VichUploaderBundle.

Is there a way to display them?

For the moment I did this in my twig file

<html>
    <head>
        <meta charset="UTF-8">
        <title>Title of the PDF</title>
    </head>
    <body>
        <h4>{{ title }}</h4>
        <p>Lorem Ipsum</p>
        <div class="user-pic">
            {% if app.user and app.user.avatar and app.user.avatar.imageName %}
                <img class="img img-responsive img-rounded" src="{{ absolute_url(vich_uploader_asset(app.user.avatar, 'imageFile'))}}">

            {% else %}
                <img src={{"/images/avatar/default.jpg"}}>
            {% endif %}
        </div>
    </body>
</html>

But it doesn't work. The image is missing. There is only the "alt".

My vichUploader config :

avatar:
      uri_prefix: /images/avatar
      upload_destination: "%kernel.project_dir%/public/images/avatar"

      inject_on_load: false
      delete_on_update: true
      delete_on_remove: true

In my controler :

 /**
     * Générer un PDF
     * @Route("/absence/pdf", name="absence_pdf")
     *
     * @return void
     */
    public function createPDF()
    {
        // Configure Dompdf according to your needs
        $pdfOptions = new Options();
        $pdfOptions->set('defaultFont', 'Arial')
        ->set('isRemoteEnabled', true);

        // Instantiate Dompdf with our options
        $dompdf = new Dompdf($pdfOptions);

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

        // 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
        ]);
    }

I've this error :

Error: Maximum execution time of 120 seconds exceeded

eronn
  • 1,690
  • 3
  • 21
  • 53
  • What do you mean by *it doesn't work*? Add your VichUploader configuration and the controller-part that actually renders the pdf/image to the question please. What happens if you render this template to pdf? Is the image in the pdf: a) missing b) blank, c) showing `default.jpg`, d) ... ? We can't guess the current behavior of this code snippet. – Nicolai Fröhlich Oct 06 '19 at 08:37
  • On the pdf, I only have the "alt" part of the image that appears. Otherwise, the picture is missing. I will update my first post – eronn Oct 06 '19 at 08:57
  • it's been a while since i used dompdf. the issue is that dompdf doesn't know about your local webserver in the first place and therefore can't render the image from the - currently relative - path. Solution below. – Nicolai Fröhlich Oct 06 '19 at 09:28

1 Answers1

1

The DomPDF renderer doesn't know the base URL of the images because you're just passing the HTML string to it.

In order to render images correctly with DomPDF you need to use an absolute path and enable remote sources as follows:

In your template use the absolute_url twig function:

absolute_url(vich_uploader_asset(app.user.avatar, 'imageFile'))

In your controller allow DomPDF to render "remote" images:

 $pdfOptions->set('isRemoteEnabled', true);
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • Thanks for your response ! I updated my code, but when I launch the page, I've an "Error: Maximum execution time of 120 seconds exceeded" error. However, in my twig, I have now "" (I updated my first post with the new code) – eronn Oct 06 '19 at 11:22