laravel-dompdf: Images do not render inside generated PDF
This is just a Dompdf wrapper! I understand that this package is just a Laravel wrapper for https://github.com/dompdf/dompdf Any issues with PDF rendering, CSS that is not applied correctly, aligning/fonts/characters etc that are not directly related to this package, should be reported there. When having doubts, please try to reproduce the issue with just dompdf. If it’s also present there, do not open an issue here please.
Describe the bug After upgrading to Laravel 6 and subsequently a higher version of this library, images no longer load with any of my PDFs. Enabling debug mode give me the following error.
Permission denied on /homepages/45/d641872465/htdocs/development/mynewable/storage/app/user-files/graemem/expenses/personal/5/world-aids-day-rem.png. The file could not be found under the directory specified by Options::chroot.
To Reproduce Laravel DOMPDF version: 0.8.7 Laravel Framework version: 6.2
Add any image to a view from any folder within the application, then use the PDF facade to load a view
/**
* Generate a PDF for this deal
*
* @param Deal $deal
*
* @return void
*/
public function generatePDF(Deal $deal)
{
$this->authorize('view', $deal);
$pdf = PDF::setOptions(['isPhpEnabled' => true])->loadView('pdf.user.deal', compact('deal'));
return $pdf->stream("{$deal->name}.pdf");
}
In Blade
<img
class="newable-logo"
src="{{ public_path('assets/images/newable-logo-blue.png') }}"
alt="{{ public_path('assets/images/newable-logo-blue.png') }}"
>
Expected behavior The image should load on the generated PDF.
Screenshots If applicable, add screenshots to help explain your problem.
Additional context I am using the full path as specified in other issues of a similar nature.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 21
Ran into the same issue with images not being rendered. Modified version of @CamiloPerezOrtiz’s suggestion worked for my use case:
In
Controller
:In
example.blade.php
:Using that approach I was able to render the image in the PDF without issue, where previously I had seen “Image not found or unsupported type”.
@webesistemas try with this, transform the image in base 64 in the controller
$opciones_ssl=array( “ssl”=>array( “verify_peer”=>false, “verify_peer_name”=>false, ), ); $img_path = ‘images/’ . $user->images; $extencion = pathinfo($img_path, PATHINFO_EXTENSION); $data = file_get_contents($img_path, false, stream_context_create($opciones_ssl)); $img_base_64 = base64_encode($data); $path_img = ‘data:image/’ . $extencion . ‘;base64,’ . $img_base_64;
in the blade src=“{{ $path_img }}” width=“80”
in my case this solved the problem
Hello @igoptx that version are you using, in my case I am working with 0.8.5 version, try with this: transform the image in base 64
In the controller: $opciones_ssl=array( “ssl”=>array( “verify_peer”=>false, “verify_peer_name”=>false, ), ); $img_path = ‘images/’ . $user->images; $extencion = pathinfo($img_path, PATHINFO_EXTENSION); $data = file_get_contents($img_path, false, stream_context_create($opciones_ssl)); $img_base_64 = base64_encode($data); $path_img = ‘data:image/’ . $extencion . ‘;base64,’ . $img_base_64;
In the blade <img src=“src=”{{ $path_img }}“”>
in my case this solved the problem, or could you share what error shows laravel.
for me it is not working. Even when using full link image
Thanks, sir. I am experiencing this issue too. Solved.
Hi,
I am using this library, and also facing the images issue on windows server, When I am checking it on linux server or localhost server, it’s showing the images, but on live server It’s showing “Image not found or type unknown”. I have compared the php version and required extenions that are same on both server (Linux and Windows).
Html code
Php code
$pdf = PDF::setOptions([‘isHtml5ParserEnabled’ => true, ‘isRemoteEnabled’ => true ,‘chroot’ => public_path()])->setPaper(‘a4’, ‘portrait’)->loadView(‘admin.final-report’, $this->data); return $pdf->download($report_filename);
PHP version : 7.4.1 Laravel Version : Laravel Framework 7.29.3 barryvdh/laravel-dompdf : 0.8.7
Hello, I think it is an update problem, I am working on a laravel project in 5.8 update composer and the library was updated and it sent me the errors you mention, I tried with this <img src = ”{{url ($img)}}”> on my localhost this worked but the production server not <img src = ”{{asset ($img)}}”> <img src = ”{{public_path ($img)}}”> <img src = ”{{public_path (). $img}} ”>
The solution I found was to reduce the name of the image, I mean, it had a name with a hash of 100 characters when I reduced it to 30 and it reloaded me, but I think it is a detail of the library that I will continue testing.
Sorry my english is not very good.
Hi @ritesh1way maybe it’s site certificate problem, try this:
$pdf->setHttpContext(stream_context_create([ ‘ssl’ => [ “verify_peer”=>false, “verify_peer_name”=>false, ], ]));
Check this
https://github.com/barryvdh/laravel-dompdf/issues/567