Search code examples
dompdf

DOMPDF repeat element in dynamic page


I am generating a report using dompdf. And tried repeating an element in a page where it could take up to 4 pages depending on the data. But it only created on the first page

Currently i have this html markup.

<html>
  <head>
<style>
  @page { 
	margin: .5in;  
  }
  .footer{
	position:fixed;
	width:100%;
	bottom:60px;	
  }
  .page-3{
	position:relative;
  }
  .repeat{
	position:fixed;
	width:100%;
                top:10px;
  }
</style>
  </head>
  <body>		
	
<div class="page page-1">
  <div class="content">
	<div class="content-header">...</div>
	<div class="content-text">...</div>
  </div>
</div>

<div class="page page-2">
  <div class="content">
	<div class="content-header">...</div>
	<div class="content-text">...</div>
  </div>
</div>

<div class="page page-3">
  <div class="content">
	<div class="content-header repeat">
	  This must repeat in all pages generated
	</div>
	<div class="content-text">
	  atleast 2~4 pages
	</div>
  </div>
</div>

<div class="page page-4">
  <div class="content">
	<div class="content-header">...</div>
	<div class="content-text">...</div>
  </div>
</div>

<div class="footer">
  footer
</div>
	
  </body>
</html>

Let's say .page-3 generated 3 pages. But the .repeat is only seen on the first page inside .page-3

I tried using a background image for .page-3 but it still the same and is shown only on the first generated page.

I using the 0.8.2 version of dompdf.


Solution

  • If anyone is having a same problem as i have. I end up using a page_script.

    Since i know the first 2 pages are static and the last page is static, I accessed the $PAGE_NUM and place a condition like this

    `

        $pdf->page_script('
                if( $PAGE_NUM != 1 ){ // 1st page has no header
                    $text = "";
                    if( $PAGE_NUM == 2 ){
                        $text = "This is 2nd page";
                    }else if( $PAGE_NUM > 2 && $PAGE_NUM < $PAGE_COUNT ){
                        $text = "This is the page 3 until before the last page";
                    }else if( $PAGE_NUM == $PAGE_COUNT ){
                        $text = "This is last page";
                    }
    
                    // setup postioning, color, size etc for design     
    
                    // use $text as header text                 
                    $pdf->text( $posX, $posY, $text, $font, $size, $color);
                }
    
        ');
    

    `

    And removed all <div class="content-header">...</div> in the HTML markup