How to Generate PDF in Laravel 10 using DomPDF

Generating PDFs is a common requirement in web applications. For Laravel Generate Pdf files, you can utilize the popular library called “dompdf.” Dompdf is a HTML to PDF converter that allows you to generate PDF files from HTML views or raw HTML code. Here’s a step-by-step guide on how to generate PDFs in Laravel using dompdf.

Dompdf is an HTML to PDF converter which is convert HTML along with css layout rendering in pdf layout. In this guide, we’ll walk through the process of generating a PDF in Laravel.

1. Install Laravel App

If you haven’t already set up a Laravel project, create a new project using the following command in your terminal:

composer create-project --prefer-dist laravel/laravel generatePDF

2. Install the Dompdf library

Begin by installing the Dompdf library through Composer. Open your terminal and navigate to your Laravel project directory, then run the following command.

composer require barryvdh/laravel-dompdf

After successfully installing the package, now open the config/app.php file and add the service provider and alias.

'providers' => [
	....
	Barryvdh\DomPDF\ServiceProvider::class,
],
  
'aliases' => [
	....
	'PDF' => Barryvdh\DomPDF\Facade::class,
]

3. Make Routes

Define a route in your routes/web.php file that will handle the PDF generation.

Route::get('/generate-pdf', 'PDFController@generate')->name('generate-pdf');

4. Create Controller

Generate a controller that will handle the PDF generation. Run the following command to create the PDFController.

php artisan make:controller PDFController

Open the app/Http/Controllers/PDFController.php and define the generate method. This method will generate the PDF using Dompdf.

<?php
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use PDF;
  
class PDFController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function generate()
    {
        $data = [
            'title' => 'Sample PDF',
            'content' => 'This is the content of the PDF file.'
        ];

        $pdf = PDF::loadView('generate-pdf', $data);

        return $pdf->download('sample.pdf');
      }
}

In this example, we’re defining a generate method that loads a view called generate-pdf.blade.php and passes in some data ($data array). The loadView method creates a PDF from the view, and the download method allows you to download the generated PDF with the given filename.

5. Create Blade File

Create a blade view file named generate-pdf.blade.php inside the resources/views directory. This view will contain the HTML structure and content you want to include in your PDF file.

Next, open the resources/views/generate-pdf.blade.php file and update the following code on it.

<!DOCTYPE html>
<html>
<head>
   <title>Generate Pdf</title>
</head>
<body>
	<h1>Welcome to codingdriver.com - {{ $title }}</h1>
       <p>{!! $content !!}</p>
</body>
</html>

That’s it! You have successfully generated PDFs in Laravel using the Dompdf library. You can customize the view and the data passed to it based on your specific requirements.

1 thought on “How to Generate PDF in Laravel 10 using DomPDF”

Leave a Comment