How to Generate PDF File using DomPDF – Tutorial by Example

In this tutorial, we will explore how to generate PDF files in Laravel by using the DomPDF package. Generating PDF files is a common requirement when developing ERP systems, e-commerce websites, or applications that require reports or invoices in PDF format. We will walk through a simple example of creating and downloading a PDF file…

By.

•

min read

In this tutorial, we will explore how to generate PDF files in Laravel by using the DomPDF package. Generating PDF files is a common requirement when developing ERP systems, e-commerce websites, or applications that require reports or invoices in PDF format.

We will walk through a simple example of creating and downloading a PDF file in Laravel 8.

[lwptoc]

 

Step 1: Install Laravel 8

To start, we need to create a fresh Laravel 8 application. Open your terminal or command prompt and run the following command:

composer create-project --prefer-dist laravel/laravel projectName

 

Step 2: Install DomPDF Package

Now, we need to install the barryvdh/laravel-dompdf composer package in our Laravel 8 application by running the following command:

composer require barryvdh/laravel-dompdf

After successfully installing the package, open the config/app.php file and add the service provider and alias as shown below:

'providers' => [
    // ...
    Barryvdh\DomPDF\ServiceProvider::class,
],
  
'aliases' => [
    // ...
    'PDF' => Barryvdh\DomPDF\Facade::class,
]

 

Step 3: Add Route

In this step, we will create a route for generating the PDF. Open the routes/web.php file and add the following route:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
  
Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

 

Step 4: Add Controller

Now, we need to create a new controller called PDFController that will handle the generatePDF method for our route. Add the following code:

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use PDF;
  
class PDFController extends Controller
{
    public function generatePDF()
    {
        $data = [
            'title' => 'Welcome to OurWebsite.com',
            'date' => date('m/d/Y')
        ];
          
        $pdf = PDF::loadView('ourPDF', $data);
    
        return $pdf->download('OurWebsite.pdf');
    }
}

 

Step 5: Create View File

Finally, let’s create a view file named ourPDF.blade.php (located in resources/views/ourPDF.blade.php) for the layout of the PDF file and insert the following code:

<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>{{ $date }}</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident</p>

</body>
</html>

With this setup, you can download the generated PDF file as shown below:

http://your-app-url/generate-pdf

Now, you are ready to run this example and check the results.

 

FAQs

 

  1. What is DomPDF?DomPDF is a package that allows you to generate PDF files from HTML content. It is widely used in PHP applications, including Laravel, to create PDF files from HTML templates.
  2. Can I customize the PDF layout and styling?Yes, you can customize the PDF layout and styling by modifying the HTML and CSS in the view file.
  3. How do I use a different PDF package in Laravel?There are several other PDF packages available for Laravel, such as Snappy (based on wkhtmltopdf). To use a different package, follow its installation and usage instructions.
  4. Can I generate a PDF file from a database?Yes, you can generate a PDF file from database records by fetching the data in the controller and passing it to the view file. Then, use the data in the HTML template to create the desired PDF layout.
  5. Is it possible to create a PDF file with images?Yes, you can include images in the PDF file by adding the <img> tag in the HTML template and specify the image source URL.

 

Conclusion

We have successfully created a Laravel 8 application that generates a PDF file using the DomPDF package.

This tutorial demonstrated the process of installing Laravel 8, adding the DomPDF package, configuring routes and controllers, and creating the view file for the PDF layout. You can now generate PDF files in your Laravel applications with ease.

Leave a Reply

Your email address will not be published. Required fields are marked *