Search code examples
phpsymfonydompdf

How can I use html code in DOM PDF footer?


This is how I create the footer with DOMPdf:

$canvas = $dompdf->get_canvas();
$footer = $canvas->open_object();
$w = $canvas->get_width();
$h = $canvas->get_height();
$canvas->page_text($w-85,$h-28,"footer right",'helvetica',8);
$canvas->page_text($w-320,$h-28,"Page {PAGE_NUM} of {PAGE_COUNT}",'helvetica',8);
$canvas->page_text($w-550,$h-28,"footer left", 'helvetica',8);

This creates 3 sections in my footer, which works fine for me. But I need the possibility in each line to integrate html text. I tried it like this:

    $canvas->page_text($w-85,$h-28,"<b>My Company</b><br>My street<br>My city",'helvetica',8);
    $canvas->page_text($w-320,$h-28,"Page {PAGE_NUM} of {PAGE_COUNT}",'helvetica',8);
    $canvas->page_text($w-550,$h-28,"footer left", 'helvetica',8);

But the html code is not converted in the pdf.

I also tried:

$text = $dompdf->loadHtml("<b>My Company</b><br>My street<br>My city");
$canvas->page_text($w-85,$h-28,$test,'helvetica',8);

Like this nothing is loaded


Solution

  • Working example:

    <html>
    <head>
        <style>
        @page { margin: 180px 50px; }
        #header { position: fixed; left: 0px; top: -180px; right: 0px; height: 150px; background-color: orange; text-align: center; }
        #footer { position: fixed; left: 0px; bottom: -180px; right: 0px; height: 150px; background-color: lightblue; }
        #footer .page:after { content: counter(page, upper-roman); }
        </style>
    <body>
        <div id="header">
        <h1>Widgets Express</h1>
        </div>
        <div id="footer">
        <p class="page">Page </p>
        </div>
        <div id="content">
        <p>the first page</p>
        <p style="page-break-before: always;">the second page</p>
        </div>
    </body>
    </html>