How to Generate PDF Files Using Dompdf in Laravel

Generate PDF Files in Laravel Using Dompdf

How to generate a PDF using Dompdf in Laravel:

In the digital age, generating PDF files dynamically is a common requirement for many web applications. Laravel, being a popular PHP framework, provides various libraries and packages to simplify this task. One such library is Dompdf, which allows you to easily create PDF files from HTML views within your Laravel application. In this tutorial, we will explore how to generate PDF files using Dompdf in Laravel step by step.

  1. First, install the dompdf in Laravel via composer by running the following command in your terminal:
composer require dompdf/dompdf

2. Next, create a new controller by running the following command in your terminal:

php artisan make:controller PdfController

app\Http\Controllers\PdfController.php

<?php

namespace App\Http\Controllers;

use Dompdf\Dompdf;
use Illuminate\Http\Request;


class PdfController extends Controller
{
    public function generatePdf(Request $request)
    {
        // Get the HTML content to be converted to PDF
        $html = "<h1 style='text-align:center;'>ALgocodersmind|DomPDF|generate|</h1>";
        $html .= $request->input('html');
        
        // Instantiate Dompdf
        $dompdf = new Dompdf();
        
        // Load the HTML content
        $dompdf->loadHtml($html);
        
        // Set the paper size and orientation
        $dompdf->setPaper('A4', 'portrait');
        
        // Render the PDF
        $dompdf->render();
        
        // Output the generated PDF to the browser
        return $dompdf->stream();
    }
  
}

3. Open the newly created PdfController and add the following code to the top of the file to import the necessary classes:

use Dompdf\Dompdf;
use Illuminate\Http\Request;

4. Add a new method to the controller that will generate the PDF. In this example, the method is called generatePdf():

public function generatePdf(Request $request)
{
    // Get the HTML content to be converted to PDF
    $html = $request->input('html');
    
    // Instantiate Dompdf
    $dompdf = new Dompdf();
    
    // Load the HTML content
    $dompdf->loadHtml($html);
    
    // Set the paper size and orientation
    $dompdf->setPaper('A4', 'portrait');
    
    // Render the PDF
    $dompdf->render();
    
    // Output the generated PDF to the browser
    return $dompdf->stream();
}

5. Add a route to the web.php file that points to the generatePdf() method of the PdfController. In this example, the route is defined as follows:

Route::get('/get-pdf', function () {
    return view('pdf');
});
Route::post('/generate-pdf', [PdfController::class, 'generatePdf']);

6. Create a form in your HTML view that will allow the user to input the HTML content to be converted to PDF. In this example, the form is defined as follows resources\views\pdf.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        form {
            max-width: 500px;
            margin: 20px auto;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
            padding: 20px;
        }

        label {
            display: block;
            margin-bottom: 10px;
            color: #333;
        }

        textarea {
            width: 100%;
            height: 200px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            padding: 10px;
            font-size: 16px;
        }

        button[type="submit"] {
            background-color: #007bff;
            color: #fff;
            border: none;
            padding: 10px 20px;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button[type="submit"]:hover {
            background-color: #0069d9;
        }
    </style>
    </head>

    <body>
        <form action="/generate-pdf" method="post">
            @csrf
            <label for="html">Enter HTML:</label>
            <textarea name="html" id="html"></textarea>
            <button type="submit">Generate PDF</button>
        </form>
    </body>
    </html>

Output Images:

dompdf in laravel

dompdf in laravel

Our Recommendation

Avatar of Akhand Pratap Singh

Akhand Pratap Singh

Greetings and a warm welcome to my website! I am Akhand Pratap Singh, a dedicated professional web developer and passionate blogger.

Related Post

Leave a Comment





Newsletter

Subscribe for latest updates

We don't spam.

Loading

Categories