Laravel 12 - How to Generate PDF

Touseef Afridi
19 Jun 25

Laravel 12 - How to Generate PDF

In this tutorial, we will learn how to generate PDF files in Laravel 12 using DOMPDF. We'll cover the installation, basic setup, and simple steps to create and download PDFs in your Laravel app.


If you're a video person, feel free to skip the post and check out the video instead!


Quick Overview

This guide walks through the process of generating PDFs in a Laravel application using the DOMPDF package. It begins with setting up a new Laravel 12 project or using an existing one, opting for a minimal setup with MySQL and Pest. Once inside the project directory, the barryvdh/laravel-dompdf package is installed to handle PDF rendering from HTML. The package’s configuration is then published for customization. A dedicated controller is created to generate PDFs, followed by defining a route to trigger the download. A Blade view (myPDF.blade.php) is used to design the PDF layout, incorporating dynamic data such as the title and date. After starting the Laravel development server, visiting the specified route generates and downloads the PDF. If each step is followed correctly, the PDF will include all the intended content from your Blade view.

Step # 1 : Create a Laravel 12 Project.

You can either start fresh or work with an existing Laravel 12 application. If Laravel is installed globally, you can quickly create a new project by running the following command.
laravel new pdf
Alternatively, you can use Composer to create the project.
composer create-project laravel/laravel pdf
During setup, consider these options for a clean starting point.
  • Starter Kit: Select None (no built-in auth).
  • Testing Framework: Choose Pest for a lightweight test suite.
  • Database: Pick MySQL as your preferred database.
  • Migrations: Type yes to run the default migrations.
  • Frontend: Confirm with yes to install frontend dependencies and build assets.

This sets up a fresh Laravel 12 project named pdf, ready for development with minimal scaffolding.

Step # 2 : Navigate to the Project.

Open your terminal (e.g., Git Bash or CMD) and move into your project directory.
cd c:xampp/htdocs/pdf
You’re now inside the Laravel project folder and can start building, running commands, or customizing your application.

Step # 3 : Install DOMPDF for PDF Generation.

To generate PDFs from HTML in your Laravel 12 app, install the barryvdh/laravel-dompdf package using Composer.
composer require barryvdh/laravel-dompdf
This package acts as a simple Laravel wrapper around the DOMPDF library, making it easy to convert Blade views or raw HTML into downloadable PDFs.

Step # 4 : Publish the Configuration (Optional).

To adjust default settings like paper size or orientation, publish the config file.
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
After running this command, a dompdf.php file will appear in your config folder, where you can tweak PDF options to better fit your project’s requirements.

Step # 5 : Set Up the PDF Controller.

To manage PDF generation in your Laravel 12 app, start by creating a dedicated controller.
php artisan make:controller PDFController
Once created, open the new controller file and add the following logic.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PDFController extends Controller
{
    public function generatePDF()
    {
        $data = [
            'title' => 'Laravel 12 - Code Shortcut',
            'date' => now()->format('m/d/Y'),
        ];
        $pdf = PDF::loadView('myPDF', $data);
        return $pdf->download('codeshotcut.pdf');
    }
}
The generatePDF method sets up a simple data array containing a title and the current date, then passes it to a Blade view named myPDF. This view is rendered into a PDF using the DOMPDF package and returned as a downloadable file called codeshotcut.pdf. We’ll create the myPDF view later in Step 7, where the structure and content of the PDF will be defined.

Step # 6 : Define a Route.

Start by importing the PDFController at the top of your web.php file.
use App\Http\Controllers\PDFController;
Then, register a route to handle the PDF generation.
Route::get('/pdf', [PDFController::class, 'generatePDF']);
This route connects the /pdf URL to the generatePDF method in the controller. When accessed in the browser, it will trigger the PDF creation process and return the generated file for download.

Step # 7 : Create the PDF View.

Next, let’s create the Blade view that will define the layout and content of the PDF. Inside the resources/views directory, create a file named myPDF.blade.php, and add the following HTML.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ $title }}</title>
    <style>
        body {
            font-family: DejaVu Sans, sans-serif;
            padding: 32px;
            color: #1f2937;
            background: #ffffff;
        }
        h1 {
            font-size: 24px;
            color: #2563eb;
            margin-bottom: 8px;
        }
        .date {
            font-size: 12px;
            color: #6b7280;
            margin-bottom: 24px;
        }
        p {
            font-size: 14px;
            line-height: 1.6;
            margin-bottom: 16px;
        }
        .footer {
            margin-top: 40px;
            font-size: 12px;
            text-align: center;
            color: #9ca3af;
        }
    </style>
</head>
<body>
    <h1>{{ $title }}</h1>
    <div class="date">Generated on {{ $date }}</div>
    <p>
        Welcome to your Laravel 12 PDF demo! This PDF is generated with the DOMPDF package using a simple Blade view layout.
    </p>
    <p>
        Lorem Ipsum has been the industry's standard dummy text since the 1500s. It's used as placeholder content across design, development, and documentation.
    </p>
    <p>
        Thanks for checking out this example from <strong>Code Shotcut</strong> — your shortcut to Laravel mastery.
    </p>
    <div class="footer">
        © {{ date('Y') }} Code Shotcut
    </div>
</body>
</html>
This view uses the $title and $date variables provided by the controller. When the PDF is generated, this HTML will be rendered and converted into the actual PDF content. You can customize this template further to match your layout or include other dynamic data.

Step # 8 : Test the PDF Generation.

Now that everything is set up, you can test it. Start the Laravel development server with.
php artisan serve
Then open your browser and visit: http://127.0.0.1:8000/pdf. This will trigger the generatePDF method in your controller. If all steps are correctly followed, including the controller, route, and view, the app will generate a PDF using the defined layout and data and prompt a download named codeshotcut.pdf.


Conclusion

By following this guide, you've successfully integrated PDF generation into your Laravel 12 application using the DOMPDF package. Your setup now enables users to download PDFs with dynamic content like titles and dates, all structured through a Blade view. The process is fully functional with the controller, route, and view working in harmony. You can easily extend this by customizing the view layout or adjusting DOMPDF’s settings to match your design needs.
For more flexibility or advanced features, be sure to check out the official DOMPDF documentation.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering