Categories

Friday, November 22, 2024
#919814419350 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

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.

 

therichpost
the authortherichpost
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 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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