Laravel Take PDF of a record not whole table

2

I have a table that contains the records in the following way and I want the data it contains to be printed in a PDF from registration to registration, not to print all the records, that is why a button is generated in the table corresponding to a registration separately.

            @foreach($puos as $puo)
            <tr>
                <td>{{ $puo->id }}</td>
                <td>{{ $puo->puo }}</td>
                <td>{{ $puo->compania->nombre }}</td>
                <td>{{ $puo->proyecto->nombre }}</td>
                <td>{{ $puo->partida }}</td>
                <td>{{ $puo->proveedor->nombre }}</td>
                <td>{{ $puo->forma_pago }}</td>
                <td>{{ $puo->usuario->nombre }}</td>
                <td>{{ $puo->usuario1->nombre }}</td>
                <td>{{ $puo->producto->nombre }}</td>
                <td>{{ $puo->cantidad }}</td>
                <td>{{ $puo->precio_unitario }}</td>
                <td>{{ $puo->descuento_porcentaje }}</td>
                <td>{{ $puo->iva }}</td>
                <!--<td>{{ $puo->precio_total }}</td>-->
                <td>{{ $puo->descuento }}</td>
                <td>{{ $puo->preciounitario_descuento }}</td>
                <td>{{ $puo->iva_total }}</td>
                <td>{{ $puo->total }}</td>                                                            
                <td>
                    <a href="{{ route('puos.edit', $puo->id) }}" class="btn btn-warning"> <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span></a>
                    <a href="{{ route('puos.destroy', $puo->id) }}" class="btn btn-danger"><span class="glyphicon glyphicon-remove-circle" aria-hidden="true" onclick="return confirm('¿Seguro que quieres borrarlo')"></span></a>
                    <a href="{{ route('pdfview' ,     $puo->id) }}"><button class="btn btn-c">descargar pdf</button></a>
                </td>
            </tr>
        @endforeach

The third button calls a function to print the PDF, which performs its function but stays on a blank page, https://imgur.com/a/CWJCM

In the controller I have this:

public function imprimir($id){
    $puo= Puo::find($id);
    $view =  \View::make('pdfview', compact('puo', $puo))->render();
    $pdf = \App::make('dompdf.wrapper');
    $pdf->loadHTML($view);

    return $pdf->download("pdfview.pdf");
 } 

And what you print contains these data:

<div class="row">
        <div class="col-xs-12">
          <div class="box box-info">
            <div class="box-header">
              <div class="box-tools">

              </div>
            </div><!-- /.box-header -->
            <div class="box-body table-responsive no-padding">
              <table class="table table-hover">

                @foreach($puos as $puo)
                <tr>
                  <td>{{ $puo->id }}</td>
                  <td>{{ $puo->puo }}</td>
                  <td>{{ $puo->compania->nombre }}</td>
                  <td>{{ $puo->proyecto->nombre }}</td>
                  <td>{{ $puo->partida }}</td>
                  <td>{{ $puo->proveedor->nombre }}</td>
                  <td>{{ $puo->forma_pago }}</td>
                  <td>{{ $puo->usuario->nombre }}</td>
                  <td>{{ $puo->usuario1->nombre }}</td>
                  <td>{{ $puo->producto->nombre }}</td>
                  <td>{{ $puo->cantidad }}</td>
                  <td>{{ $puo->precio_unitario }}</td>
                  <td>{{ $puo->descuento_porcentaje }}</td>
                  <td>{{ $puo->iva }}</td>
                  <td>{{ $puo->descuento }}</td>
                  <td>{{ $puo->preciounitario_descuento }}</td>
                  <td>{{ $puo->iva_total }}</td>
                  <td>{{ $puo->total }}</td>
                </tr>
               @endforeach
              </tbody></table>
            </div><!-- /.box-body -->
          </div><!-- /.box -->
        </div>
 </div>

In the archivo de rutas this is generated:

Route::get('pdfview', 'PuosController@imprimir');
    
asked by Alberto Cepero de Andrés 21.09.2017 в 12:44
source

4 answers

1

The first thing to do is install DOMPDF :

composer require barryvdh/laravel-dompdf

You add the ServiceProvider

Barryvdh\DomPDF\ServiceProvider::class,

You add the facade

'PDF' => Barryvdh\DomPDF\Facade::class,

Once installed, we will create the driver and the view for the pdf:

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PuosController extends Controller
{
    public function imprimir(Request $id){

        // recuperamos el registro, usamos el namespace (\App\) si no añadiste el modelo Puo en los use
        $puo = \App\Puo::find($id);

        // creamos y almacenamos la vista
        $vista = view('nombre_vista')
                ->with('puo', $puo);

        // Generamos el pdf pasandole la vista
        $pdf =  \PDF::loadHTML($vista);

        // retornamos la salida del pdf
        return $pdf->stream('nombre.pdf');
    }
}

View for pdf

<table  style="width: 100%; border: 1px solid #ccc;">
  <tbody>
    <tr>
    <tr>
      <td>ID</td>
      <td>{{ $puo->id }}</td>
    </tr>
    <tr>
      <td>PUO</td>
      <td>{{ $puo->puo }}</td>
    </tr>
    <tr>
      <td>Nombre</td>
      <td>{{ $puo->compania->nombre }}</td>
    </tr>
    <tr>
      <td>Nombre proyecto</td>
      <td>{{ $puo->proyecto->nombre }}</td>
    </tr>
    <tr>
      <td>Partida</td>
      <td>{{ $puo->partida }}</td>
    </tr>
    <tr>
      <td>Usuario</td>
      <td>{{ $puo->usuario->nombre }}</td>
    </tr>
    <tr>
      <td>Producto</td>
      <td>{{ $puo->producto->nombre }}</td>
    </tr>
    <tr>
      <td>Cantidad</td>
      <td>{{ $puo->cantidad }}</td>
    </tr>
    <tr>
      <td>IVA</td>
      <td>{{ $puo->iva }}</td>
    </tr>
    <tr>
      <td>Descuento</td>
      <td>{{ $puo->descuento }}</td>
    </tr>
  </tbody>
</table>

Remember that DOMPDF does not get along with bootstrap , so it is advisable to create the view directly with the css we need and nothing else, on the other hand if you are only going to bring A record of the table does not need the @foreach .

I deleted some of the columns that you put in your view to simplify the example.

Obviously I assume you know how to create and use the necessary routes in Laravel.

    
answered by 29.09.2017 / 10:26
source
0

My suggestion is that you use the package of Barryvdh Laravel DomPDF .

Also use the Route Model Binding to directly inject your model into the controller. Laravel is already responsible for managing if the model exists and generating the corresponding error codes.

For binding in boot of RouteServiceProvider includes:

Route::model('pou', App\Pou::class);

And on the routes:

Route::get('pdfview/{pou}', 'PuosController@imprimir');

Then in the controller you will be able to define the object directly and with the package of Barryvdh are two lines of code:

public function imprimir(Pou $pou)
{
    $pdf = PDF::loadView('pdfview', ['pou' => $pou]);
    return $pdf->download('pdfview.pdf');
}
    
answered by 25.09.2017 в 13:44
0

first change the button to download the pdf. instead of route use url to go to a controller

<a href="{{url('pdfview' , $puo->id)}}"><button class="btn btn-c">descargar pdf</button></a>

now your route for example

 Route::get('pdfview', 'ItemController@imprimir');

and in the controller do the following:

public function imprimir($id){


    $pou= Pou::find($id);



    $view =  \View::make('//aqui ingresa la direccion donde esta los datos a imprimir', compact('pou', $puo))->render();
    $pdf = \App::make('dompdf.wrapper');
    $pdf->loadHTML($view);
    return $pdf->download("Data de pou");



     }
    
answered by 23.09.2017 в 10:34
0

I have a solution, a bit of a mix of one of the answers of the companions.

In the rutas file, I have configured it in the following way:

Route::get('puos/{id}/pdfview', [
'uses' => 'PuosController@imprimir',
'as'   => 'puos.pdfview'
]); 
Route::get('pdfview',array('as'=>'pdfview','uses'=>'ItemController@pdfview'));
Route::get('imprimirPdf',array('as'=>'imprimirPdf','uses'=>'ItemController@imprimirPdf'));

And in the Index of my table PUO the button stayed like this:

<a href="{{ route('puos.pdfview' ,     $puo->id) }}"><button class="btn btn-c">descargar pdf</button></a>

The function% co_of% in% co_of%

    public function imprimir($id){
    $puo= Puo::find($id);
    $view =  \View::make('pdfview', compact('puo', $puo))->render();
    $pdf = \App::make('dompdf.wrapper');
    $pdf->loadHTML($view);

    return $pdf->download("pdfview.pdf");
 }  
    
answered by 26.09.2017 в 09:51