How to Generate a PDF in Laravel?

Updated 12 September 2023

Generating product PDFs for promotion is widely used in the laravel eCommerce systems. In this article, we are going to see how to generate PDF in laravel.

What is dompdf liberary ?

Dompdf is an HTML to PDF converter The dompdf is (mostly) the CSS 2.1 compliant HTML layout and rendering engine written in PHP language.

It is the style-driven renderer: it will download and read an external stylesheets, inline style tags, and all the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

How to generate a PDF in Laravel?

Step 1: Install the laravel-dompdf package

With the help of a composer you can easily install the dependencies of laravel-dompdf package.
Simply run this command inside your laravel project:

It will install all the dependencies of laravel-dompdf package in your laravel project.

Step 2: Configure laravel-dompdf package

Just go to Config/App.php and create these entries in providers and aliases.

Step 3: Create a blade file

Inside the resources >> views folder, create a new file called layout.blade.php.

Step 4: Create Models and Migrations for it

To create the migration run this command

To create the Model run this command

Now go to the mmigration file i.e create_pdf_data_table and add your columns.

Now run migrate command

Step 5: Create controllers and Routes

Ex: PDF contrtoller. So, run this command

Now inside your web->routes.php, Create two routes for create and store the data

Step 6: Get the data from the user

Create a form and get the data from the user and send it to the controller via he routes you have created already.

Step 7: Store your data into the database

Store the data yiou have fetched from the form and create an entry into the database:

Also, add the fillable fields inside the PDFData.php model file.

Step 8: View the data into a blade file

Step: 9 Create a route to download the pdf file

Add the following code inside the route file.

Now, update the list.blade.php file and add the Download PDF link.

Step: 10 Create pdf.blade.php file to design the PDF

We have created a simple table which will be generated inside the PDF.

Step: 11 Write a controller function to download the PDF

And this is it. You can now download your generated file into PDF Format.
Source:
DOM  PDF Github

Collaborate with Laravel Development Company to develop customized solutions for E-commerce, CRM, and third-party API integrations.

. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home