In this post, I will demonstrate how to create a Laravel PDF with an image. In my previous example, I showed how to create a?Laravel PDF from HTML. Let's now insert the image into your PDF file.
Typically, we include images on PDF for a product report, invoices with a company logo, or a screenshot of a report.
To begin, we must install Laravel. If it's already installed, just skip it.

Table of Content

  1. Laravel Installation
  2. Install dompdf Package
  3. Setup Routes and Controller
  4. Setup View

Step 1: Laravel Installation

If you don't already have a Laravel 8 installation on your local machine, run the following command:

composer create-project --prefer-dist laravel/laravel laravel-pdf-image-example

cd laravel-pdf-image-example

Step 2: Install dompdf Package

To convert HTML to PDF in Laravel, we must first install the barryvdh/laravel-dompdf package. Execute the following command:

composer require barryvdh/laravel-dompdf

Step 3: Setup Routes and Controller

Let's start by making some routes and a controller for our Laravel PDF generator.



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('/', function () {
    return view('welcome');

Route::get('pdf', [PdfController::class, 'index']);

To create a controller, use the following command:

php artisan make:controller PdfController

The generated PdfController can then be edited. Please see the following:


namespace App\Http\Controllers;

use PDF;
use Illuminate\Http\Request;

class PdfController extends Controller
    public function index() 
    	$pdf = PDF::loadView('pdf.sample-with-image', [
    		'title' => ' Laravel Pdf with Image Example',
    		'description' => 'This is an example Laravel pdf with Image tutorial.',
    		'footer' => 'by <a href=""></a>'
        return $pdf->download('sample-with-image.pdf');

Step 4: Setup View

Now, let's use an image generator to create a view for our PDF. In this example, I make a pdf folder within resources/views/ and then a blade file sample-with-image.blade.php. See the sample code below:

<!DOCTYPE html>
	<title>{{ $title }}</title>
	<p>{{ $description }}</p>


	<p>Put your text here.</p>

	<p>Place your dynamic content here.</p>

    <strong>Public Folder:</strong>
    <img src="{{ public_path('images/codesolutionstuff.jpg') }}" style="width: 20%">

    <strong>Storage Folder:</strong>
    <img src="{{ storage_path('app/public/images/codesolutionstuff.jpg') }}" style="width: 20%">



	<p style="text-align: center;">{!! $footer !!}</p>

NOTE: You must have an images folder within public and an images folder within \storage\app\public, and your image must exist in both folders. You can call it whatever you want.

Now that our code is complete, let's put it to the test by running the following commands:

php artisan serve

then enter the following URL into your browser:

I hope you will like the content and it will help you to learn Generate Laravel PDF with Image Example
If you like this content, do share.

Recommended Posts

View All

Laravel One to Many Polymorphic Relationship

laravel polymorphic relations example, laravel polymorphic one to many, laravel eloquent polymorphic relations, laravel one to many polymorphic

How to inline row editing using Laravel 9

Learn how to implement inline row editing in Laravel 9 with our step-by-step guide. Make editing data faster and more efficient on your website.

Laravel whereMonth and whereYear Example

laravel whereMonth and whereYear example, whereMonth, whereYear, where condition in laravel 8, date function in laravel 8, whereMonth and whereYear in...

Laravel 9 Capture Image from Webcam and Store in Database

Learn how to capture images from webcam using Laravel 9 and store them in a database. Step-by-step guide with code examples and best practices.

How To Create Url Shortener In Laravel 9 Application

How to create a URL shortener tool using Laravel 9's short URL package, similar to or