How to convert HTML...
 
Notifications
Clear all

[Solved] How to convert HTML to PDF in Laravel?

1 Posts
1 Users
0 Reactions
30 Views
Posts: 3
Admin
Topic starter
(@imukeshkaushal)
Member
Joined: 7 months ago

This is very common problem that are facing by the developers when they want to convert html view to the pdf. For this reason this topic is very interesting for you. You have to follow some steps.

Step 1. Install barryvdh/laravel-dompdf Package in your laravel application. So run the following command which is given below.

composer require barryvdh/laravel-dompdf

 

Step 2 . Create a Controller or PdfController like something.

namespace App\Http\Controllers;

use Barryvdh\DomPDF\Facade\Pdf;

class PdfController extends Controller
{
    public function generatePdf()
    {
        $data = ['title' => 'Laravel PDF Example', 'date' => now()];
        
        // Render the PDF using a view
        $pdf = Pdf::loadView('pdf.example', $data);
        
        // Download the PDF
        return $pdf->download('example.pdf');
    }
}

 

Step 3. Create the Blade View (resources/views/pdf/example.blade.php)

<!DOCTYPE html>
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>Generated on: {{ $date }}</p>
</body>
</html>

 

Step 4. Add this route to routes/web.php

use App\Http\Controllers\PdfController;

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

 

Yes, Finished. Congratulation now visit http://your-app-url/generate-pdf to download the PDF. This setup allows you to convert HTML file to PDF quickly and efficiently!

Thanks. If you help this post then please like and share with others.