Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mknparreira/60348056e45d02180b9d23ad1f9a3bda to your computer and use it in GitHub Desktop.
Save mknparreira/60348056e45d02180b9d23ad1f9a3bda to your computer and use it in GitHub Desktop.
PHP | How to fix a bug which does not allows display images inside PDF using dompdf package

Introduction

We are using a package to create and make pdf download called Dompdf (https://github.com/barryvdh/laravel-dompdf).

In some time, we needed to display the company image logo inside pdf report. we did everything we could and it did not working.
In the other words, convert image to base64.

File: PdfController.php

Make sure that option "isPhpEnabled" is true

$pdf = \App::make('dompdf.wrapper');
$pdf->setOptions(['isRemoteEnabled' => true, 'isHtml5ParserEnabled' => true, 'isPhpEnabled' => true]);

File: template_pdf.blade.php

@php
    $path   = $media->getPath();
    $type   = pathinfo($path, PATHINFO_EXTENSION);
    $data   = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
@endphp
<img src="{{$base64}}"/>

Reference: spatie/laravel-medialibrary#1269 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment