Laravel 8 Generate PDF File | Laravel DomPDF

📔 : Laravel 🔗

इस topic में आप सीखेंगे कि कैसे Laravel 8 में blade template का use करके PDF file को generate करते हैं।

Important

I Hope, आपने Laravel project install कर लिया होगा , अगर नहीं किया तो नीचे दी गयी command run करके project install कर लें।

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

Install barryvdh/laravel-dompdf Package

Laravel 8 में PDF file generate करने के लिए barryvdh/laravel-dompdf package का use करेंगे , तो सबसे पहले package install करते हैं।

composer require barryvdh/laravel-dompdf

Add Providers & Alias

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,
],

Add Routes

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']);

Create Controller

Next , अब हमें एक PDFController create करना है जिसे routes/web.php file में use किया था।

File : App/Http/Controllers/PDFController.php
Copy Fullscreen Close Fullscreen
<?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 कर सकते हैं।

Create View File

अब जो view file हमने PDFController में use की थी , वो create करके data को print करते हैं।

File : resources/views/download_pdf.blade.php
Copy Fullscreen Close Fullscreen

<!DOCTYPE html>
<html>
<head>
<title>{{$title}}</title>
</head>

<body>
<h1>{{ $title }}</h1>
<p>{{ $description }}</p>
<p></p>
</body>
</html>

Run

php artisan serve

hit 127.0.0.1:8000/download-pdf URL

Save PDF File

आप चाहे तो , 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'));

Show Images In 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 करते हैं।

Related Topics :

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

b2eprogrammers