Minimal example:
Package installation barryvdh/Laravel-dompdf
composer require barryvdh/laravel-dompdf
After the installation is completed, enter the file app/config.php
and add the following settings:
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
]
and
'aliases' => [
...
'PDF' => Barryvdh\DomPDF\Facade::class,
]
return to command line and type:
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
to publish the settings in the folder config
the file dompdf.php
.
Observing: there is a folder in the installation package that should be copied manually to the folder storage
of Larable, this folder contains the source files used by this package to generate the reports in PDF
and the way is
vendor\dompdf\dompdf\lib
and the folder is fonts
, looking like this.
After the setup and configuration process create a controller
for example:
<?php namespace App\Http\Controllers;
use App\Stackoverflow;
use Barryvdh\DomPDF\Facade as PDF;
class PdfviewController extends Controller
{
private $model;
public function __construct(Stackoverflow $model)
{
$this->model = $model;
}
public function index()
{
$data['model'] = $this->model->all();
return PDF::loadView('view', $data)
->stream();
}
}
the route:
Route::get('/viewpdf', 'PdfviewController@index');
and your View with this structure, generating a list of table information stackoverflow
:
<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Raleway', sans-serif;
font-weight: 100;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 12px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<table>
<thead>
<tr>
<th>Id</th>
<th>Descrição</th>
</tr>
</thead>
<tbody>
@foreach($model as $item)
<tr>
<td>{{$item->id}}</td>
<td>{{$item->description}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</body>
</html>
having the exit:
References:
Need to be exactly this PDF generator, it has something I do not like that is system dependent installation? for example this is very simple barryvdh/Laravel-dompdf ...
– novic