Laravel MySQL Best Pratice : Laravel Tips And Tricks
Features of Laravel 9 : What is new in Laravel 9
Laravel echo, socket io and redis with example : Laravel socket io with redis
Best Branded Mouse Under 500 In India Available On Amazon
JavaScript eval() Function In Hindi | JS eval() Function
PHP var_export() Function In Hindi | var_export() In PHP In Hindi
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 करेंगे।
सबसे पहले हम एक pdfexample
नाम का application set कर लेते हैं , अगर आपने पहले से install कर रखा है तो इस step को skip कर सकते हैं।
composer create-project --prefer-dist laravel/laravel pdfexample
Laravel में PDF generate और download करने के लिए Laravel की barryvdh/laravel-dompdf
package का use किया जाता है।
इस package की help से PDF documents create और download करके user को provide कर सकते हैं।
composer require barryvdh/laravel-dompdf
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,
],
●●●
अब एक PDFController.php
setup करेंगे जिसके generatePDF()
method में हम pdf को generate करने का logic लिखेंगे।
php artisan make:controller PDFController
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');
}
}
●●●
अगर आप ध्यान से देखेंगे तो पाएंगे कि 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>
अब आपको एक 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 भी हो सकती है।
●●●
आप चाहे तो 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 करते हैं :)
●●●
Loading ...
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