How to Add Image to PDF file in Laravel 10

Websolutionstuff | Dec-20-2023 | Categories : Laravel PHP

Greetings, Laravel enthusiasts! Today, let's explore a practical aspect of web development – adding images to PDF files using Laravel 10. Whether you're creating invoices, reports, or any other document, this step-by-step guide will empower you to seamlessly integrate images into your PDFs.

Join me on this journey, and let's enhance our Laravel skills together.

In this article, we'll how to add an image to a pdf file in laravel 10. Also, we are using barryvdh/laravel-dompdf package to generate PDF files in laravel 8/9/10. You can add image on PDF in laravel 8, Laravel 9 and Laravel 10.

Step 1: Install and Set Up Laravel 10

Ensure you have Laravel 10 installed on your machine. If not, you can create a new project with:

composer create-project laravel/laravel mypdfproject
cd mypdfproject

 

Step 2: Install Required Packages

We'll need the barryvdh/laravel-dompdf package for handling PDFs. Install it using Composer:

composer require barryvdh/laravel-dompdf
 
Step 3: Configure Laravel-dompdf Package

Add the service provider and facade in your config/app.php:

'providers' => [
    // ...
    Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [
    // ...
    'PDF' => Barryvdh\DomPDF\Facade::class,
],

 

 

Step 4: Generate PDF Controller

Create a new controller for handling PDF generation:

php artisan make:controller PDFController

 

Step 5: Add Image to PDF Controller

In your PDFController.php file, use the following code to add an image to the PDF:

use PDF;
use Illuminate\Http\Request;

class PDFController extends Controller
{
    public function generatePDF()
    {
        $data = [
            'imagePath' => public_path('images/your_image.jpg'),
            'title' => "Add images to PDF files in Laravel 10 - Websolutionstuff"
            // Add other data as needed
        ];

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

        return $pdf->stream('document.pdf');
    }
}

 

Step 6: Create Blade View

Create a new Blade view file, document.blade.php, in the resources/views/pdf/ directory. Use the following code to embed the image in the PDF:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel 9/10 Add Image To PDF File - Websolutionstuff</title>
</head>
<body>
    <h2>{{ $title }}</h2>
    <img src="{{ $imagePath }}" alt="Image">
    <br><br/>
    <strong>Public Folder:</strong>
    <img src="{{ public_path('laravel_10.jpg') }}" style="width: 100%;">
    <br/>
    <strong>Storage Folder:</strong>
    <img src="{{ storage_path('app/public/laravel.png') }}" style="width: 100%;">
</body>
</html>

 

Step 7: Define Route

In your web.php file, define a route to trigger the PDF generation:

use App\Http\Controllers\PDFController;

Route::get('/generate-pdf', [PDFController::class, 'generatePDF']);

 

Step 8: Test Your Application

Run your Laravel application:

php artisan serve
 
Conclusion:

And there you have it! You've successfully added images to your PDF files in Laravel 10. With the power of the barryvdh/laravel-dompdf package. Also, you can use it in Laravel 8 and Laravel 9.

 


You might also like:

Recommended Post
Featured Post
Laravel Rollback Targeted Migration Reversals
Laravel Rollback Targeted Migr...

As a Laravel developer, I understand the significance of migrations in managing database changes and maintaining a consi...

Read More

May-29-2023

How to Generate QR Code in Node.js
How to Generate QR Code in Nod...

In this example we will see how to generate QR Code in Node.js application. In this example we will use qrcode npm...

Read More

Sep-20-2021

Laravel 9 Has Many Through Relationship Example
Laravel 9 Has Many Through Rel...

In this article, we will see that laravel 9 has many through relationship example. hasManyThrough relationship is diffic...

Read More

Apr-04-2022

How To Convert Image To Base64 In Laravel 9
How To Convert Image To Base64...

In this article, we will see how to convert an image to base64 in laravel 9. Here, we will convert the image to bas...

Read More

Dec-29-2022