onlinecode

Generate PDF from HTML in PHP Laravel using Dompdf Library

Generate PDF from HTML in PHP Laravel using Dompdf Library

In this post we will give you information about Generate PDF from HTML in PHP Laravel using Dompdf Library. Hear we will give you detail about Generate PDF from HTML in PHP Laravel using Dompdf LibraryAnd how to use it also give you demo for it if it is necessary.

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.     BarryvdhDomPDFServiceProvider::class,
  4. ],
'providers' => [
	....
	BarryvdhDomPDFServiceProvider::class,
],
  1. 'aliases'=>[
  2.     ....
  3.     'PDF'=> BarryvdhDomPDFFacade::class,
  4. ],
'aliases' => [
	....
	'PDF' => BarryvdhDomPDFFacade::class,
],

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

app/Http/Controllers/ProductController.php

  1. namespace AppHttpControllers;
  2. use AppHttpRequests;
  3. use IlluminateHttpRequest;
  4. use AppProduct as Product;
  5. use PDF;
  6. class ProductController extends Controller
  7. {
  8. public functionhtmltopdfview(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. returnview('htmltopdfview');
  17. }
  18. }
namespace AppHttpControllers;

use AppHttpRequests;
use IlluminateHttpRequest;
use AppProduct as Product;
use PDF;

class ProductController extends Controller
{

    public function htmltopdfview(Request $request)
    {
        $products = Products::all();
        view()->share('products',$products);

        if($request->has('download-pdf')){
            $pdf = PDF::loadView('htmltopdfview');
            return $pdf->download('htmltopdfview');
        }

        return view('htmltopdfview');
    }
}

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.

resources/view/htmltopdfview.blade.php

  1. <divclass="row">
  2.     <ahref="{{ 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>
<div >

	<a href="{{ route('htmltopdfview',['download'=>'pdf']) }}">Download PDF</a>

	<table>
		<tr>
			<th>Name</th>
			<th>Details</th>
		</tr>
		@foreach ($products as $product)
		<tr>
			<td>{{ $product->name }}</td>
			<td>{{ $product->details }}</td>
		</tr>
		@endforeach
	</table>
</div>

One more thing, this is the demo only you can use this code as you want to generate pdf.

Label :

PHP

Laravel PHP Framework

File

How To

API

Hope this code and post will helped you for implement Generate PDF from HTML in PHP Laravel using Dompdf Library. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version