Configure page headers with DOMPDF

4

Is there any way to configure DOMPDF to place a header on all pages of the generated PDF?

I am generating a report and would like all the pages of the report to have the header of the first one.

    //define o estilo da página pdf
$style='
    <style>
    @page {
            margin-top: 20px;
        }
#head{
background-image: url("fad.jpg");
background-repeat: no-repeat;
font-size: 25px;
text-align: center;
height: 60px;
width: 500px;
margin: 0 auto;
}
#corpo{
width: 500px;
margin: 0 auto;
}
table{
border-collapse: collapse;
}
td{
    padding: 3px;
}
    </style>';

//define o cabeçalho da página
$head='<div id="head">Lista de Compras</div>
<div id="corpo">';


//inclui a biblioteca do dompdf
require_once("lib/dompdf/dompdf_config.inc.php");

//recebendo os dados do Formulário
$quant=$_POST['quantidade'];
$tipo=$_POST['tipo'];
$produto=$_POST['produto'];
$obs=$_POST['obs'];

//define o corpo do documento
$body='
    <table border="1px" >
        <tr bgcolor="#ccc">
            <td>Quantidade</td>
            <td>Tipo</td>
            <td>Produto</td>
            <td>Obs.</td>
        </tr>
    ';
for ($i = 0; $i < count($quant); $i++) {
    $tmp='<tr>
        <td width="15%">'.$quant[$i].'</td>
        <td width="15%">'.$tipo[$i].'</td>
        <td width="40%">'.$produto[$i].'</td>
        <td width="30%"> '.$obs[$i].'</td>
        ';   
    $body=$body.$tmp;
}


//define o rodapé da página
$footer='
    </table>
    </div>
    ';

//concatenando as variáveis
$html=$head.$style.$body.$footer;

//gerando o pdf
$html = utf8_decode($html);
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("compras.pdf");
    
asked by anonymous 09.09.2015 / 19:02

1 answer

4

Ready.

Changes

  • I've set the html head and body tags in sequence, just like a .html .
  • I put type="text/css" . It's good to tell the browser specifically what the code is.
  • In the header #head I added position:fixed , it's mandatory to fix it on every page. Left in the center of the page using margin: auto , with left: 0 and right: 0 . CSS normal.
  • No% of% that controls the structure of the page itself, set top% of%. And I set @page of 120px to -110 so that the header does not go up on content.
  • I put margin-top in the table header so that it repeats on every page. It was my choice, but you can take it if you want.
  • I put #head - not within the repetitive structure - just to tell the code that from there comes the tabulated data (specify).
  • Then I closed the tags thead and tbody at the end.
  • Footer should also be body and out of body content. Just like the header.
  • Code

    <?php
         //define o estilo da página pdf
         $style='<html><head>
            <style type="text/css">
           @page {
                margin: 120px 50px 80px 50px;
            }
            #head{
                background-image: url("fad.jpg");
                background-repeat: no-repeat;
                font-size: 25px;
                text-align: center;
                height: 110px;
                width: 100%;
                position: fixed;
                top: -100px;
                left: 0;
                right: 0;
                margin: auto;
            }
            #corpo{
                width: 600px;
                position: relative;
                margin: auto;
            }
            table{
                border-collapse: collapse;
                width: 100%;
                position: relative;
            }
            td{
                padding: 3px;
            }
            #footer {
                position: fixed;
                bottom: 0;
                width: 100%;
                text-align: right;
                border-top: 1px solid gray;
            }
            #footer .page:after{ 
                content: counter(page); 
            }
            </style></head><body>';
    
        //define o cabeçalho da página
        $head='<div id="head">Lista de Compras</div>
               <div id="corpo">';
    
        //inclui a biblioteca do dompdf
        require_once("dompdf/dompdf_config.inc.php");
    
        //recebendo os dados do Formulário
        $quant      = 22;
        $tipo       = 'Unidade';
        $produto    = 'Garrafa PET';
        $obs        = 'Sem Obs';
    
        //define o corpo do documento
        $body='
            <table border="1px">
                <thead>
                <tr bgcolor="#ccc">
                    <td>Quantidade</td>
                    <td>Tipo</td>
                    <td>Produto</td>
                    <td>Obs.</td>
                </tr></thead><tbody>';
    
        for ($i = 0; $i < 130; $i++) {
            $tmp='<tr>
                <td width="15%">'.$quant.'</td>
                <td width="15%">'.$tipo.'</td>
                <td width="40%">'.$produto.'</td>
                <td width="30%"> '.$obs.'</td>';   
            $body = $body.$tmp;
        }
    
    
        //define o rodapé da página
        $footer='</tbody>
            </table>
            </div>
            <div id="footer">
                <p class="page">Página </p>
            </div></body></html>  ';
    
        //concatenando as variáveis
        $html=$head.$style.$body.$footer;
    
        //gerando o pdf
        $html = utf8_decode($html);
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        $dompdf->stream("compras.pdf");
    
    ?>
    
        
    15.09.2015 / 16:10