Generating pdf with MPDF class

Asked

Viewed 2,392 times

1

I am generating a PDF with database data and the code it is running correctly.

Behold:

<?php

include_once '../controller/ClienteController.php';
include_once '../controller/LocacaoController.php';
include_once 'mpdf/mpdf.php';

$cc = new ClienteController();
$cliente = $cc->listaClienteId($_REQUEST['id']);

$endereco = $cliente->getEndereco();
$telefones = $cliente->getTelefones();
$telefone1 = $telefones[0];
$telefone2 = $telefones[1];

$nomeCliente = $cliente->getNome();
$cpf = $cliente->getCpf();
$tel1 = $telefone1;
$tel2 = $cc->validaTelefone($telefone2);
$cidade = $endereco->getCidade();
$rua = $endereco->getRua();
$numero = $endereco->getNumero();

$lc = new LocacaoController();
$objetos = $lc->listaLocacoesFitaId($_REQUEST['id']);
$locacao = $objetos[0];

$cliente = $locacao->cliente();
$filme = $locacao->midia();
$valor = "R$" . number_format($locacao->getValor(), 2);
$multa = "R$" . number_format($locacao->getMulta(), 2);
$dataL = $locacao->getDataLocacao();
$dataE = $locacao->getDataEntrega();


$pagina ="
<html>
<body>
<h1>Vintage Locadora</h1>
<h2>Histórico de Locações de Clientes</h2>

<h3>Dados do Cliente</h3>

<table>
  <tr>
    <th>Nome</th>
    <th>CPF</th>
    <th>Telefone 1</th>
    <th>Telefone 2</th>
    <th>Cidade</th>
    <th>Rua</th>
    <th>Numero</th>
  </tr>
  <tr>
    <td>$nomeCliente</td>
    <td>$cpf</td>
    <td>$tel1</td>
    <td>$tel2</td>
    <td>$cidade</td>
    <td>$rua</td>
    <td>$numero</td>
  </tr>
</table>

<h3>Histórico de Locações de Fita</h3>

<table>
  <tr>
    <th>Nome do Cliente</th>
    <th>Filme Locado</th>
    <th>Valor da Locação</th>
    <th>Multa</th>
    <th>Data da Locação</th>
    <th>Data de Entrega</th>
  </tr>
  <tr>
    <td>$cliente</td>
    <td>$filme</td>
    <td>$valor</td>
    <td>$multa</td>
    <td>$dataL</td>
    <td>$dataE</td>
  </tr>
</table>
</body>
</html>
";

$arquivo ='vintage.pdf';
$pdf = new mPDF();
$css = file_get_contents('css/historico-estilo.css');
$pdf->WriteHTML($css, 1);
$pdf->WriteHTML($pagina);
$pdf->Output($arquivo, 'I');

?>

inserir a descrição da imagem aqui

The problem is, on the lines of code:

$objetos = $lc->listaLocacoesFitaId($_REQUEST['id']);
$locacao = $objetos[0];

If you return more than one object, how will I create the new rows in the table? Because with only one object I already define within the string $pagina lines with the values of the object, now returning more than one object, I have to create dynamic lines for the table.

  • Have you tried to implement any loop loop that generates the table for all records?

  • @Andersoncarloswoss as well ? has to give me an example

1 answer

2


First, we can isolate the tasks. As the data appears only once, let’s define the text in the variable $top, with the header codes and client details:

include_once '../controller/ClienteController.php';
include_once '../controller/LocacaoController.php';
include_once 'mpdf/mpdf.php';

$cc = new ClienteController();
$cliente = $cc->listaClienteId($_REQUEST['id']);

$endereco = $cliente->getEndereco();
$telefones = $cliente->getTelefones();
$telefone1 = $telefones[0];
$telefone2 = $telefones[1];

$nomeCliente = $cliente->getNome();
$cpf = $cliente->getCpf();
$tel1 = $telefone1;
$tel2 = $cc->validaTelefone($telefone2);
$cidade = $endereco->getCidade();
$rua = $endereco->getRua();
$numero = $endereco->getNumero();

$top = "
<html>
<body>
<h1>Vintage Locadora</h1>
<h2>Histórico de Locações de Clientes</h2>

<h3>Dados do Cliente</h3>

<table>
  <tr>
    <th>Nome</th>
    <th>CPF</th>
    <th>Telefone 1</th>
    <th>Telefone 2</th>
    <th>Cidade</th>
    <th>Rua</th>
    <th>Numero</th>
  </tr>
  <tr>
    <td>$nomeCliente</td>
    <td>$cpf</td>
    <td>$tel1</td>
    <td>$tel2</td>
    <td>$cidade</td>
    <td>$rua</td>
    <td>$numero</td>
  </tr>
</table>

<h3>Histórico de Locações de Fita</h3>

<table>
  <tr>
    <th>Nome do Cliente</th>
    <th>Filme Locado</th>
    <th>Valor da Locação</th>
    <th>Multa</th>
    <th>Data da Locação</th>
    <th>Data de Entrega</th>
  </tr>";

Now, to enter the data of all locations, just create a repeat loop. generating a new row in the table for each record:

$mid = "";

$lc = new LocacaoController();
$objetos = $lc->listaLocacoesFitaId($_REQUEST['id']);

foreach ($objetos as $locacao)
{

    $cliente = $locacao->cliente();
    $filme = $locacao->midia();
    $valor = "R$" . number_format($locacao->getValor(), 2);
    $multa = "R$" . number_format($locacao->getMulta(), 2);
    $dataL = $locacao->getDataLocacao();
    $dataE = $locacao->getDataEntrega();

    $mid .=  "<tr>
                <td>$cliente</td>
                <td>$filme</td>
                <td>$valor</td>
                <td>$multa</td>
                <td>$dataL</td>
                <td>$dataE</td>
              </tr>";

}

Once this is done, we need the code that completes the document, $bot:

$bot = "</table>
        </body>
        </html>";

Finally, we put the three parts together to create the content of the page:

$pagina = $top . $mid . $bot;

And generate the PDF:

$arquivo ='vintage.pdf';
$pdf = new mPDF();
$css = file_get_contents('css/historico-estilo.css');
$pdf->WriteHTML($css, 1);
$pdf->WriteHTML($pagina);
$pdf->Output($arquivo, 'I');

Browser other questions tagged

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