DEV Community

Solmaz Babakan
Solmaz Babakan

Posted on

dompdf page numbering and cover image for html content exceeding one print page.

Hi everyone.
i fetched some html content from phpmyadmin sql table and print them to pdf by dompdf in my codeigniter 3 project.
here, i have some columns (page titles) with some html content which could exceed one print page (A4 landscape).
as well i add page number or background cover for pages, but just the first page in each column content has cover image and just the last page has footers for page number.
here is the function i used to create html content for dompdf:
PHP Code:


    // Helper function to generate page HTML with title
    public function generatePageHtml($name,$title1, $title2 = null, $title3 = null, $content, $image_path, $page_number=null) {
        $html = '<div style="position: relative; width: 100%; height: 100%; page-break-after: always; margin: 0; padding: 0;">';
        $html .= '<style>@page { margin: 0; }</style>';

        if ($image_path && file_exists($image_path)) {
            $image = file_get_contents($image_path);
            $base64 = 'data:image/jpeg;base64,' . base64_encode($image);
            $html .= '<img src="' . $base64 . '" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; object-fit: cover; margin: 0; padding: 0;">'; // opacity: 0.7;
        }

        $html .= '<div style="padding: 20px; position: relative; z-index: 1; margin: 0;">';
        $html .= '<h1 style="text-align: center; text-decoration: underline; margin: 0;">' . $title1 . '</h1>';

        if ($title2) {
            $html .= '<h2 style="text-align: center; margin: 0;">' . $title2 . '</h2>';
        }
        if ($title3) {
            $html .= '<h3 style="text-align: center; margin: 0;">' . $title3 . '</h3>';
        }

        $html .= $content;
        $html .= '</div>';
        $html .= '<div style="position: absolute; bottom: 20px; left: 20px; font-size: 12px; font-weight: bold;font-style: italic; color: gray;"> '. $name .' - '. $title1. ' - '. $title2. '</div>';

        if ($page_number !== null) {
            $html .= '<div style="position: absolute; bottom: 20px; right: 10px; font-size: 12px; font-weight: bold; color: gray;border: 1px solid black; padding: 10px; display: inline-block; border-radius: 3px;"> '.$page_number . '</div>';
        }
        $html .= '</div>';
        return $html;
    } 


Enter fullscreen mode Exit fullscreen mode

how should i consider coding so that when a html content exceeds one print page, the next pages of same content as well has cover image and page numbering for all.

Top comments (0)