Share this:

As the demand for generating PDF files dynamically increases, Laravel has provided developers with an easy-to-use package called DomPDF to generate PDF files effortlessly. In this article, we will walk through the process of generating PDF files in Laravel 10 using DomPDF.

What is DomPDF? DomPDF is a PHP package that allows developers to create PDF files dynamically from HTML and CSS. It is an open-source package that supports UTF-8 encoded HTML and CSS, making it easier for developers to generate PDF files without having to deal with the intricacies of PDF generation.

Step 1: Install DomPDF Package

The first step to generate PDF files using DomPDF is to install the package. You can install the package via composer by running the following command:

composer require dompdf/dompdf

Step 2: Create Route

After installing the package, we need to create a route to generate the PDF file. Let’s create a route in the web.php file.

Route::get('/generate-pdf', function () {
   $pdf = App::make('dompdf.wrapper');
   $pdf->loadHTML('<h1>Welcome to Laravel 10</h1>');
   return $pdf->stream();
});

In the above route, we have created a PDF object using the App::make() method, loaded an HTML string into it, and returned the PDF file using the stream() method. Now, if you visit the /generate-pdf route in your browser, you will see the generated PDF file.

Step 3: Generate PDF from View

You can also generate a PDF file from a Blade view. Let’s create a view file named pdf.blade.php with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Generate PDF in Laravel 10 using DomPDF - LaravelTuts.com</title>
</head>
<body>
    <h1>Welcome to Laravel 10</h1>
</body>
</html>

Now, let’s modify the route to generate a PDF file from the view:

Route::get('/generate-pdf', function () {
    $pdf = App::make('dompdf.wrapper');
    $pdf->loadView('pdf');
    return $pdf->stream();
});

In the above route, we have used the loadView() method to load the pdf.blade.php view and generate a PDF file from it.

Step 4: Add CSS to PDF

You can also add CSS styles to the PDF file. Let’s modify the pdf.blade.php file to include some CSS styles:

<!DOCTYPE html>
<html>
<head>
    <title>Generate PDF in Laravel 10 using DomPDF - LaravelTuts.com</title>
    <style>
        h1 {
            color: red;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Welcome to Laravel 10</h1>
</body>
</html>

Now, if we generate the PDF file using the above view, we will see the red-colored h1 tag in the generated PDF file.

Step 5: Save PDF to Disk

If you want to save the generated PDF file to disk, you can use the save() method instead of the stream() method. Let’s modify the route to save the PDF file to the disk:

Route::get('/generate-pdf', function () {
    $pdf = App::make('dompdf.wrapper');
    $pdf->loadView('pdf');
    return $pdf->save(storage_path('app/public/pdf/generate-pdf.pdf'));
});

In the above route, we have used the save() method to save the generated PDF file to the storage/public/pdf directory.

Conclusion:

In this article, we have learned how to effortlessly generate PDF files in Laravel 10 using DomPDF. We have gone through the steps to install the DomPDF package, create a route to generate a PDF file, generate a PDF file from a Blade view, add CSS to the PDF file, and save the PDF file to the disk. With these steps, you can now generate PDF files easily in Laravel 10.

However, it is important to note that generating PDF files dynamically can be resource-intensive, especially when generating large PDF files. Therefore, it is recommended to use caching techniques to reduce the load on the server.

Overall, DomPDF is a great package to generate PDF files in Laravel 10, and with its easy-to-use API, it makes generating PDF files a breeze.

Share this:

Categorized in: