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.

routes.php/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('/', 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:

<?php

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' => 'codesolutionstuff.com Laravel Pdf with Image Example',
    		'description' => 'This is an example Laravel pdf with Image tutorial.',
    		'footer' => 'by <a href="https://www.codesolutionstuff.com/">codesolutionstuff.com</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>
<html>
<head>
	<title>{{ $title }}</title>
</head>
<body>
	<p>{{ $description }}</p>

	<br>

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

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

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

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

    <br/>

	<br>

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

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:

http://127.0.0.1:8000/pdf

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 9 Ajax Form Validation Example


In Laravel 9, we may ajax submit a form following validation. Laravel's ajax form validation is simple.

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 8/7/6 Google ReCaptcha v2 Form Validation


In form validation, Laravel 8,7,6 uses Google v2 Captcha/Re-Captcha. You will learn how to add Google v2 ReCaptcha form validation to a Laravel form i...

Laravel 8 Generate PDF File using DomPDF | Laravel 8 PDF


How to generate pdf from view, html, blade in laravel 8. Here, we would share with you how to generate pdf file from blade view in laravel 8

Laravel 9 CKeditor Image Upload With Example


Learn how to easily upload images in CKEditor using Laravel 9 with this step-by-step tutorial and example code. Improve your web development skills no...