DEV Community

Cover image for Generate Laravel PDF with Image Example
Code And Deploy
Code And Deploy

Posted on

Generate Laravel PDF with Image Example

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/generate-laravel-pdf-with-image-example

In this post, I will show you an example of how to generate a Laravel PDF with an image. In my previous example, I have an example of how to implement Laravel PDF from HTML. Now let's include the image in your PDF file.

Usually, we include images on PDF for a product report, invoices that have a company logo, or maybe a report that is a screenshot.

To start we need to install Laravel. Just skip if already installed.

Step 1: Laravel Installation

If you don't have a Laravel 8 install in your local just run the following command below:

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

cd laravel-pdf-image-example

Enter fullscreen mode Exit fullscreen mode

Step 2: Install dompdf Package

To generate HTML to PDF in Laravel we need to install barryvdh/laravel-dompdf package. Run the following command below:

composer require barryvdh/laravel-dompdf
Enter fullscreen mode Exit fullscreen mode

Step 3: Setup Routes and Controller

Let's create routes and 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']);
Enter fullscreen mode Exit fullscreen mode

Run the command below to make a controller:

php artisan make:controller PdfController
Enter fullscreen mode Exit fullscreen mode

Then edit the PdfController generated. See below:

<?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' => 'CodeAndDeploy.com Laravel Pdf with Image Example',
            'description' => 'This is an example Laravel pdf with Image tutorial.',
            'footer' => 'by <a href="https://codeanddeploy.com">codeanddeploy.com</a>'
        ]);

        return $pdf->download('sample-with-image.pdf');
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Setup View

Now, let's set up our view for our PDF with an image generator. In this example, I create a pdf folder inside resources/views/then create a blade file sample-with-image.blade.php. See below sample code:

<!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/codeanddeploy.jpg') }}" style="width: 20%">

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

    <br/>

    <br>

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

NOTE: You must have images folder inside public and images folder inside \storage\app\public and your image must exist in the stated folder. You can name it what you want.

Now, our code is ready let's test it by running the commands below:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

then run the URL below to your browser:


http://127.0.0.1:8000/pdf
Enter fullscreen mode Exit fullscreen mode

Here is the example generated a result of Laravel PDF with images using dompdf:

generate-laravel-pdf

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/generate-laravel-pdf-with-image-example if you want to download this code.

Happy coding :)

Top comments (0)