Saturday, July 11, 2020
therichposts@gmail.com
Laravel

How to generate PDF from html view file in laravel? the rich post

Laravel 7.2 routing with route group auth guard check with prefix

generate PDF from html view file in laravel

Views

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.

 

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, Angular 8, Angular 9, Angular 10 Nodejs, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.