Laravel 12 PDF File Create in Dompdf

Laravel 12 dompdf package is popular to create or geneate pdf file. In this whole article we will install fresh laravel 12 version and also use barryvdh/laravel-dompdf for generating pdf file. This package can easily create pdf file from blade file. Here is the steps we will follow to reach our goal

  1. Install Fresh 12 Project
  2. Install Necessary barryvdh/laravel-dompdf Composer Package
  3. Declare Routes 
  4. Create Blade File
  5. Preview PDF in Browser

Install Fresh 12 Project

For installing dompdf in laravel 12 project we will install fresh laravel 12 empty project. You can also use inside your existing laravel 9,10,11 project also. 

composer create-project --prefer-dist laravel/laravel laravel12-dompdf 12.*
cd laravel12-dompdf

Install Necessary barryvdh/laravel-dompdf Composer Package

We will now install required barryvdh/laravel-dompdf php composer package. This package will help us  to create pdf file from our application. For installing we will use below command as well

composer require barryvdh/laravel-dompdf
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
Declare Routes

We will now declare routes and add necessary code for generating pdf file. Here we have not used any controller. You can copy and change your code accordingly for using inside controller

use Illuminate\Support\Facades\Route;
use Barryvdh\DomPDF\Facade\Pdf;

Route::get('/generate-pdf', function () {
    $data = [
        'title' => 'Laravel Dompdf File',
        'content' => 'This is a sample PDF generated Laravel Dompdf from programmingmindset.com'
    ];

     $pdf = Pdf::loadView('pdf.sample', $data);
    return $pdf->stream('sample.pdf'); //this will load pdf in browser
});

Read also: Real time notification using reverb laravel 11

Read also: Laravel 11 invoice generate using dompdf1

To download directly after hitting the route, you can call download method

Route::get('/generate-pdf', function () {
    $data = [
        'title' => 'Laravel Dompdf File',
        'content' => 'This is a sample PDF generated Laravel Dompdf from programmingmindset.com'
    ];

     $pdf = Pdf::loadView('pdf.sample', $data);
    return $pdf->download('sample.pdf');
});
Add Blade File

As we will generate pdf file from our existing blade file, so we will have to create a blade file. Let's create a blade at resources/views/pdf/sample.blade.php

We have used dummy text inside blade file. You should change it according to need and also you can pass variable from loadView method 

<!DOCTYPE html>
<html>
<head>
    <title>{{ $title }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        .container {
            text-align: center;
            margin-top: 50px;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>{{ $title }}</h1>
    <p>{{ $content }}</p>

</div>
<div class="text-content">
    What is Lorem Ipsum?
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
    standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a
    type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
    remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
    Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of
    Lorem Ipsum.

    <br>
    <br>
    Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
    "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem
    aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo
    enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui
    ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur,
    adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat
    voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut
    aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil
    molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

    1914 translation by H. Rackham
    "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will
    give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the
    master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but
    because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful.
    Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because
    occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial
    example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who
    has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who
    avoids a pain that
</div>

</body>
</html>

After hitting the route in browser 127.0.0.1:8000/generate-pdf we will see pdf file as below

laravel 12 dompdf pdf file create - programmingmindset.com


Hope that this tutorial helped a lot to generate pdf file. You can read more article related to laravel, cloud , php etc. Stay tune 

Tags: