PDF Generator with Laravel Framework?

4

I need to make snappy generate reports from BD data. But for this I needed examples of using snappy , in the documentation of github you even have an example, but that example did not help me. I needed an example of how to use Snappy with controller , views and rotas . Can someone help me, please?

@@ Edit

It does not have to be exactly that, it's what I saw in Laracasts which was the best PDF generator. I'll take a look at the dompdf documentation. Anyway, if I use the dompdf would you have a sample code like I said above? Thank you in advance

    
asked by anonymous 06.03.2017 / 19:04

1 answer

6

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,createafontsforexample:

<?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','[email protected]');

anditsViewwiththisstructure,generatingalistofinformationfromthecontrollertable:

<!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:

  

06.03.2017 / 20:58