How to dompdf laravel ?

Spread the love

To use dompdf in a Laravel project for PDF generation, you’ll need to follow a series of steps. Below is a step-by-step guide:

Step 1: Install Dompdf via Composer

Open your terminal and navigate to your Laravel project directory. Then, run the following command to install dompdf:

composer require barryvdh/laravel-dompdf

Step 2: Configure Laravel Dompdf

After installation, you need to configure Laravel to use Dompdf. Open your config/app.php file, and add the service provider and alias:

'providers' => [
    // ...
    Barryvdh\DomPDF\ServiceProvider::class,
],

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

Step 3: Publish the Configuration File

Run the following command to publish the configuration file:

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

This will generate a config/dompdf.php file.

Step 4: Basic Usage in a Controller

Now, you can use Dompdf to generate a PDF in your controller. Below is a simple example:

// app/Http/Controllers/PdfController.php

namespace App\Http\Controllers;

use PDF;

class PdfController extends Controller
{
    public function generatePDF()
    {
        $data = ['name' => 'John Doe', 'email' => 'john@example.com'];

        $pdf = PDF::loadView('pdf.example', $data);

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

Step 5: Create a Blade View for the PDF

Create a Blade view file (e.g., resources/views/pdf/example.blade.php) with the content you want in your PDF:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example PDF</title>
</head>
<body>
    <h1>Hello, {{ $name }}!</h1>
    <p>Email: {{ $email }}</p>
</body>
</html>

Step 6: Define a Route

Open your routes/web.php file and define a route to the generatePDF method in your controller:

// routes/web.php

use App\Http\Controllers\PdfController;

Route::get('/generate-pdf', [PdfController::class, 'generatePDF']);

Step 7: Test

Run your Laravel development server:

php artisan serve

Visit http://127.0.0.1:8000/generate-pdf in your browser, and it should prompt you to download the generated PDF.

That’s it! You’ve successfully integrated Dompdf into your Laravel project for PDF generation. Adjust the code and views as needed for your specific use case.

To use dompdf in a Laravel project for PDF generation, you’ll need to follow a series of steps. Below is a step-by-step guide:

Step 1: Install Dompdf via Composer

Open your terminal and navigate to your Laravel project directory. Then, run the following command to install dompdf:

composer require barryvdh/laravel-dompdf

Step 2: Configure Laravel Dompdf

After installation, you need to configure Laravel to use Dompdf. Open your config/app.php file, and add the service provider and alias:

'providers' => [
    // ...
    Barryvdh\DomPDF\ServiceProvider::class,
],

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

Step 3: Publish the Configuration File

Run the following command to publish the configuration file:

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

This will generate a config/dompdf.php file.

Step 4: Basic Usage in a Controller

Now, you can use Dompdf to generate a PDF in your controller. Below is a simple example:

// app/Http/Controllers/PdfController.php

namespace App\Http\Controllers;

use PDF;

class PdfController extends Controller
{
    public function generatePDF()
    {
        $data = ['name' => 'John Doe', 'email' => 'john@example.com'];

        $pdf = PDF::loadView('pdf.example', $data);

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

Step 5: Create a Blade View for the PDF

Create a Blade view file (e.g., resources/views/pdf/example.blade.php) with the content you want in your PDF:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example PDF</title>
</head>
<body>
    <h1>Hello, {{ $name }}!</h1>
    <p>Email: {{ $email }}</p>
</body>
</html>

Step 6: Define a Route

Open your routes/web.php file and define a route to the generatePDF method in your controller:

// routes/web.php

use App\Http\Controllers\PdfController;

Route::get('/generate-pdf', [PdfController::class, 'generatePDF']);

Step 7: Test

Run your Laravel development server:

php artisan serve

Visit http://127.0.0.1:8000/generate-pdf in your browser, and it should prompt you to download the generated PDF.

That’s it! You’ve successfully integrated Dompdf into your Laravel project for PDF generation. Adjust the code and views as needed for your specific use case.

Leave a Comment

Scroll to Top