In web development, generating PDF files dynamically from HTML content is a common requirement. Whether it’s generating reports, invoices, or any other printable document, having the ability to convert HTML to PDF seamlessly is invaluable. Laravel, a popular PHP framework, offers several packages to achieve this task, and one of the most widely used is DomPDF. In this tutorial, we’ll walk through the process of setting up and using DomPDF in Laravel 8 to generate PDF files from HTML content.
Before we begin, ensure you have the following prerequisites installed on your system:
Firstly, we need to install the DomPDF package into our Laravel project. Open your terminal and navigate to your Laravel project directory. Then, run the following Composer command:
bash
composer require dompdf/dompdf
This command will fetch and install the DomPDF library along with its dependencies into your Laravel project.
Next, let’s define a route that will be responsible for generating our PDF. Open your routes/web.php
file and add the following route definition:
php
use App\Http\Controllers\PDFController;
Route::get('/generate-pdf', [PDFController::class, 'generatePDF'])-
>name('generate.pdf');
Now, let’s create a controller where we’ll define the logic for generating our PDF. Run the following Artisan command in your terminal:
bash
php artisan make:controller PDFController
This command will create a new controller named PDFController
. Open this controller located at app/Http/Controllers/PDFController.php
and add the following method:
php
<?php
namespace App\Http\Controllers;
use Dompdf\Dompdf;
use Illuminate\Http\Request;
class PDFController extends Controller
{
public function generatePDF()
{
// Generate PDF logic here
}
}
In the generatePDF()
method, let’s add the logic to generate our PDF using DomPDF. Here’s how you can do it:
php
public function generatePDF()
{
// HTML content to be converted to PDF
$html = '<h1>Hello, World!</h1>';
// Instantiate Dompdf with our options
$dompdf = new Dompdf();
// Load HTML content
$dompdf->loadHtml($html);
// (Optional) Set paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render HTML as PDF
$dompdf->render();
// Output generated PDF to browser
$dompdf->stream('document.pdf');
}
Now that we’ve set up everything, it’s time to test our route. Start your Laravel development server by running:
bash
php artisan serve
Then, navigate to http://localhost:8000/generate-pdf
in your web browser. You should see a PDF file containing the text “Hello, World!” displayed.
In this tutorial, we’ve learned how to generate PDF files from HTML content in Laravel 8 using the DomPDF library. With this knowledge, you can now dynamically create PDF documents for various purposes within your Laravel applications. DomPDF offers extensive features for customizing PDF output, allowing you to tailor the generated documents to your specific needs. Experiment with different HTML structures and styles to create professional-looking PDFs tailored to your application requirements.