Export data to PDF file using laravel-dompdf in Laravel 5

In this article, we will talk about the steps to export data to PDF in Laravel 5. We will use a package named laravel-dompdf for it. We have seen that many of the web applications need to generate PDF file from the data in the database. In this article, we will use systems users list to export to pdf.

We can use the package laravel-dompdf to download pdf file or also to stream it online. For this, we can use two functions

let’s get started!

Create Project

We will create a fresh laravel project for this article. We will create via composer.

Database Setup

We will use mysql database. Lets create a new database kodementor and do required setting in .env file.

Create Authentication

Since, we are going to export our user list, we will create a laravel default authentication with the following command:

This will create a folder in App/Http/Controllers directory named Auth folder where controllers for login and registration are stored. Similarly, it also create view files inside Resources/views directory.

Migrate Tables

Since, we already have default migration for users and password reset tables. We will just migrate these migrations to create tables users and password_resets.

Create Routes

We will create three routes: listing users, download pdf and view pdf on browser as /users, /view-pdf and download-pdf.

Create Controller

Now, it’s time to create our controller. We will create a UserController. We will define three methods ie index, viewPDF and downloadPDF. Index method will return a blade file which displays list of users. Similarly, viewPDF will open a pdf file in the browser. Lastly, downloadPDF will download a pdf file containing a list of users.

To create a controller, run the following command.

Make necessary changes to the controller file. In index method, retrieve the users list from database and pass it the view.

Similarly, we will create downloadPDF method that will download pdf file containing users list from database.

Lastly, we will create a viewPDF method that will display a pdf file in browser without downloading it.

Thus, our overall controller looks like below.

Create View

After all setup, routes and controller, it’s time to create view files. We will create two blade files i.e. index.blade.php for displaying all users list in resources/views/users/ directory and users.blade.php works as a template for pdf file which is stored at resources/views/pdf/ directory.

I have used a separate pdf directory because we can store all the pdf template in this directory that will help us to organize our files easily.

So, our view file for displaying all users looks like below:

Similarly, our view file which works as a pdf template looks like below:

Additional Configuration

You can chain the methods:

You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)

It’s time to test our code.

When we navigate to the route /users, we will get a list of users
Export data to PDF file using laravel-dompdf in Laravel 5
When we click on the View PDF button, we will see a list of users on the browser.
Export data to PDF file using laravel-dompdf in Laravel 5 stream
When we click on the Download PDF button, it will download a pdf file containing a list of users.

If you want to read Laravel CRUD application from scratch, you can read this article.