How to Create PDF File in Laravel 11


Creating a PDF file in Laravel 11 is straightforward with the help of the `dompdf` library, which allows you to convert HTML to PDF. Here's a step-by-step guide on how to create a PDF file in Laravel 11.


### Step 1: Install `dompdf` Package


First, you need to install the `barryvdh/laravel-dompdf` package, which is a wrapper around the `dompdf` library.


Run the following command in your terminal:


```bash

composer require barryvdh/laravel-dompdf

```


### Step 2: Configure the Package (Optional)


Laravel automatically detects the package, so no manual registration is required. However, if you want to customize the configuration, you can publish the package’s configuration file:


```bash

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

```


This will create a `config/dompdf.php` file where you can adjust the settings as needed.


### Step 3: Create a Controller for Generating PDFs


Next, create a controller that will handle the PDF generation:


```bash

php artisan make:controller PDFController

```


In `app/Http/Controllers/PDFController.php`, add the following code to generate the PDF:


```php

namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Barryvdh\DomPDF\Facade\Pdf;


class PDFController extends Controller

{

    public function generatePDF()

    {

        $data = [

            'title' => 'Laravel 11 PDF Example',

            'date' => date('m/d/Y'),

            'content' => 'This is a sample PDF generated using Laravel 11.'

        ];


        $pdf = Pdf::loadView('pdf.myPDF', $data);


        return $pdf->download('laravel_pdf_example.pdf');

    }

}

```


### Step 4: Create a View for the PDF


Create a Blade view that will be converted into a PDF. This view will contain the HTML structure of the PDF:


Create a new file at `resources/views/pdf/myPDF.blade.php`:


```html

<!DOCTYPE html>

<html>

<head>

    <title>{{ $title }}</title>

    <style>

        body {

            font-family: DejaVu Sans;

        }

        .title {

            text-align: center;

            font-size: 24px;

            margin-bottom: 20px;

        }

        .content {

            font-size: 14px;

            line-height: 1.5;

        }

        .date {

            text-align: right;

            font-size: 12px;

            margin-top: 20px;

        }

    </style>

</head>

<body>

    <div class="title">{{ $title }}</div>

    <div class="content">

        {{ $content }}

    </div>

    <div class="date">{{ $date }}</div>

</body>

</html>

```


### Step 5: Define the Route


Now, you need to create a route to access the PDF generation function. In `routes/web.php`, add the following route:


```php

use App\Http\Controllers\PDFController;


Route::get('/generate-pdf', [PDFController::class, 'generatePDF'])->name('generate.pdf');

```


### Step 6: Generate the PDF


Start the Laravel development server:


```bash

php artisan serve

```


Now, navigate to `http://localhost:8000/generate-pdf` in your browser. This will trigger the `generatePDF` method, generate the PDF file, and prompt you to download it.


### Step 7: Customize and Extend


You can customize the HTML template and style the PDF as needed. You can also pass dynamic data from your database or other sources to create more complex PDFs.


### Conclusion


By following these steps, you can easily create and download PDF files in Laravel 11 using the `dompdf` package. This approach is highly customizable and can be extended to generate various types of PDFs based on your application's needs.

Post a Comment

0 Comments