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.
इस topic में आप सीखेंगे कि कैसे Laravel 8 में blade template का use करके PDF file को generate करते हैं।
I Hope, आपने Laravel project install कर लिया होगा , अगर नहीं किया तो नीचे दी गयी command run करके project install कर लें।
composer create-project --prefer-dist laravel/laravel projectName
Laravel 8 में PDF file generate करने के लिए barryvdh/laravel-dompdf package का use करेंगे , तो सबसे पहले package install करते हैं।
composer require barryvdh/laravel-dompdf
Next , अब हमें providers और alias add करने होंगे जिससे हम , short name के साथ package को access कर सकें। इसके लिए config/app.php file open करने और providers और alias Array में नीचे दी गयी lines add कर दें।
File : config/app.php 'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ],
File : routes/web.php <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PDFController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('download-pdf', [PDFController::class, 'download_pdf']);
Next , अब हमें एक PDFController create करना है जिसे routes/web.php file में use किया था।
<?php
namespace App\Http\Controllers;
use PDF;
class PDFController extends Controller
{
/**
* Download PDF
*
* @return \Illuminate\Http\Response
*/
public function generatePDF()
{
$data = [
"title" => "learnhindituts.com Generate PDF File",
"description" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
];
// make a file download_pdf.blade.php in views folder.
$pdf = PDF::loadView('download_pdf', $data);
return $pdf->download('learnhindituts_pdf.pdf');
}
Note : यहां पर dummy data Array use किया है हालाँकि आप चाहें तो , आप किसी Model का use करके Database से भी data return कर सकते हैं।
अब जो view file हमने PDFController में use की थी , वो create करके data को print करते हैं।
<!DOCTYPE html>
<html>
<head>
<title>{{$title}}</title>
</head>
<body>
<h1>{{ $title }}</h1>
<p>{{ $description }}</p>
<p></p>
</body>
</html>
php artisan serve
hit 127.0.0.1:8000/download-pdf URL
आप चाहे तो , PDF File को download न करके , save भी कर सकते हैं। इसके लिए आपको save(path) method का use किया जाता है।
// savea PDF file in public folder. $pdf = PDF::loadView('download_pdf', $data); $pdf->save(public_path('learnhindituts_pdf.pdf'));
ध्यान रहे , by default PDF File में images के Urls disable होते हैं , images show करने के लिए options set करने पड़ेंगे।
$pdf = PDF::loadView('download_result_pdf', $data)->setOptions(['defaultFont' => 'sans-serif', 'isRemoteEnabled' => true]); return $pdf->download(pdf_name);
I Hope, आपके लिए ये topic काफी help full रहा होगा , और आपको यह समझ आया होगा कि laravel 8 में PDF File कैसे generate / save करते हैं।