How to add header and footer in mpdf

3

I'm doing a job where I have to generate a PDF from mPDF and it generates correctly, but from the moment I add header or footer, it stops working and does not generate the PDF, the goal was to add pagination to the footer, which is one of the requirements I have already looked for other alternatives on the internet and are all based on the same, can you help me? Below I leave the code.

<?php

include("pdf/mpdf.php");

$servidor = "localhost"; $user = "root"; $password = ""; $database = "relatorios"; $idrelatorio = $_GET["idrelatorio"]; $con = mysqli_connect($servidor, $user, $password, $database);


$sql = "SELECT * FROM 'relatorios' WHERE 'id_relatorio'=" . $idrelatorio; $result = mysqli_query($con, $sql);


$pagina = "

    <h2>Relatório Dinâmico</h2>";

while ($row = mysqli_fetch_array($result)) {

    $id_categoria = $row["id_categoria"];
    $data_inicio = $row["data_inicial"];
    $data_fim = $row["data_final"];
    if ($row["id_categoria"] == 1) {
        $categoria = "Batismos";
    } else if ($row["id_categoria"] == 2) {
        $categoria = "Catecúmenos";
    } else if ($row["id_categoria"] == 3) {
        $categoria = "Óbitos";
    } else if ($row["id_categoria"] == 4) {
        $categoria = "Crismas";
    } else if ($row["id_categoria"] == 5) {
        $categoria = "Catequese";
    } else if ($row["id_categoria"] == 6) {
        $categoria = "Documentos";
    } else if ($row["id_categoria"] == 7) {
        $categoria = "Famílias";
    } else if ($row["id_categoria"] == 8) {
        $categoria = "Intenções de Missa";
    } else if ($row["id_categoria"] == 9) {
        $categoria = "Paroquianos";
    } else if ($row["id_categoria"] == 10) {
        $categoria = "Casamentos";
    }

    $pagina .= "
                   Categoria: " . $categoria . "<br>
                   Nome: " . $row['nome'] . "<br>
                   Descrição: " . $row['descricao'] . "<br>"; }


$sqldadoscampos = "SELECT 'NomeCampo','SQLCampo' FROM 'relatoriodinamicocampos' rdc, 'campos_categorias' c WHERE rdc.'idRelatorio'=" . $idrelatorio . " and rdc.'idCampo'=c.'idCampo'"; $resultdadoscampos = mysqli_query($con, $sqldadoscampos); $sqlultimo = "SELECT ";

$arrayCamposDescricao = array(); $arrayCamposValue = array();

while ($row = mysqli_fetch_array($resultdadoscampos)) {
    $sqlultimo .= " '" . $row["SQLCampo"] . "' ,";
    $arrayCamposDescricao[] = $row["NomeCampo"];
    $arrayCamposValue[] = $row["SQLCampo"]; }

$sqlultimo = substr($sqlultimo, 0, -1);

if ($id_categoria == 1) {
    $sqlultimo .= " FROM 'baptisms' WHERE 1 and 'baptism_date' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria == 2) {
    $sqlultimo .= " FROM 'catechumens' WHERE 1 and 'data_catecumenatos' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria == 3) {
    $sqlultimo .= " FROM 'deaths' WHERE 1 and 'data_catecumenatos' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria == 4) {
    $sqlultimo .= " FROM 'chrisms' cs, 'chrisms_entities' ce WHERE cs.'id_crismas'=ce.'id_crismas' and 'data_crisma' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria
== 5) {
    $sqlultimo .= " FROM 'catechisms' c,'catechisms_student' cs WHERE c.'id_catequese'=cs.'id_catequese' and 'inscription_date' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria
== 6) {
    $sqlultimo .= " FROM 'documents' WHERE 1"; } else if ($id_categoria == 7) {
    $sqlultimo .= " FROM 'families' f,'families_persons' fp WHERE f.'id_familias'=fp.'id_familias' and 'creation_date' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria
== 8) {
    $sqlultimo .= " FROM 'mass_intentions' WHERE 1 and 'intention_date' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria == 9) {
    $sqlultimo .= " FROM 'parishioners' WHERE 1 and 'birthdate' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; } else if ($id_categoria == 10) {
    $sqlultimo .= " FROM 'weddings' WHERE 1 and 'wedding_date' BETWEEN '" . $data_inicio . "' and '" . $data_fim . "'"; }

$pagina .= "<br/><br/>"; $resultdadosutlimocampos = mysqli_query($con, $sqlultimo); while ($row = mysqli_fetch_array($resultdadosutlimocampos)) {

    $cont = 0;
    foreach ($arrayCamposValue as $result) {
        $pagina .= "<div><b>" . $arrayCamposDescricao[$cont] . ":</b> " . $row[$result] . "</div>";
        $cont++;
    }

    $pagina .= "<br/><br/>"; }

$mpdf = new mPDF('', '', '', '', 30, 30, 40, 50);

$mpdf->SetHTMLHeader('<div>LDS</div>');

$footer = "<table width=\"1000\">
                   <tr>
                     <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">{PAGENO}</td>
                   </tr>
                 </table>";

$mpdf->SetHTMLFooter($footer);


$mpdf->WriteHTML($pagina); $mpdf->Output();


exit; ?>

If I do not have the "SetHTMLFooter" and the "SetHTMLHeader" works fine, the moment I add these lines it stops working.

    
asked by anonymous 31.12.2017 / 12:15

1 answer

1

When doubts arise, do not hesitate to look in official documentation .

The above link has this example (which fits your needs):

<?php
$mpdf = new \Mpdf\Mpdf();

// Define the Header/Footer before writing anything so they appear on the first page
$mpdf->SetHTMLHeader('
<div style="text-align: right; font-weight: bold;">
    My document
</div>');
$mpdf->SetHTMLFooter('
<table width="100%">
    <tr>
        <td width="33%">{DATE j-m-Y}</td>
        <td width="33%" align="center">{PAGENO}/{nbpg}</td>
        <td width="33%" style="text-align: right;">My document</td>
    </tr>
</table>');

$mpdf->WriteHTML('Hello World');

$mpdf->Output();

You just need to be aware of the use of namespaces (if you do not use adapt to require) and output.

    
31.12.2017 / 18:17