2012-07-03 251 views
7

我忙于一个需要大量pdf文件的项目。因为他们都需要公司的设计,所以我使用带有标志/水印的背景图片。TCPDF autopagebreak + backgroundimage

一切都很好,如果我只有1页,但是当有多个页面时,背景只在第一页。

$pdf->Image('bg/background.jpg', 0, 0, 210, 297, '', '', '', false, 0, '', false, false, 0); 
     $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 
$pdf->setPageMark(); 
$pdf->SetAutoPageBreak(true); 
$pdf->writeHTML($bodyText, true, true, true, true, ''); 
$pdf->lastPage(); 
$pdf->Output('doc.pdf', 'I'); 

所以我的$ bodyText的更多的则是1页...

是否有解决方案有每一页的背景?

感谢

沃特

回答

19

您可以用自定义标题功能扩展TCPDF类,并添加图片到标题,带有TCPDF::Image。这里是如何做到这一点内TCPDF examples

从例如一个例子:

// Extend the TCPDF class to create custom Header and Footer 
class MYPDF extends TCPDF { 
    //Page header 
    public function Header() { 
     // get the current page break margin 
     $bMargin = $this->getBreakMargin(); 
     // get current auto-page-break mode 
     $auto_page_break = $this->AutoPageBreak; 
     // disable auto-page-break 
     $this->SetAutoPageBreak(false, 0); 
     // set bacground image 
     $img_file = K_PATH_IMAGES.'image_demo.jpg'; 
     $this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0); 
     // restore auto-page-break status 
     $this->SetAutoPageBreak($auto_page_break, $bMargin); 
     // set the starting point for the page content 
     $this->setPageMark(); 
    } 
} 

,并使用MYPDF代替TCPDF就像你使用TCPDF。我不知道PDF主体是否可以与标题重叠,但我认为如果您明确指定页边距和标题大小,则可以。

让我知道这是否工作。

+0

谢谢!这很好用! – TwinsIT

+1

您可以随时将其标记为解决方案,如果您觉得它值得投票,也可以对其进行投票。 – SinistraD

+0

对我也适用,谢谢。 – ekerner