A guide on how to generate a PDF file in your Laravel 8 application

Madhavi Imashi
4 min readJun 26, 2021
An image showing the conversion of HTML to PDF via Dompdf

Looking to generate a PDF file in your Laravel application?

Don’t worry if you haven’t ever tried that before. I will guide you through the steps from beginning to end until you download your PDF file by yourself.

In general, Portable File Format(PDF) can be used at instances like generating reports, CVs, detailed documents and etc.. which provides some information to the users.

So in this tutorial, I will demonstrate the steps to generate a report PDF file to display the list of rooms in a hotel system in tabular format. The content to be displayed in the PDF file can be anything depending on your requirement. (Note that: If you have already created a blade file for your PDF, you can skip the first step.)

1. Create the report of room list in a Blade View template

Since we need to display room details in the PDF file, I will first create a blade file to display the fetched data in a tabular format(You will understand this fetching part when you refer the step 5 & 6).

Inside the resource/views folder create a new blade file. Name it as report.blade.php and code as below to get the design view of room list PDF as in Figure 1.1.

Figure 1.0 (report.blade.php)
Figure 1.1

Now we have created the design view of the report blade file which we will be converting to a PDF file at the end.

But how will you generate this PDF file?

Create a button where ever you want it to be in your application which generates the PDF once it’s clicked.

2.Create a Report button and set the path to it

Figure 1.2

In the blade file related to the page where you need to display the Report button, Create a button and set the correct path inside href tag.

(For this demonstration, I have created a separate blade file called index.blade.php to display the room list as well as the Report button.)

You can use the HTML code in Figure 1.2 in your blade file to create a similar looking button for the report as in the Figure 1.3

Figure 1.3

Now that you have designed almost everything regard to the PDF file, Let’s move on to the last few steps to enable the DOMPDF package in our application in order to get the use of DOMDPF package on behalf of PDF downloading.

3. Install DOMPDF package

DOMPDF is an HTML to PDF convertor for PHP in general. It is a style-driven render which can download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements.

It also needs a very simple step to install this package in Laravel 8.

Type the following command in your terminal to install it! (Make sure the terminal is opened in the correct folder path for your application)

4. Configure DOMPDF package

Just installing the DomPDF fpackage is not enough.

You have to add the service provider and the aliase as below inside the config/app.php file of your app.

Finally, Execute the following command in the terminal to publish the assets from vendor.

Then a list of packages will appear on the terminal window. You just have to selsct the “Provider: Barryvdh\DomPDF\ServiceProvider option from that list(You may have to type the number belongs to that option and hit enter).

Now you will be able to see a new file dompdf.php has created inside the config folder.

In order to use this DomPDF library, don’t forget to use the following attribute in your controller class as in Figure 1.4.

Now you have enabled the DOMPDF package in your application. So what remains to be done at all is to create a function in the controller class and to define the route.

5. Create a function in the Controller class to fetch data from Database

Controller class acts as a directing traffic between Views and Models. So we can use the controller class to communicate with the model class in order to fetch data from the database.

Go to app/Http/Controllers/HotelRoomController.php and define a sepearte function(ex: RoomListPDF()). This function will;

— fetch all the records in the database table,

— share those data with the report blade file which we created in the step 1

— and allow downloading the PDF file.

Figure 1.4

6. Define a route

Go to routes/web.php file and create a seperate route/path to download the PDF file.

(Then when you click on the Report button, the browser will navigate to the controller class and execute the RoomListPDF() function and generate the PDF successfully).

You’re almost Done!!!!!

Run the application and generate the PDF file by clicking on the Report button that you’ve created.

command to Run the application

Ultimately, You have converted the HTML code into a PDF file which means you’re done!!

--

--