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

How To Integrate Google Maps in Laravel


A number of ways are available through the Google Maps JavaScript API to build maps for web applications.

Laravel Full Text Search with Scout


You will have learned how the scout elastic search functions from today's tutorial

Laravel One to One Eloquent Relationship Tutorial


Laravel One To One Eloquent Relationships Tutorial Example, Laravel Eloquent ORM is the best possible database solution, ORM Mapping is very easy, lar...

Laravel 8 Custom 404, 500 Error Page Example


How to create custom error page in Laravel 8 and we will also try to tell you why we required to create the custom error page.

Laravel 9 Socialite Login with Facebook Tutorial


In this tutorial, we'll learn how to use the Socialite package in Laravel to login with a Facebook social networking account.