Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images not shown in PDF #57

Closed
MaazAli opened this issue Aug 5, 2014 · 32 comments
Closed

Images not shown in PDF #57

MaazAli opened this issue Aug 5, 2014 · 32 comments
Labels

Comments

@MaazAli
Copy link

MaazAli commented Aug 5, 2014

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.

@barryvdh
Copy link
Owner

barryvdh commented Aug 5, 2014

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

@vadimwe
Copy link

vadimwe commented Mar 16, 2015

works fine in last version. In old version need to update dompdf package manually

@RokSiEu
Copy link

RokSiEu commented Jun 16, 2015

Was this problem ever solved? Because it seems that it wasn't.
When I do

dd($view->render());

everything is OK, image is displayed.

but when I do

$html = View::make('emails.upn.pdf-invoice', $data);
$pdfUPN = PDF::loadHTML($html)->stream();
$randomTime = time();
if (!is_dir("upn/")) {
     mkdir("upn/");
}
file_put_contents('./upn/'.$CaseID.'-'.$randomTime.'.pdf',$pdfUPN);
$pdf = './upn/'.$CaseID.'-'.$randomTime.'.pdf';

PDF doesn't contain image.
Here's HTML/CSS code that is used in emails.upn.pdf-invoice

 <html>
<head>
<meta http-equiv="Content-Type" content="charset=utf-8" />
<meta charset="UTF-8">
<title>PDF Upn</title>

<style type="text/css">
 * {
     font-family: "DejaVu Sans";
     font-size: 8px;
    }
.upn{background-image:url("https://sub.domain.com/upn.jpg"); background-repeat:no-repeat; width:700px; height:342px; position:absolute;}
</style>
</head>
<body>
   <div class="upn">test</div>
</body>

@xAockd
Copy link

xAockd commented Jul 31, 2015

@RokSiEu
The same problem.
For temporary fixing, I'm using the relative path to my assets "upn.jpg"
Even path what built by asset helper doesn't help me.

@bebnev
Copy link

bebnev commented Jul 31, 2015

Hi
@xAockd, @RokSiEu can you provide some more information about this problem, because I have just installed fresh laravel app (5.1.8) and laravel-dompdf (0.6.0) I used the code that @RokSiEu had provided, but i get pdf with the image (even when using asset function).
The code:

$html = View::make('emails.upn.pdf-invoice', []);
$pdfUPN = PDF::loadHTML($html)->stream();
$randomTime = time();
if (!is_dir("upn/")) {
  mkdir("upn/");
}
$CaseID = '123';
file_put_contents('./upn/'.$CaseID.'-'.$randomTime.'.pdf',$pdfUPN);
$pdf = './upn/'.$CaseID.'-'.$randomTime.'.pdf';

And the emails.upn.pdf-invoice:

<html>
<head>
    <meta http-equiv="Content-Type" content="charset=utf-8" />
    <meta charset="UTF-8">
    <title>PDF Upn</title>

    <style type="text/css">
        * {
            font-family: "DejaVu Sans";
            font-size: 8px;
        }
        .upn {
          background-image:url("{{ asset('images/test.jpg') }}");
          background-repeat:no-repeat;
          width:700px;
          height:342px;
          position:absolute;
       }
    </style>
</head>
<body>
<div class="upn">test</div>
</body>

The output pdf
output-pdf

@xAockd
Copy link

xAockd commented Jul 31, 2015

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

@RokSiEu
Copy link

RokSiEu commented Jul 31, 2015

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
#57 (comment)
.

@xAockd
Copy link

xAockd commented Jul 31, 2015

/etc/hosts file

@casank25
Copy link

casank25 commented Sep 9, 2015

@xAockd +1, lost 1 day on this until i saw this.

@ericgeurts
Copy link

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

@AMABK
Copy link

AMABK commented Jul 13, 2016

This might help someone

This should do the trick.

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

@neoacevedo
Copy link

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

@hzung
Copy link

hzung commented Apr 12, 2017

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;

@roshimon
Copy link

Thank you @hzung. Took lot of time at last found your solution & it just worked fine :)

@rdtandel
Copy link

rdtandel commented Aug 30, 2017

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

You just need to change it to,

src="assets/images/image.png"

@nicoHersant
Copy link

@AMABK thanks! it did the trick!

@acedesigns
Copy link

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.

@andresams
Copy link

@xAockd Thank you very much, your solution worked perfectly.

@acedesigns
Copy link

@andresams
Do you mind to share your source code snippet of what/how you did it? PLEASE !!

@DominikStyp
Copy link

I wonder why local links with port doesn't work:
http://mydoiman.test:8000/images/x.jpg - doesn't work in pdf (works in browser)
but every other image from google images for example works.

@PhillyWebGuy
Copy link

PhillyWebGuy commented Jun 19, 2018

@acedesigns

Change path:

$path = public_path('/images/company_Logo.jpg');

And I specified size of image:

<img src="{{ $base64 }}" style="width: 100px; height:100px;">

@SabrineMihni
Copy link

@rdtandel thank you so much, it did the trick 💯

@CristianGno
Copy link

@hzung I do not know if you will read this, but I thank you, you have taken me out of a big hurry.

@imadphp
Copy link

imadphp commented Jan 16, 2019

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.

@andreshg112
Copy link

I don't know why this is happening. It is working on shared hosting, it was working using Xampp on Zorin but It's not working using Docker on Mac.
imagen

@emma255
Copy link

emma255 commented Apr 24, 2020

Me too i struggled for the same error for 3 days. Also when i tried tricks mentioned above, none of them worked too. But when i changed the image file to .jpg file(extension) everything worked well.

<img src="{{ public_path('img/netphone.jpg') }}">

  • Laravel version: 5.8.38
  • PHP version: 7.3
  • arryvdh/laravel-dompdf version: 0.8.6

@stale
Copy link

stale bot commented Jul 28, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most issues are actually related to DompDF (eg rendering the PDF itself), so please raise an issue there: https://github.com/dompdf/dompdf
If you believe this is still an actual issue with this library, please reply to this issue.

@stale stale bot added the stale label Jul 28, 2020
@stale stale bot closed this as completed Aug 6, 2020
@mohsin82
Copy link

in my local, all images are working.

but in live site, some png images are working and some are not working.

i am using laravel dompdf

can some help me on this?

@maherelgamil
Copy link

You can fix it by adding full path of image not URL

public_path('images/logo.png ');

And it will be worked fine

@costatavares
Copy link

@andreshg112
In my case, I added data: image / png; base64
directly in the img tag

<img src = "data: image / png; base64, 333343344343 />

for example

@Akaaal
Copy link

Akaaal commented Jan 16, 2021

In my case, i'm using external css and spend time to figure out how to render background images. This works for me:

pdf.php :

$dompdf = new Dompdf();	
$content .= '<link type="text/css" media="all" href="https://mydomaine.com/path/to/pdf-css.php" rel="stylesheet" />';	
$dompdf->loadHtml($content);

pdf-css.php :

<?php
    header("Content-type: text/css; charset: UTF-8");
?>
<?php $image_path = 'https://mydomaine.com/path/to/images/test.jpg';  ?>
html, body { margin:20px;padding:0;width:100%;font-family:Helvetica;}
.testbkg {width:200px;height:200px;background-image:url(' <?php echo $image_path ?>');background-repeat: no-repeat;background-size: cover;background-position:center}

But SVG don't work...

@aurawindsurfing
Copy link

aurawindsurfing commented Apr 26, 2021

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')))}}"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests