How to generate PDF from html view file and download using dompdf in laravel?

Today now in this post i will show you How to generate PDF from html view file and download using dompdf in laravel? In normally, when we are working on big ERP level project or any report basis project by using laravel. we need to generate the PDF file for data from the database table. Now in this tutorial i will give you a very simple way about how to create a pdf file. And how to download that pdf in laravel application step by step for beginners.

Today here in this post i will use laravel-dompdf package for the create pdf file and this package will also provide to download function. So in this example i need a one table as “items” . And also i will be download the pdf file of items table data also with the tabular format, we can also write our own css on the view file for pdf, so then we can make this a batter pdf file.

Step 1: Installation

So now in first step we need to download the laravel-dompdf plugin for the generate pdf file from html view blade file. So at first need to run the bellow command in our terminal:

composer require barryvdh/laravel-dompdf

Now need to open config/app.php file and then add the service provider and aliase.

'providers' => [
	....
	Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [
	....
	'PDF' => Barryvdh\DomPDF\Facade::class,

],
Step 2: Add Route

So in this is step here need to add the route for generate the view. And need to open your app/Http/web.php file and then add the following route.

Route::get('pdfview',array('as'=>'pdfview','uses'=>'ItemController@pdfview'));
Step 3: Create Controller

Ok good, now we have to create a new controller as name ItemController in this on path app/Http/Controllers/ItemController.php. And also Make sure we should have the items table with the some data. Now this controller will manage the data and generate the pdf file, so put the bellow content in controller file:

app/Http/Controllers/ItemController.php

namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;
class ItemController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function pdfview(Request $request)
    {
        $items = DB::table("items")->get();
        view()->share('items',$items);
        if($request->has('download')){
            $pdf = PDF::loadView('pdfview');
            return $pdf->download('pdfview.pdf');
        }
        return view('pdfview');
    }
}
Step 4: Create View File

Now in the last step, we need to create the view file as “pdfview.blade.php” for generate the view and also for pdf file, so create the pdfview file and put the bellow code:

resources/view/pdfview.blade.php

<style type="text/css">
	table td, table th{
		border:1px solid black;
	}
</style>
<div class="container">
	<br/>
	<a href="{{ route('pdfview',['download'=>'pdf']) }}">Download PDF</a>
	<table>
		<tr>
			<th>No</th>
			<th>Title</th>
			<th>Description</th>
		</tr>
		@foreach ($items as $key => $item)
		<tr>
			<td>{{ ++$key }}</td>
			<td>{{ $item->title }}</td>
			<td>{{ $item->description }}</td>
		</tr>
		@endforeach
	</table>
</div>

Read Also: How to convert file extension using CloudConvert in laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →