Search code examples
phplaravelpdf

PDF download wont work in Laravel Project


Im new to laravel and I cant seem to download the pdf of my blade php file. Please help me out! Thank you!

This is what I get If i click my "Export to PDF" button in my productlist.blade.php:

ErrorException Undefined variable: products (View: C:\xampp\htdocs\laravel\productcrud\resources\views\productlist.blade.php)

This is my product controller file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use Barryvdh\DomPDF\Facade as PDF;

class ProductController extends Controller
{
...

public function showProductList(){
    $products = Product::all();
    return view('productlist', compact('products'));
  }

  public function createPDF() {
  $data = Product::all();

  view()->share('productlist',$data);
  $pdf = PDF::loadView('productlist', $data);

  return $pdf->download('pdf_file.pdf');
  }
 }

This is the body of my productlist.blade.php:

<body>
<div class="container mt-4">
<div class="card bg-light border-warning">
<div class="card-header">
    <h2 class="text-center display-4 mb-3">Product List</h2>

    <div class="float-right">
    <a class="btn btn-primary btn-lg" href="{{ URL::to('/productlist/pdf') }}">Export to PDF</a>
        <a class="btn btn-primary m-2 btn-lg" href="{{ route('products.index') }}"> Back</a>
    </div>
</div>

<div class="card-body">
    <table class="table table-bordered table-striped text-center">
        <thead>
            <tr class="thead-dark text-center">
        <th scope="col">No.</th>

        <th scope="col">Name</th>

        <th scope="col">Image</th>

        <th scope="col">Barcode</th>

        <th scope="col">Price</th>

        <th scope="col">Quantity</th>

        <th scope="col">Status</th>

        <th scope="col">Created At</th>

        <th scope="col">Updated At</th>
            </tr>
        </thead>
        <tbody>
            @foreach($products as $product)
            <tr>
            
        <td>{{ $product->id }}</td>
        <td>{{ $product->name }}</td>
        <td><img src ="{{ asset('storage/images/products/'.$product->image) }}" alt ="" width ="80px"></td>
        <td>{{ $product->barcode }}</td>
        <td>{{ $product->price }}</td>
        <td>{{ $product->quantity }}</td>
        <td>{{ $product->status }}</td>
        <td>{{ $product->created_at }}</td>
        <td>{{ $product->updated_at }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>
</div>
</div>
<script src="{{ asset('js/app.js') }}" type="text/js"></script>
</body>

This is my web.php:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\PDFController;

Route::get('/', function () {
return view('welcome');
});

Route::resource('products', ProductController::class);
Route::get('/productlist', 'App\Http\Controllers\ProductController@showProductList');
Route::get('/productlist/pdf','App\Http\Controllers\ProductController@createPDF');

Solution

  • Update your code to use this:

    $pdf = PDF::loadView('productlist', ['products' => $data]);
    

    When passing data to a view, as is what's happening above, you should pass an associative array. Then, for each key in that array, laravel will automatically create a variable in the scope of the view file's code with that name and value.

    For example, in the above, we send: ['products' => $data] and laravel will create a $products variable whose value will be $data and that variable can be used by the view.

    See the related docs here