Laravel 5.4 and Dompdf - problems?

Asked

Viewed 752 times

0

I’m trying to generate a PDF from a view, and it’s working, however, when I call the route to call the controller, the upload is for several seconds, even minutes to generate the PDF, and download, not to mention not applying my CSS styles. I’d like a light, to know what I can do.

PS: use another library, only if it is simpler.

View:

<!doctype html>
<html lang="pt-BR">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css">
    <title>Lista de chamada</title>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading text-center">
                        <h4>Lista de chamada</h4>
                    </div>

                    <div class="panel-body text-center">
                        <table class="table table-striped">
                            <thead>
                                <th>#</th>
                                <th>Cartão SUS</th>
                                <th>Data de nascimento</th>
                                <th>Avaliação alterada</th>
                                <th>Peso</th>
                                <th>Altura</th>
                                <th>Assinatura</th>
                            </thead>
                            <tbody>
                                @foreach($pacientes as $paciente)
                                    <tr>
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ $paciente->NumeroCartaoSUS }}</td>
                                        <td>{{ $paciente->DataNascimento }}</td>
                                        <td>{!!
                                                $paciente->AvaliacaoAlterada == 1 ?
                                                    Icon::ok().' Sim' :
                                                    Icon::remove().' Não'
                                            !!}
                                        </td>
                                        <td>{{ $paciente->Peso }}</td>
                                        <td>{{ $paciente->Altura }}</td>
                                        <td>  </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Controller:

<?php

namespace Famerp\Http\Controllers\PDF;

use PDF;
use Famerp\Models\Paciente;
use Famerp\Http\Controllers\Controller;

class PDFController extends Controller
{
    public function pdfListaChamada()
    {
        $pacientes = Paciente::all();
        $pdf = PDF::loadView('pdf.pdf-lista-chamada', array('pacientes' => $pacientes));
        return $pdf->setPaper('a4')->download('lista-chamada.pdf');
    }
}

I followed the code guidelines you have on the GitHub of the project, I do not understand why.

  • Do you have an address for that code? I think CSS and Font have some settings and the generation time is also very high due maybe factors that we can not play

  • Project Github has a correct link? which link is?

  • This is the link to the PDF project: https://github.com/barryvdh/laravel-dompdf The link to my project is: https://github.com/matheuspicioli/famerp

  • Do you have many patients? This link is supported by CSS https://github.com/dompdf/dompdf/wiki/CSSCompatibility

  • Really looking like this, not knowing what happens on your machine seems correct, the code is small and clean, but, will know why the problem, is that there is no way to reproduce, is a local problem maybe.

  • Ta wait a minute, put the table in git then. I’m on hold.

  • figured out the problem

  • And what is it? It’s involving environment itself?

  • The css file, it is not accepting, should ai have a reason. mainly because of the remove fonts <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css"> of your file View and testing

  • Now it’s working, but why doesn’t it recognize my css?

  • Because it is not yours it is of a framework and the same is not compatible with the algorithm of the pdf generator. The sources for example are some as described in the documentation.

  • So to apply a CSS, I need to remove the sources from it, or just use the relative path, instead of the function that Laravel provides me?

  • You have to use the fonts and css compatible with the package, and in it it is explained that... understood? That CSS is no use!

  • It was solved by taking out the function Asset() and calling through the same relative path, this solved the PDF generation time, but the source glyphicon were not possible to load them. Thank you!!

Show 9 more comments

1 answer

-1

A recommendation for PDF with Laravel that I give is to use the libraries "barryvdh/Laravel-Snappy" "h4cc/wkhtmltoimage-amd64" and "h4cc/wkhtmltopdf-amd64" in my opinion is the best way to generate PDF in Laravel.

PS.: So far I haven’t had any problems with CSS compatibility, images etc

Example:

$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

https://github.com/barryvdh/laravel-snappy

  • it is no use to indicate to use certain libraries if you do not provide a minimum example. Edit your answer and add this detail, will help everyone.

Browser other questions tagged

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