Example 24

Description

A header and footer can be defined. Their height will reduce the printable height of the body.
The header here in the example contains a small image on the left side and some text on the right side. It has a grey background and a green line at the bottom of it.
The footer has a black line at the top and below that the page number and the total number of pages are printed.
A manual page break is added to show the repetition of the header and footer

Output

<?php

include_once "../src/Report.php";

use Adi\ReportLib as ReportLib;

// Create report instance
//  default format A4, portrait with margins left = 20mm, top = 10mm, right = 10mm and bottom = 10mm
$report = new ReportLib\Report();
$report->setCountPages(true);

// Get ref to the report body
$body = $report->getBody();
$header = $report->getHeader();
$footer = $report->getFooter();

addHeader($header);
addFooter($footer);

$body->AddPageBreak();

// Produce the output of the report
//  uses the same params as TCPDF (F = File, I = Preview etc.)
$report->output(__DIR__ . "/example_024.pdf", 'I');


function addHeader(ReportLib\SerialFrame $header) : void
{
    $tsBold = ReportLib\TextStyles::getTextStyle(ReportLib\TextStyles::BOLD);

    $vc = $header->AddVContainer();
    $vc->setMarginBottom(5.0);

    $box = $vc->AddBox();
    $box->setUseFullWidth(true);
    $box->setPadding(1.0);
    $box->setHeight(15.0);
    $box->setBackground("#EEEEEE");

    $hc = $box->AddHContainer();
    $hc->setUseFullWidth(true);
    $hc->setUseFullHeight(true);

    $if = $hc->AddImage("image.jpg", true, 0.0, 10.0);
    $if->setVAlignment('M');

    $tf = $hc->AddText("Fancy report\nwith a header and a footer", $tsBold);
    $tf->setHAlignment('R');
    $tf->setVAlignment('T');

    $bf = $vc->AddHLine(0.3,"#00FF00");
}


function addFooter(ReportLib\SerialFrame $footer) : void
{
    $tsNormal = ReportLib\TextStyles::getTextStyle(ReportLib\TextStyles::NORMAL);

    $vc = $footer->AddVContainer();
    $vc->setMarginTop(5.0);

    $lf = $vc->AddHLine(0.3);

    $box = $vc->AddBox();
    $box->setUseFullWidth(true);
    $box->setHeight(10.0);

    $tf = $box->AddText("Page [VAR_PAGE] of [VAR_TOTAL_PAGES]", $tsNormal);
    $tf->setHAlignment('R');
    $tf->setVAlignment('T');
}