Laravel 9 Add Image to PDF File Example




Laravel 9 Add Image to PDF File Example

Hi Dev,

In this post, we will learn laravel 9 pdf images not showing. you can understand the concept of laravel 9 pdf add image example. you will learn laravel 9 pdf insert image. This post will give you a simple example of laravel 9 add the image to the pdf example. Following the bellow step for laravel 9 generate a pdf with the image.

PDF is one of the basic requirement when you are working with erp level project or e-commerce website. we may need to create a pdf file or maybe you also need to add the image with a pdf file, might be a logo or product image, etc. So, here I will give you a very simple example for creating a pdf file with laravel 9.

You need to just follow the bellow step to create a pdf file and also can download it. So let's do bellow steps.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Install dompdf Package

first of all, we will install barryvdh/laravel-dompdf composer package by following the composer command in your laravel 9 application.

composer require barryvdh/laravel-dompdf

After successfully install the package, open the config/app.php file and add the service provider and alias.


'providers' => [



'aliases' => [

'PDF' => Barryvdh\DomPDF\Facade::class,


Step 3: Add Route

In this step, we need to create routes for an item listing. so open your "routes/web.php" file and add the following route.



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;



| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!




Step 4: Add Controller

Here, we require to create a new controller PDFController that will manage generate pdf method of the route. So let's put bellow the code.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;

class PDFController extends Controller



* Display a listing of the resource.


* @return \Illuminate\Http\Response


public function generatePDF()


$data = ['title' => 'Welcome to'];

$pdf = PDF::loadView('myPDF', $data);

return $pdf->download('NiceSnippets.pdf');



Step 5: Add Blade File

Here, you have to add two images on the following path:



In Last step, let's create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:


<!DOCTYPE html>



<title>Laravel 9 Add Image To PDF File Example -</title>



<h1>{{ $title }}</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>


<strong>Public Folder:</strong>

<img src="{{ public_path('dummy.jpg') }}" style="width: 200px; height: 200px">


<strong>Storage Folder:</strong>

<img src="{{ storage_path('app/public/dummy.jpg') }}" style="width: 200px; height: 200px">



Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:


I hope it can help you...

#Laravel 9