0 votes

How to generate PDF with laravel 5.4 dompdf data?

I need to generate a file .pdf with dompdf but I'm not returning anything when I press the button.

My PDFController

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Product;

use PDF;


class PDFController extends Controller

{

public function pdf(Request $request){
    $products=Product::all();
    view()->share('products', $products);
    if ($request->has('descargar')) {
        $pdf = PDF::loadView('cart.index');
        return $pdf->download('cotizacion');
    }
    return view('cart.index');
}

}

View that I wish to generate a PDF, at the end I call the route at the last button, index.blade.php

    <table class="table table-hover">
        <thead>
        <tr>
            <th>Name</th>
            <th>Price</th>
            <th>qty</th>
            <th>size</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        @foreach(Cart::content() as $cartItem)
            <tr>
                <td>{{$cartItem->name}}</td>
                <td>{{$cartItem->price}}</td>
                <td width="50px">
                    {!! Form::open(['route' => ['cart.update',$cartItem->rowId], 'method' => 'PUT']) !!}
                    <input name="qty" type="text" value="{{$cartItem->qty}}">


                </td>
                <td>
                    <div > {!! Form::select('size', ['small'=>'Small','medium'=>'Medium','large'=>'Large'] , $cartItem->options->has('size')?$cartItem->options->size:'' ) !!}
                       </div>

                </td>

                <td>
                    <input style="float: left"  type="submit" class="button success small" value="Ok">
                    {!! Form::close() !!}

                    <form action="{{route('cart.destroy',$cartItem->rowId)}}"  method="POST">
                       {{csrf_field()}}
                       {{method_field('DELETE')}}
                       <input class="button small alert" type="submit" value="Delete">
                     </form>
                </td>
            </tr>
        @endforeach

        <tr>
            <td></td>
            <td>
                Tax: ${{Cart::tax()}} <br>
                Sub Total: $ {{Cart::subtotal()}} <br>
                Grand Total: $ {{Cart::total()}}
            </td>
            <td>Items: {{Cart::count()}}

            </td>
            <td></td>
            <td></td>

        </tr>
        </tbody>
    </table>

    <a href="{{route('checkout.shipping')}}" class="button">Checkout</a>
    <a href="{{route('pdf')}}" class="button small alert">Import to PDF</a>
</div>



@extends('layouts.main')

@section('content')
    <div class="row">
        <h3>Cart Items</h3>


        <table class="table table-hover">
            <thead>
            <tr>
                <th>Name</th>
                <th>Price</th>
                <th>qty</th>
                <th>size</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            @foreach(Cart::content() as $cartItem)
                <tr>
                    <td>{{$cartItem->name}}</td>
                    <td>{{$cartItem->price}}</td>
                    <td width="50px">
                        {!! Form::open(['route' => ['cart.update',$cartItem->rowId], 'method' => 'PUT']) !!}
                        <input name="qty" type="text" value="{{$cartItem->qty}}">


                    </td>
                    <td>
                        <div > {!! Form::select('size', ['small'=>'Small','medium'=>'Medium','large'=>'Large'] , $cartItem->options->has('size')?$cartItem->options->size:'' ) !!}
                           </div>

                    </td>

                    <td>
                        <input style="float: left"  type="submit" class="button success small" value="Ok">
                        {!! Form::close() !!}

                        <form action="{{route('cart.destroy',$cartItem->rowId)}}"  method="POST">
                           {{csrf_field()}}
                           {{method_field('DELETE')}}
                           <input class="button small alert" type="submit" value="Delete">
                         </form>
                    </td>
                </tr>
            @endforeach

            <tr>
                <td></td>
                <td>
                    Tax: ${{Cart::tax()}} <br>
                    Sub Total: $ {{Cart::subtotal()}} <br>
                    Grand Total: $ {{Cart::total()}}
                </td>
                <td>Items: {{Cart::count()}}

                </td>
                <td></td>
                <td></td>

            </tr>
            </tbody>
        </table>

        <a href="{{route('checkout.shipping')}}" class="button">Checkout</a>
        <a href="{{route('pdf')}}" class="button small alert">Import to PDF</a>
    </div>

@endsection

My route, web.php

Route::get('/pdf', 'PDFController@pdf')->name('pdf');

0voto

This problem what I had, But it is not sure that is your case.

It is and it happens that this library needs some modules of PHP-specific. So that an instance of Apache with PHP by default does not bring them, you Have to install or enable it.

For this reason does not appear in your Localhost.

The solution that I found for my problem (remember that it may not be your case), was to install these modules. Or in my case as had a shared hosting, in cpanel you enable the module dom

Habilitación de dom

I hope that you help. With respect to the installation of the DOM in your local, I did not install or do not install it well. Why don't you document on these steps.

I hope you help him and us accounts as I was.

PD: if you were able to install the DOM on your localhost, compartenos the steps to steps that you did for another may do so.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X