Generate PDF from HTML in PHP Laravel using Dompdf Library

Generate PDF from HTML in PHP Laravel using Dompdf Library

In this tutorial, i will tell you how to generate pdf file from HTML view in Laravel PHP by using laravel-dompdf package.

There are lots of packages too that are using to generate pdf and using HTML and CSS, you can easily set the beautiful layout for pdf file.

Using laravel-dompdf library, you can easily generate pdf file in your Laravel web application.

Generating pdf files are helpfull when you are sending report in email from large amount of data then you can convert your html view in single pdf file and then attach with your mail.

Dompdf is a PHP library which is used to generate PDF file from HTML view.

Step 1: Installation

In this step you are going to install laravel-dompdf package to generate pdf file from HTML view blade file. You can use composer to download this plugins so copy this command and run in your terminal promp.

composer require barryvdh/laravel-dompdf

Now after downloading libraries you will have to set their service provider and aliase in following path config/app.php.

  1. 'providers' => [
  2.     ....
  3.     Barryvdh\DomPDF\ServiceProvider::class,
  4. ],

  1. 'aliases' => [
  2.     ....
  3.     'PDF' => Barryvdh\DomPDF\Facade::class,
  4. ],
Step2: Routes.php

I assume that you have successfully download libraries of laravel-dompdf so let's start with route file.

add below line of code in your routes.php which you will get in following path app/Http/routes.php.

  1. Route::get('htmltopdfview',array('as'=>'htmltopdfview','uses'=>'ProductController@htmltopdfview'));
Step3: Create Product Controller

Now i am going to create ProductController and assume that you will have product model and table with some dummy data that will be used to list result.

In htmltopdfview method, i am sharing a global variable which you can use anywhere in your application and use pdf class to load view to generate in pdf file.

  1. namespace App\Http\Controllers;
  2. use App\Http\Requests;
  3. use Illuminate\Http\Request;
  4. use App\Product as Product;
  5. use PDF;
  6. class ProductController extends Controller
  7. {
  8. public function htmltopdfview(Request $request)
  9. {
  10. $products = Products::all();
  11. view()->share('products',$products);
  12. if($request->has('download')){
  13. $pdf = PDF::loadView('htmltopdfview');
  14. return $pdf->download('htmltopdfview');
  15. }
  16. return view('htmltopdfview');
  17. }
  18. }
Step 4: Create htmltopdfview View File

Finally you will have to create a view where you will list down all your products and will generate this view in pdf file too.

  1. <div class="row">
  2.     <a href="{{ route('htmltopdfview',['download'=>'pdf']) }}">Download PDF</a>
  3.     <table>
  4.         <tr>
  5.             <th>Name</th>
  6.             <th>Details</th>
  7.         </tr>
  8.         @foreach ($products as $product)
  9.         <tr>
  10.             <td>{{ $product->name }}</td>
  11.             <td>{{ $product->details }}</td>
  12.         </tr>
  13.         @endforeach
  14.     </table>
  15. </div>

Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort bayan sakarya escort serdivan escort sakarya escort sakarya escort serdivan escort
karasu escort akyazı escort serdivan escort akyazı escort ferizli escort sapanca escort serdivan escort söğütlü escort sakarya escort izmit escort arifiye escort erenler escort hendek escort kocaali escort pamukova escort taraklı escort geyve escort