Laravel Generate PDF Example | Download And Generate PDF In Laravel In Hindi

Blogs ❯❯ Laravel

Image could not load

Laravel Generate PDF

कई बार हमें अपन application में invoice , order या report बनाने के लिए PDF generate करने की जरूरत पड़ ही जाती है , इस topic में हम देखेंगे कि Laravel में PDF कैसे generate और download करेंगे।

Laravel में PDF generate करने के लिए third-party libraries का use किया जाता है। Laravel में सबसे popular PDF generation library TCPDF, Dompdf, और mPDF हैं।

हालाँकि इस blog में हम Dompdf का use करके PDF generate और download करेंगे।

Setup Laravel Project

सबसे पहले हम एक pdfexample नाम का application set कर लेते हैं , अगर आपने पहले से install कर रखा है तो इस step को skip कर सकते हैं।

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

Install Laravel DomPDF Package

Laravel में PDF generate और download करने के लिए Laravel की barryvdh/laravel-dompdf package का use किया जाता है।

इस package की help से PDF documents create और download करके user को provide कर सकते हैं।

composer require barryvdh/laravel-dompdf

Laravel DomPDF Package Configuration

Package को configure करने के लिए आपको config/app.php file में providers array में Package का ServiceProvider और aliases array में 'PDF' नाम दे देंगे ताकि जरूरत पढ़ने पर इसी नाम का use करके pdf generate हो जाये।

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

Setup PDF Controller

अब एक PDFController.php setup करेंगे जिसके generatePDF() method में हम pdf को generate करने का logic लिखेंगे।

php artisan make:controller PDFController
Laravel PDF Generate Logic

File : app/Http/Controllers/PDFController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use PDF; class PDFController extends Controller { public function generatePDF() { $data = [ 'title' => 'PDF Example', 'description' => 'This is an example PDF generated from Laravel.', ]; $pdf = PDF::loadView('pdfview', $data); return $pdf->download('pdf_filename.pdf'); } }

Setup View

अगर आप ध्यान से देखेंगे तो पाएंगे कि pdfview.blade.php file use की गयी है , तो चलिए इसे भी setup कर लेते हैं।

file : resources/views/pdfview.blade.php

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>{{ $title }}</title> <style> body { font-family: Arial, sans-serif; } </style> </head> <body> <div class="header"> <div class="title">{{ $title }}</div> <div class="description">{{ $description }}</div> </div> <div> <p>This is the main content of your PDF.</p> <p>You can include any HTML content here.</p> </div> </body> </html>

Add Route

अब आपको एक route create करना है जो आपके controller के generatePDF() method को call करेगा।

File : routes/web.php

Route::get('generate-pdf', 'PDFController@generatePDF');

That's it , अब आप http://localhost/pdfexample/generate-pdf URL पर visit करके PDF generate कर सकते हैं। PDF browser में दिखाई देगी और download भी हो सकती है।

Laravel Save PDF File

आप चाहे तो pdf file को अपने application में कही save भी कर सकते हैं।

जैसे नीचे दिए गए example में file को storage/app/public folder में save किया गया है।

public function generatePDF() { $data = [ 'title' => 'PDF Example', 'description' => 'This is an example PDF generated from Laravel.', ]; $pdf = PDF::loadView('pdfview', $data); // Define the file path where you want to save the PDF. $pdfFilePath = storage_path('app/public/pdf_filename.pdf'); // Save the PDF to the specified path. $pdf->save($pdfFilePath); return 'PDF saved successfully at : ' . $pdfFilePath; }

I Hope, आपको समझ आ गया होगा कि laravel में कैसे PDF download या save करते हैं :)

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook