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 record to record, not to print all the records, that is why a button is generated in the table that corresponds to a record 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 does its job but ends up with 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 it prints contains this 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>
This archivo de rutas
is generated in it:
Route::get('pdfview', 'PuosController@imprimir');
I have a solution, a bit mixup of one of the fellow answer.
In the file
rutas
I have configured it as follows:And in
Index
my tablePUO
the button looks like this:The function
Imprimir
inPuosController
The first thing you need to do is install DOMPDF :
You add the ServiceProvider
You add the facade
Once installed we will create the controller and the view for the pdf:
Controller:
View for the pdf
Remember that
DOMPDF
it does not get along withbootstrap
, so it is recommended to create the view directly with the css that we need and nothing else, on the other hand, if you are only going to bring a record from the table, you do not need the@foreach
.I removed some columns from the ones you put in your view to simplify the example.
Obviously I assume that you know how to create and use the necessary routes in Laravel.
first change the button to download the pdf. instead of route use url to go to a controller
now your route for example
and in the controller do the following:
My suggestion is that you use the Barryvdh Laravel DomPDF package .
Also, you use the Route Model Binding to directly inject your model into the controller. Laravel already takes care of managing if the model exists and generating the corresponding error codes.
For the binding in the
boot
delRouteServiceProvider
incorporates:And in the routes:
Then in the controller you can directly define the object and with the package
Barryvdh
there are two lines of code: