DomPDF API for PDF generation in PHP

2

I use the software laragon that already comes with laravel and composer integrated to the project, I executed the line:

Note: I did not bother with good practice yet, I want to make it work first

composer require barryvdh/laravel-dompdf

To install the PDF generation API. I added the following lines in the file of the / config app.php folder

On providers

Barryvdh\DomPDF\ServiceProvider::class, 

In aliases

'PDF' => Barryvdh\DomPDF\Facade::class,

I imported into the class where the pdf generation method is

use PDF;

In the code I already used in several ways and none worked:

    $clientes = Cliente::all();
    $view = view('Pdf.PdfClientesReport2', compact('clientes'));
    $pdf = \App::make('dompdf.wrapper');      
    $pdf->loadHTML($view);
    //Aqui ele conseguir ver pelo dd() que ele pega dados do pdf mas nao 
    consigo imprimir
    //dd($pdf);   
    $pdf->download('clientes');

In this way also with a function within the generation method:

function geraTeste($clientes){

        $code = "<!DOCTYPE html>
            <html>
            <head>

            <title>Document</title>
            </head>
            <body>
            <table>
                <tr>
                    <td>nome</td>
                    <td>CPF/CNPJ</td>
                    <td>IE</td>
                </tr>";

        foreach ($clientes as $c){
            $code += "<tr>";
            $code += "<td>'.$c->name.'</td>";
            $code += "<td>'.$c->cpf_cnpj.'</td>";
            $code += "<td>'.$c->inscricao_est.'</td>";
            $code += "</tr>";

        }
        $code += "</table>";
        $code += "</body>
        </html>";

        return $code;
    }

    $clientes = Cliente::all();
    $pdf = \App::make('dompdf.wrapper');
    $pdf->loadHTML(geraTeste($clientes));
    return $pdf->stream();

In this way too:

    $pdf = PDF::loadView('Pdf.PdfClientesReport2', compact('clientes'));
    return $pdf->download('invoice.pdf');

That too:

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

I got this way now, but I wanted to be able to open it on my own browser.

$pdf  =  \App::make('dompdf.wrapper');
    $view =  View::make('Pdf.PdfClientesReport2', compact('clientes'))->render();
    $pdf->loadHTML($view);
    $pdf->stream();
    //return $pdf->stream('invoice');
    return $pdf->download('profile.pdf');

If someone can help, I accept your questions ..

    
asked by anonymous 10.09.2017 / 14:14

2 answers

0

You can use a PDF viewer in Javascript to receive streaming, I suggest Mozilla PDF.js:

link

    
18.09.2017 / 20:06
0

When you used the return $pdf->stream(); it did not pass the name of the file to be mounted.

return $pdf->stream('profile.pdf');

That should work.

    
19.06.2018 / 18:40