3

I want to add an image to my pdf generated by snappy ,but nothing appear

In my twig :

I am persisting the name of the file in my data base, survey.event.image contain the name of the image

<div style="margin-top: 26px; width: 200px">

    <img   src="{{ asset('uploads/imagesFile/'~survey.event.image) }}" id="imgEvent">

</div>

I have enabled images =>true

return new Response(
    $this->get('knp_snappy.pdf')->getOutputFromHtml($html,array(
        'default-header'=>null,
        'encoding' => 'utf-8',
        'images' => true,
        'enable-javascript' => true,
        'margin-right'  => 7,
        'margin-left'  =>7,
        'javascript-delay' => 5000
    )),
    200,
    array(
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename="file1.pdf"'
    )
);
Julie
  • 563
  • 6
  • 10
  • 23

4 Answers4

4

Try to use absolute_url to get absolute assets url

<div style="margin-top: 26px; width: 200px">

<img   src="{{ absolute_url(asset('uploads/imagesFile/'~survey.event.image)) }}" id="imgEvent">

ryl
  • 333
  • 1
  • 4
  • 12
2

In case anyone is still for a solution. In my Laravel 8 i was able to load the image but while downloading the image it takes a while to load & then failed to download. I was using snappy for pdf generate & download.

Here's how i solve it.

From this

<img src="{{asset('assets/images/czm/bismillah.png')}}" width="10%" alt="">

To this. I just remove laravel default asset and convert the image into base64.

<img src="{{'data:image/png;base64,'.base64_encode(file_get_contents(public_path('assets/images/czm/bismillah.png')))}}" width="180"/>

You can find more here

Fahim Ahmed
  • 111
  • 7
0

Use absolute=true for your path to the image.

<img src="{{ asset('uploads/imagesFile/'~survey.event.image, , absolute=true) }}" id="imgEvent">
Keutelvocht
  • 670
  • 2
  • 10
  • 28
-1

Maybe the image path is wrong, try this :

<div style="width: 200px; height: auto;">
  <img src="{{ pathToWeb }}/uploads/imagesFile/image.jpg" alt="img">
</div>

And add this in your config.yml :

# Twig Configuration
twig:
  globals:
    pathToWeb: "%kernel.root_dir%/../web"
user7867717
  • 285
  • 3
  • 15