Generating pdf with class MPDF

1

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

See:

<?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');

?>

The problem is, in the lines of code:

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

If I 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 to rows with object values, now returning more than one object, I have to create dynamic rows for the table.

    
asked by anonymous 19.02.2017 / 22:15

1 answer

2

First, we can isolate the tasks. Since the data only appears once, let's define the text in the variable $top , with the header codes and customer 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 data for all locations, simply 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 ends the document, $bot :

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

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

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

And generate 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');
    
19.02.2017 / 22:52