Generating PDF documents in Laravel can be a daunting task, especially for beginners. However, with the right tools and resources, it can be a straightforward process. One such tool is DOMPDF, a PHP library that enables developers to create and convert HTML documents to PDF format.
In this article, we will walk you through the steps of generating a PDF document in Laravel using DOMPDF. We will also show you how to customize the view and add headings to your PDF document.
Step 1: Install and Configure DOMPDF
To begin, you need to install DOMPDF on your Laravel project. You can do this by adding the DOMPDF library to your composer.json file and running the following command in your terminal:
composer update
After installing the library, you need to configure it to work with Laravel. In your config/app.php file, add the following line to the providers array:
'providers' => [ ... Barryvdh\DomPDF\ServiceProvider::class,],
Also, add the following line to the aliases array:
'aliases' => [ ... 'PDF' => Barryvdh\DomPDF\Facade::class,],
Read Also : How to generate and download pdf using Laravel Snappy
Step 2: Create a Custom View for the PDF Document
Next, you need to create a custom view for your PDF document. In this view, you can include the content you want to display on the PDF document, such as text, images, and tables.
To create a custom view, navigate to your resources/views directory and create a new file called pdf.blade.php
. This file will contain the HTML code that will be converted to a PDF document.
For the purpose of this tutorial, let’s create a simple view that displays a table of users. Add the following code to your pdf.blade.php
file:
<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>User List</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; border-bottom: 1px solid #ddd; } th { background-color: #f2f2f2; } </style> </head> <body> <h1>User List</h1> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <td>{{ $user->id }}</td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> </tr> @endforeach </tbody> </table> </body></html>
This view contains a table of users with their ID, name, and email. Note that we have included some basic styling for the table using CSS.
Step 3: Create a Route to Generate the PDF Document
Now that we have created a custom view for our PDF document, we need to create a route that will generate the PDF document and allow the user to download it.
In your routes/web.php
file, add the following code:
Route::get('/generate-pdf', function () { $users = \App\Models\User::all(); $pdf = PDF::loadView('pdf', compact('users')); return $pdf->download('users.pdf');});
This code defines
a route called /generate-pdf
that loads all users from the database and passes them to the pdf.blade.php
view using the loadView()
method provided by DOMPDF.
We then call the download()
method on the $pdf
object to generate the PDF document and allow the user to download it. The download()
method takes a filename as an argument, which will be the name of the PDF document that the user downloads.
Step 4: Add Headings to the PDF Document
To add headings to our PDF document, we can simply modify the pdf.blade.php
view that we created earlier. Let’s add a heading to the top of the document, as well as a subheading above the table of users.
<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>User List</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; border-bottom: 1px solid #ddd; } th { background-color: #f2f2f2; } .heading { font-size: 24px; font-weight: bold; margin-bottom: 20px; } .subheading { font-size: 18px; font-weight: bold; margin-bottom: 10px; } </style> </head> <body> <div class="heading">User List</div> <div class="subheading">List of all registered users</div> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <td>{{ $user->id }}</td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> </tr> @endforeach </tbody> </table> </body></html>
In this modified view, we have added a div
with the class of heading
to display the main heading, and a div
with the class of subheading
to display the subheading above the table of users.
We have also added some basic styling to these headings using CSS.
Step 5: Test the PDF Generation
Finally, we can test our PDF generation by navigating to the /generate-pdf
route in our browser. This will generate a PDF document containing the table of users, as well as the headings that we added earlier.
Once the PDF document has been generated, it will be automatically downloaded to the user’s computer with the filename users.pdf
, which was specified in the download()
method.
Conclusion
Generating PDF documents in Laravel can be a useful feature for any web application. With DOMPDF, it is relatively easy to create and customize PDF documents using Laravel’s built-in Blade templating engine.
In this article, we walked you through the steps of generating a PDF document in Laravel using DOMPDF. We also showed you how to customize the view and add headings to your PDF document.
By following these steps, you should be able to generate your own PDF documents in Laravel with ease.
Comments
Post a Comment