Laravel 10 Add Image to PDF File Example

07-Jun-2023

.

Admin

Laravel 10 Add Image to PDF File Example

Hi Dev,

In this post, we will learn Laravel 10 pdf images not showing. you can understand the concept of Laravel 10 pdf-adds image example. you will learn Laravel 10 pdf insert image. This post will give you a simple example of Laravel 10 add the image to the pdf example. Following the below step for Laravel 10 generate a pdf with the image.

PDF is one of the basic requirements when you are working on an ERP-level project or e-commerce website. we may need to create a pdf file or maybe you also need to add the image with a pdf file, which might be a logo or product image, etc. So, here I will give you a very simple example of creating a pdf file with Laravel 10.

You need to just follow the below step to create a pdf file and also can download it. So let's do the below steps.

Step 1: Download Laravel


Let us begin the tutorial by installing a new Laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

Step 2: Install dompdf Package

first of all, we will install barryvdh/laravel-dompdf composer package by following the composer command in your Laravel 10 application.

composer require barryvdh/laravel-dompdf

After successfully installing the package, open the config/app.php file and add the service provider and alias.

config/app.php

'providers' => [

Barryvdh\DomPDF\ServiceProvider::class,

],

'aliases' => [

'PDF' => Barryvdh\DomPDF\Facade::class,

]

Step 3: Add Route

In this step, we need to create routes for an item listing. so open your "routes/web.php" file and add the following route.

routes/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('generate-pdf',[PDFController::class,'generatePDF']);

Step 4: Add Controller

Here, we require to create a new controller PDFController that will manage generate pdf method of the route. So let's put below the code.

app/Http/Controllers/PDFController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;

class PDFController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function generatePDF()

{

$data = ['title' => 'Welcome to NiceSnippets.com'];

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

return $pdf->download('NiceSnippets.pdf');

}

}

Step 5: Add Blade File

Here, you have to add two images on the following path:

public/dummy.jpg

storage/app/public/dummy.jpg

In Last step, let's create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:

resources/views/myPDF.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 10 Add Image To PDF File Example - NiceSnippets.com</title>

</head>

<body>

<h1>{{ $title }}</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<br/>

<strong>Public Folder:</strong>

<img src="{{ public_path('dummy.jpg') }}" style="width: 200px; height: 200px">

<br/>

<strong>Storage Folder:</strong>

<img src="{{ storage_path('app/public/dummy.jpg') }}" style="width: 200px; height: 200px">

</body>

</html>

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the Laravel app:

php artisan serve

Now, you have to open the web browser, type the given URL and view the app output:

http://localhost:8000/generate-pdf

I hope it can help you...

#Laravel 10