Minimum example:
Package installation barryvdh / laravel-dompdf
composer require barryvdh/laravel-dompdf
After the installation is complete, enter the app/config.php
file and add the following settings:
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
]
and
'aliases' => [
...
'PDF' => Barryvdh\DomPDF\Facade::class,
]
go back to the command line and type:
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
to publish the settings in the config
folder to the dompdf.php
file.
There is a folder in the installation package that should be copied manually to the
storage
folder of
, this folder contains the font files used by this package for generating reports in
PDF
and the path is vendor\dompdf\dompdf\lib , which looks like this.
Aftertheinstallationandconfigurationprocess,createafonts
forexample:
<?phpnamespaceApp\Http\Controllers;useApp\Stackoverflow;useBarryvdh\DomPDF\FacadeasPDF;classPdfviewControllerextendsController{private$model;publicfunction__construct(Stackoverflow$model){$this->model=$model;}publicfunctionindex(){$data['model']=$this->model->all();returnPDF::loadView('view',$data)->stream();}}
theroute:
Route::get('/viewpdf','PdfviewController@index');
anditsViewwiththisstructure,generatingalistofinformationfromthecontroller
table:
<!DOCTYPEhtml><htmllang="{{ 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 output: