PDF Generator with Laravel Framework?

Asked

Viewed 7,792 times

4

I need to do the snappy generate reports from BD. But for that I needed examples of using the snappy, in the documentation of github there’s an example, but that example didn’t help me. I needed an example of how to use Snappy with controller, views and rotas. Can someone help me, please?

@@Edit

It doesn’t have to be exactly that, it’s just that I saw in Laracasts that was the best generator of PDF. I’ll take a look at the documentation of dompdf. Anyway, if I use the dompdf would you have an example code like I said above? Thank you already

  • 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 ...

1 answer

7


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 , 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.

inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

References:

  • 1

    hi, I’m trying to implement this answer that Voce posted here. tells me one thing: App\Stackoverflow; can be any model I have?

  • 1

    another question: what should be the name of the.Blade view?

  • 2

    @Italorodrigo can be any model, and can be in any namespace, just really need to be the data that will generate your screen.

  • 1

    @Italorodrigo the name is of your preference, any name, tells me what your difficulty in setting this example, is giving some error?

  • 1

    hi, I took the test now and generated the pdf yes. the issue is that he did not report on the screen and I did not notice. hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.