laravel-dompdf: Images not shown in PDF

I’m not sure why but when I add in images, the pdf doesn’t display it. However, when I see the html on the page, it shows up normally.

This is the error that displays on the pdf

Image not readable or empty
/images/logo.png 

Not sure what it is, but I can see the images in the HTML that is used to generate the PDF.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 32 (1 by maintainers)

Most upvoted comments

This trick works for me! 😃

Convert your Image file to Base64 format with these lines of code, and then include the $imageData into src attribute of <img> tag instead of an image URL.

$avatarUrl = 'https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png';
$arrContextOptions=array(
                "ssl"=>array(
                    "verify_peer"=>false,
                    "verify_peer_name"=>false,
                ),
            );
$type = pathinfo($avatarUrl, PATHINFO_EXTENSION);
$avatarData = file_get_contents($avatarUrl, false, stream_context_create($arrContextOptions));
$avatarBase64Data = base64_encode($avatarData);
$imageData = 'data:image/' . $type . ';base64,' . $avatarBase64Data;

This might help someone

This should do the trick.

<?php $image_path = '/images/logo.pdf'; ?>
<img src="{{ public_path() . $image_path }}">

Can you use the full path? Like http://mydomain.com/images/logo.png? Use asset('images/logo.png') for example.

src=“http://www.example.com/assets/images/image.png

You just need to change it to,

src=“assets/images/image.png”

HI Guys, I am still having this problem. My setup is as follows. And I am using Laravel 5.4.36 The Image is not Showing up on the PDF. I tried using @hzung example but a bit simpler.

OS

 System Version: macOS 10.12.6 (16G1036)

PHP Version

PHP 7.1.8 (cli) (built: Aug 28 2017 11:09:12) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

Composer File

"laravel/framework": "5.4.*",
"barryvdh/laravel-dompdf": "^0.8.1"

Controller

public function downloadPDF ($id)
    {
        $user   = UserInfo::find($id);
        $pdf    = PDF::setOptions([
            'images' => true
        ])->loadView('customer.carFact', compact('user'))->setPaper('a4', 'portrait');
        return $pdf->download('carfact_sheet.pdf');
    }

HTML :: customer/carFact.blade.php

    <?php
        $path = public_path('/assets/images/company_Logo.jpg');
        $type = pathinfo($path, PATHINFO_EXTENSION);
        $data = file_get_contents($path);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    ?>
    <table class="table table-bordered" style="margin: 0px auto;">
        <tr>
            <td colspan="4" align="center">
                <img src=" {{ $base64 }}" alt=" " class="img-responsive">
            </td>
        </tr>
     ......
   </table>

The Image is not Showing up on the PDF.

Can’t get it to work on localhost:8000 with php artisan serve

Hi, I currently have a problem. Images was showing for 3 or 4 generated pdf but now no images are shown.

I am using the @AMABK code:

<?php $image_path = '/images/logo.jpg'; ?>
<img src="{{ public_path() . $image_path }}">

It was working as I said for 3 or 4 generated files but now the image is not showed. I save each html on a file and such files the image is showed. Now I am trying with decoded image but is unsuccessful.

I am using the version 0.4 for laravel 4.2

For png files I simply do:

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

for SVG files in this case this is an background image

<img class="backdrop" src="{{'data:image/svg+xml;base64,'.base64_encode(file_get_contents(public_path('images/backgroung.svg')))}}"/>

/etc/hosts file

This might help someone

This should do the trick.

<?php $image_path = '/images/logo.pdf'; ?>
<img src="{{ public_path() . $image_path }}">

I actually didn’t expect this to work, but it does! How’s this different from public_path($image_path)? 😕

Edit: Never mind. It seems that you need to specify your local path e.g. /var/www/path/to/file instead of http://app.local/path/to-file. Makes sense.

What do you mean with “hosts” file?

On Fri, Jul 31, 2015 at 2:30 PM, Dmitriy Galievskiy < notifications@github.com> wrote:

Found where is a problem! Just add the site to the hosts file, and all will works!

— Reply to this email directly or view it on GitHub https://github.com/barryvdh/laravel-dompdf/issues/57#issuecomment-126675601 .

Found where is a problem! Just add the site to the hosts file, and all will works!