Arun Jain
Arun Jain

Reputation: 5464

TCPDF - page borders in all pages

I am generating PDF report in CakePHP Application using TCPDF Vendor package. I have to create page borders on each page of generated PDF.

I used this solution to make a page border, but was only able to draw the border on the very first page of generated PDF.

I am using the following code:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();

$pdf->SetLineStyle( array( 'width' => 15, 'color' => array(0,0,0)));

$pdf->Line(0,0,$pdf->getPageWidth(),0); 
$pdf->Line($pdf->getPageWidth(),0,$pdf->getPageWidth(),$pdf->getPageHeight());
$pdf->Line(0,$pdf->getPageHeight(),$pdf->getPageWidth(),$pdf->getPageHeight());
$pdf->Line(0,0,0,$pdf->getPageHeight());

//rest of my code to make proper Html
.....
.....

$pdf->writeHTML($content_html, true, 0, true, 0); //$content_html contains the whole Html which outputs me several PDF pages

ob_clean();
$pdf_status = $pdf->Output($directory_path.$file_name.EXT_PDF, 'F'); // save pdf on the given path

Kindly suggest the solution. Any help would be appreciated.

Upvotes: 2

Views: 6320

Answers (1)

Arun Jain
Arun Jain

Reputation: 5464

Following was the trick I used to make the margin border on all pages in generated pdf.

  1. Create a new class extend from TCPDF class
  2. Override the Header method. (This method will be called on each new pdfpage generation)

Please look into the code given below:

<?php
App::import('Vendor','tcpdf/tcpdf');
App::import('Vendor','tcpdf/config/lang/eng');
class AUTHPDF extends TCPDF
{
    protected $processId = 0;
    protected $header = '';
    protected $footer = '';
    static $errorMsg = '';

    /**
      * This method is used to override the parent class method.
    **/
    public function Header()
    {
       $this->writeHTMLCell($w='', $h='', $x='', $y='', $this->header, $border=0, $ln=0, $fill=0, $reseth=true, $align='L', $autopadding=true);
       $this->SetLineStyle( array( 'width' => 0.40, 'color' => array(153, 204, 0)));

       $this->Line(5, 5, $this->getPageWidth()-5, 5); 

       $this->Line($this->getPageWidth()-5, 5, $this->getPageWidth()-5,  $this->getPageHeight()-5);
       $this->Line(5, $this->getPageHeight()-5, $this->getPageWidth()-5, $this->getPageHeight()-5);
       $this->Line(5, 5, 5, $this->getPageHeight()-5);
    }
}

Upvotes: 6

Related Questions