How to generate PDFs in Laravel from Blade Views

 

In Laravel, you can generate PDFs from Blade views using various packages. One popular package for this purpose is ‘ laravel-dompdf ’. Below are the steps to set up and use ‘ laravel-dompdf ‘ to generate PDFs in Laravel:

Step 1: Install the Package

Run the following command to install the  ‘ laravel-dompdf  ‘  package:

composer require barryvdh/laravel-dompdf

Step 2: Register the Service Provider

For Laravel 5.5 and above, the service provider is auto-discovered. For older versions, add the service provider to your  config/app.php :

 

Step 3: Publish Configuration (Optional)

You can publish the configuration file if you want to customize some settings:

 

Step 4: Generate a PDF from a Blade View

Now, you can use the  ’  dompdf  ’  facade to generate a PDF from a Blade view. Here's a basic example in a controller:

 

In this example, a Blade view named  ‘pdf.invoice’  is loaded with some data. You can create this Blade view in the ‘ resources/views/pdf ’ directory.

 

Step 5: Create the Blade View

Create a Blade view file at ‘resources/views/pdf/invoice.blade.php’:

 

Step 6: Test the Route

Define a route in ‘routes/web.php’ to test the PDF generation:

 

Visit  ‘ /generate-pdf  ’  in your browser, and it should download a PDF file containing the content from the Blade view.

 

In conclusion, generating PDFs in Laravel using 'laravel-dompdf' is a streamlined process. Install the package, set up the service provider, customize configurations if needed, generate a PDF from a Blade view, create the view file, and test the route for seamless PDF generation.

We Serve clients globally in diverse industries

Stay Upto Date With Our Newsletter.