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
- Laravel Installation
- Install dompdf Package
- Setup Routes and Controller
- 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.