Laravel PDF Generator with DomPDF [100% Working]


Laravel

Author: Steve Alila
Reviewer: Deepak Prasad

Generating PDFs is an essential aspect of web development. Businesses and organizations often require documents compatible with various devices and easily printable.

DomPDF is a PHP library that allows developers to generate PDF documents from HTML and CSS. When combined with Laravel, DomPDF becomes a powerful tool for generating PDFs in a streamlined and efficient manner.

This article explores how to set up and use the Laravel PDF generator with DomPDF to create high-quality PDFs. Whether you're new to Laravel or an experienced developer, this guide will provide the knowledge and tools you need to create professional PDF documents for your clients or business.

 

Set up Laravel PDF generator with DomPDF

Before we can start generating PDFs with Laravel and DomPDF, we need to set up our development environment. This involves installing and configuring the necessary tools and libraries.

To use DomPDF, we first need to install it using Composer using the following command.

composer require barryvdh/laravel-dompdf

Once we have installed DomPDF, we need to configure it by creating a configuration file called dompdf.php in the config directory of our Laravel project. We can copy the configuration file from the vendor/dompdf/dompdf directory to our project's config directory using the following command:

 php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

The php artisan vendor:publish creates a config file located at config/dompdf.php. You can use the file to define local configurations to change some settings. Here are the available options and their defaults:

  • rootDir: "{app_directory}/vendor/dompdf/dompdf"
  • tempDir: "/tmp" (available in config/dompdf.php)
  • fontDir: "{app_directory}/storage/fonts" (available in config/dompdf.php)
  • fontCache: "{app_directory}/storage/fonts" (available in config/dompdf.php)
  • chroot: "{app_directory}" (available in config/dompdf.php)
  • logOutputFile: "/tmp/log.htm"
  • defaultMediaType: "screen" (available in config/dompdf.php)
  • defaultPaperSize: "a4" (available in config/dompdf.php)
  • defaultFont: "serif" (available in config/dompdf.php)
  • dpi: 96 (available in config/dompdf.php)
  • fontHeightRatio: 1.1 (available in config/dompdf.php)
  • isPhpEnabled: false (available in config/dompdf.php)
  • isRemoteEnabled: true (available in config/dompdf.php)
  • isJavascriptEnabled: true (available in config/dompdf.php)
  • isHtml5ParserEnabled: false (available in config/dompdf.php)
  • isFontSubsettingEnabled: false (available in config/dompdf.php)
  • debugPng: false
  • debugKeepTemp: false
  • debugCss: false
  • debugLayout: false
  • debugLayoutLines: true
  • debugLayoutBlocks: true
  • debugLayoutInline: true
  • debugLayoutPaddingBox: true
  • pdfBackend: "CPDF" (available in config/dompdf.php)
  • pdflibLicense: ""
  • adminUsername: "user"
  • adminPassword: "password"

Now let's create a Laravel project and use it with Laravel PDF generator.

laravel new generate_pdf
cd

This will create a new Laravel project in a directory called generate_pdf and navigate into the new project.

 

Generate PDFs with Laravel PDF generator with DomPDF

There are two main ways to generate PDFs in Laravel. One, wrap HTML tags with a DomPDF wrapper created by App::make() method. Second, you can convert a view file into PDF using Pdf::loadView method before downloading. Here are examples of each method implemented in routes/web.php:

 

Method~1: Use APP Container

use Illuminate\Support\Facades\App;

$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h2>Certificate</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolorum laborum perspiciatis ullam enim nostrum vel, impedit dolor officiis illum? Aliquam totam aliquid dicta dolorum molestias nulla laborum debitis doloremque ducimus.</p>
<p>Thank you</p>');

return $pdf->stream();   

First, we import the App facade from the Illuminate\Support\Facades namespace, which provides access to the Laravel application instance.

Next, we create a new instance of the dompdf.wrapper class, which is a Laravel-specific wrapper around the dompdf library. The App::make method is used to instantiate this wrapper class.

Then, we call the loadHTML method on the $pdf object to load some HTML content into it. This HTML content includes a heading, two paragraphs, and a message of thanks.

Finally, we call the stream method on the $pdf object to output the generated PDF to the browser as a stream. This will cause the PDF to be downloaded or displayed in the user's browser, depending on their settings.

Laravel PDF Generator with DomPDF [100% Working]

 

Method~2: create a new DOMPDF instance and load a view file

    use Barryvdh\DomPDF\Facade\Pdf;

    $pdf = Pdf::loadView('certificate');
    return $pdf->download('cert.pdf');

First, we import the Pdf facade from the Barryvdh\DomPDF\Facade namespace, which provides a static interface to the dompdf library.

Next, we create a new instance of the Pdf class by calling the static loadView method on the Pdf facade. This method loads a Laravel view named 'certificate' and passes it to the Pdf class to generate a PDF document.

Then, we call the download method on the $pdf object to download the generated PDF as a file named 'cert.pdf'. The download method prompts the user to save the PDF file, rather than displaying it in the browser.

Laravel PDF generator with DomPDF

 

Conclusion

Laravel is a compelling PHP framework that allows you to build complex web applications quickly and efficiently. One of the many benefits of Laravel is its ability to generate PDF documents, thanks to the DomPDF library easily.

Using Laravel and DomPDF together, you can create high-quality, professional PDF documents that can be downloaded, printed, and shared with others. Whether you need to generate invoices, reports, or any other type of document, Laravel and DomPDF make it easy to do so.

With its clean and intuitive syntax, Laravel is an excellent choice for you who want to focus on building your applications rather than worrying about the underlying technology. And with the power of DomPDF, you can take your applications to the next level by generating beautiful, custom PDF documents

 

Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment