Hello to all, welcome to therichpost.com. In this post, I will tell you, How to generate PDF from html view file in laravel?
Here are the following steps to generate PDF from html view file in laravel? the rich post:
1. Need to install laravel-dompdf package with below command:
composer require barryvdh/laravel-dompdf
2. After installing laravel-dompdf package you need to set their service provider and alias in following path config/app.php.
'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ],
3. Set Up blade view template for resources/view/htmltopdfview.blade.php:
<div class="row"> <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>
4. Set up the app/Http/Controllers/ProductController.php.
namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use App\Product 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::loadView('htmltopdfview'); return $pdf->download('htmltopdfview'); } return view('htmltopdfview'); } }
5. Set up the laravel routes/web.php.
Route::get('/htmltopdfview','ProductController@htmltopdfview');
Now you are done. If you have any query related to this post, then feel free to ask.
Recent Comments