Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send dynamic data to Header function in TCPDF

Tags:

php

tcpdf

As I need to get some dynamic content to my page header. So, let me know the way to send data through parameters. I have not found how to call, send parameters to the header function. Please help me to solve this..

  1. How can I call Header() function with parameters?
  2. I need to send data through parameters to Header() function. enter image description here
like image 815
Dara Naveen Kumar Avatar asked Apr 02 '18 11:04

Dara Naveen Kumar


2 Answers

This can be accomplished by setting a new property of the TCPDF class. The property will need to be set before the AddPage() method is called for the next page. Before creating a new property you may want to check the TCPDF documentation for an existing property that may be useful. Searching “get” will allow you to quickly find them.

Be careful to give the new property a unique name, so you don’t change an existing property of TCPDF. You may want to include a check for the property in case one were to be added in a future version.

Setting a parameter of the Header() method is more difficult because it is called through a series of other methods (AddPage(), startPage(), setHeader()).

Example

This example sets a new string for each page header with the new CustomHeaderText property. The example will run inside the TCPDF examples directory.

<?php
require_once('tcpdf_include.php');
class MYPDF extends TCPDF
{
    public function Header()
    {
        $this->Write(0, $this->CustomHeaderText);
    }
}
$pdf = new MYPDF();
$pdf->CustomHeaderText = "Header Page 1";
$pdf->AddPage();
$pdf->writeHTMLCell(0, 0, '', 30, '<p>Page 1 Content</p>', 0, 1, 0, true, '', true);

$pdf->CustomHeaderText = "Header Page 2";
$pdf->AddPage();
$pdf->writeHTMLCell(0, 0, '', 30, '<p>Page 2 Content</p>', 0, 1, 0, true, '', true);

$pdf->Output('example.pdf', 'I');
like image 181
Adam Moller Avatar answered Sep 19 '22 02:09

Adam Moller


You can do this by adding a new property in your extended class MYPDF in this example

<?php 
require 'tcpdf.php';
class MYPDF extends TCPDF {

    protected $company;

    public function setCompany($var){
        $this->company = $var;
    }

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // setCompany Text
        $this->Cell(0, 10, $this->company, 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

To access this

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->setCompany("My Company");
like image 34
Amit Garg Avatar answered Sep 19 '22 02:09

Amit Garg