Creating PDFs in Laravel 8 Using DomPDF: A Step-by-Step Tutorial

Creating PDFs in Laravel 8 Using DomPDF: A Step-by-Step Tutorial

In web development, generating PDF files dynamically from HTML content is a common requirement. Whether it’s generating reports, invoices, or any other printable document, having the ability to convert HTML to PDF seamlessly is invaluable. Laravel, a popular PHP framework, offers several packages to achieve this task, and one of the most widely used is DomPDF. In this tutorial, we’ll walk through the process of setting up and using DomPDF in Laravel 8 to generate PDF files from HTML content.

Prerequisites

Before we begin, ensure you have the following prerequisites installed on your system:

  • PHP >= 7.3
  • Composer
  • Laravel 8 installed
  • Basic understanding of Laravel and PHP

Step 1: Install DomPDF Package

Firstly, we need to install the DomPDF package into our Laravel project. Open your terminal and navigate to your Laravel project directory. Then, run the following Composer command:

This command will fetch and install the DomPDF library along with its dependencies into your Laravel project.

Step 2: Create a Route

Next, let’s define a route that will be responsible for generating our PDF. Open your routes/web.php file and add the following route definition:

 php
 use App\Http\Controllers\PDFController;
 Route::get('/generate-pdf', [PDFController::class, 'generatePDF'])-
 >name('generate.pdf');

Step 3: Create a Controller

Now, let’s create a controller where we’ll define the logic for generating our PDF. Run the following Artisan command in your terminal:

 bash
 php artisan make:controller PDFController

This command will create a new controller named PDFController. Open this controller located at app/Http/Controllers/PDFController.php and add the following method:

 php
 <?php

 namespace App\Http\Controllers;

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

 class PDFController extends Controller
 {
    public function generatePDF()
    {
        // Generate PDF logic here
    }
 }

Step 4: Generate PDF

In the generatePDF() method, let’s add the logic to generate our PDF using DomPDF. Here’s how you can do it:

 php
 public function generatePDF()
 {
    // HTML content to be converted to PDF
    $html = '<h1>Hello, World!</h1>';

    // Instantiate Dompdf with our options
    $dompdf = new Dompdf();

    // Load HTML content
    $dompdf->loadHtml($html);

    // (Optional) Set paper size and orientation
    $dompdf->setPaper('A4', 'portrait');

    // Render HTML as PDF
    $dompdf->render();

    // Output generated PDF to browser
    $dompdf->stream('document.pdf');
 }

Step 5: Test the Route

Now that we’ve set up everything, it’s time to test our route. Start your Laravel development server by running:

 bash
 php artisan serve

Then, navigate to http://localhost:8000/generate-pdf in your web browser. You should see a PDF file containing the text “Hello, World!” displayed.

Conclusion

In this tutorial, we’ve learned how to generate PDF files from HTML content in Laravel 8 using the DomPDF library. With this knowledge, you can now dynamically create PDF documents for various purposes within your Laravel applications. DomPDF offers extensive features for customizing PDF output, allowing you to tailor the generated documents to your specific needs. Experiment with different HTML structures and styles to create professional-looking PDFs tailored to your application requirements.

Request a Quote

    captcha

    Baiju M

    Senior Business Developer

    Send us a Mail
    × How can I help you?