How to Add Image to PDF File Example in laravel 9?

Apr 28, 2022 . Admin

Hi Dev,

In this post, we will learn laravel 9 pdf images 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. follow bellow step for laravel 9 generate a pdf with image.

PDF is one of the basic requirements 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 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.

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

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.

config/app.php
'providers' => [
    Barryvdh\DomPDF\ServiceProvider::class,
],
  
'aliases' => [
    'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3: Create 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.

routes/web.php
<?php

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!
|
*/

Route::get('generate-pdf',[PDFController::class,'generatePDF']);
Step 4: Create 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.

app/Http/Controllers/PDFController.php

<?php

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 NiceSnippets.com'];
        $pdf = PDF::loadView('myPDF', $data);
        return $pdf->download('NiceSnippets.pdf');
    }
}
Step 5: Create Blade File

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

public/dummy.jpg storage/app/public/dummy.jpg

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

resources/views/myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Add Image To PDF File Example - Mywebtuts.com</title>
</head>
<body>
    <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>    
    <br/>
    <strong>Public Folder:</strong>
    <img src="{{ public_path('dummy.jpg') }}" style="width: 200px; height: 200px">
    <br/>
    <strong>Storage Folder:</strong>
    <img src="{{ storage_path('app/public/dummy.jpg') }}" style="width: 200px; height: 200px">
</body>
</html>

Now we are ready to run this example and check it...

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:

http://localhost:8000/generate-pdf

I hope it can help you...

#Laravel 9