Changing or eliminating Header & Footer in TCPDF - php

AddPage() in tcpdf automatically calls Header and Footer. How do I eliminate/override this?

Use the SetPrintHeader(false) and SetPrintFooter(false) methods before calling AddPage(). Like this:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();

A nice easy way to have control over when to show the header - or bits of the header - is by extending the TCPDF class and creating your own header function like so:
class YourPDF extends TCPDF {
public function Header() {
if (count($this->pages) === 1) { // Do this only on the first page
$html .= '<p>Your header here</p>';
}
$this->writeHTML($html, true, false, false, false, '');
}
}
Naturally you can use this to return no content as well, if you'd prefer to have no header at all.

Here is an alternative way you can remove the Header and Footer:
// Remove the default header and footer
class PDF extends TCPDF {
public function Header() {
// No Header
}
public function Footer() {
// No Footer
}
}
$pdf = new PDF();

How do I eliminate/override this?
Also, Example 3 in the TCPDF docs shows how to override the header and footer with your own class.

Example:
- First page, no footer
- Second page, has footer, start with page no 1
Structure:
// First page
$pdf->startPageGroup();
$pdf->setPrintFooter(false);
$pdf->addPage();
// ... add page content here
$pdf->endPage();
// Second page
$pdf->startPageGroup();
$pdf->setPrintFooter(true);
$pdf->addPage();
// ... add page content here
$pdf->endPage();

// set default header data
$pdf->SetHeaderData('', PDF_HEADER_LOGO_WIDTH, 'marks', 'header string');
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
With the help of above functions you can change header and footer.

Related

TCPDF and FPDI, footer with page nr. not working

I'm using TCPDF in combination with FPDI. All working good except the Footer (want to add Page Numbers there).
My Code:
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends FPDI {
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 5, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
// add external PDF with FPDI
$pdf = new FPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(1, 4, 1, 1, true);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 3);
... this footer hook is working fine if I'm not suing FPDI. What do I miss here?
You are initiating an instance of FPDI and not of MYPDF. For sure the Footer() method will not be invoked that way.
IIRC you also have to leave setPrintFooter() to true.

Send dynamic data to Header function in 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..
How can I call Header() function with parameters?
I need to send data through parameters to Header() function.
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');
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");

FPDF function control

I'm new to programming and I'm creating a pdf with FPDF(A PHP Class). At the bottom of my code you will notice that I create a new page and call the function Header(). For some reason, the header is also applied to the first page. How do I only apply the header to the second page?
Here is a link to a live version of the pdf: http://fosterinnovationculture.com/experiments/fpdf/index-two.php
<?php
require('fpdf.php');
// classes
class PDF extends FPDF
{
function Logo(){
$this->Image('images/logo.png',1,5.5,3);
}
//change name of function
function HeaderOne($xCor, $yCor, $text)
{
$this->SetX($xCor);
$this->SetY($yCor);
$this->SetFont('Arial','B',18);
$this->Cell(5,1,$text);
}
//Add bottom border
function BorderLine()
{
$this->SetDrawColor(0,0,0);
$this->SetFillColor(0,0,0);
$this->Rect(1, 10, 6.5, .015, 'F');
}
function Footer()
{
// Go to 1.5 cm from bottom
$this->SetY(-5.7);
// Select Arial italic 8
$this->SetFont('Arial','I',8);
// Print centered page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'R');
}
function Header(){
// Subtitle
$this->SetY(.25);
$this->SetX(1);
$this->SetFont('Arial','',12);
$this->Cell(6,1,'INNOVATION READINESS ASSESSMENT');
// Line
$this->SetDrawColor(0,0,0);
$this->SetFillColor(0,0,0);
$this->Rect(1, .5, 6.5, .015, 'F');
}
}
//pdf document preferences
$pdf = new PDF('p', 'in', 'Letter');
$pdf->AddPage();
$pdf->SetMargins(1,1,1,1);
//Page 1
$pdf->SetDrawColor(238,170,40);
$pdf->SetFillColor(238,170,40);
$pdf->Rect(1, 1, 6.5, .25, 'F');
$pdf->Image('images/header.jpg',1,1.5,6.5,3.5,'JPG','');
$pdf->Logo();
$pdf->HeaderOne(1,9,'Innovation Readiness Assessment');
$pdf->BorderLine();
//Page 2
$pdf->AddPage();
$pdf->Header();
// Close and output file to the browser
$pdf->Output();
?>
See the official documentation for Header() and
AddPage().
The AddPage() function already internally calls Header().
There is no need to call Header() explicitly here.

TCPDF does not render external CSS

I am trying to generate PDFs using TCPDF library. So far, I am able to generate the PDF files using PHP code. Although it does not render external CSS and result becomes useless as without CSS the document is not readable.
Following is my code in the controller:
public function generate_pdf() {
$this->load->model('rosters', 'roster');
$data = array();
$data['start_date'] = isset($_POST['fortnightStartDate']) ?
date("Y-m-d", strtotime(getFortnightStartDate($_POST['fortnightStartDate']))) :
getFortnightStartDate();
$cid = $_POST['cid'];
$wno = $_POST['wno'];
$data['no']=$wno;
$startDate = strtotime($data['start_date']);
$data['schedule'] = $this->roster->getShifts($cid, $data);
$data['startDate'] = $startDate;
$listing = $this->load->view('roster/roster_pdf', $data, true);
/**
* Creates an example PDF TEST document using TCPDF
* #package com.tecnick.tcpdf
* #abstract TCPDF - Example: XHTML + CSS
* #author Nicola Asuni
* #since 2010-05-25
*/
// Include the main TCPDF library (search for installation path).
require_once(APPPATH.'libraries/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 061');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 061', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
$pdf->writeHTML($listing, true, false, true, false, '');
//Close and output PDF document
$pdf->Output('TEST.pdf', 'F');
}
I am passing the $listing variable which holds the data that needs to be included in the PDF. The data is printing fine but without CSS. If I try to include the external CSS it pops up the error.
Code to include external css
$listing. = '<style>'.file_get_contents(base_url("styles/style.css")).'</style>';
I try to include this line of code below the $listing variable but it generates following error:
A PHP Error was encountered
Severity: Warning
Message: array_merge(): Argument #2 is not an array
Filename: tcpdf/tcpdf.php
Line Number: 16375
Is there any other way I can load the external CSS ??
Any help will be highly appreciated.
After researching here and there, I come to know that TCPDF library has few limitations including CSS limitation and does not support many CSS properties. Then, I looked into dompdf library and found it useful. Surprisingly, it solved all my problem with almost similar code and also it is way easy to use than TCPDF. Following is my piece of code i wrote to solve my problem (The code is similar until the $listing variable then i simply change the library code.)
$listing .= '<style>'.file_get_contents(base_url("styles/style.css")).'</style>';
require_once(APPPATH.'libraries/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($listing);
$dompdf->set_paper('a4', 'landscape');
$dompdf->render();
file_put_contents('my_pdf_test.pdf', $dompdf->output());
//$dompdf->stream("dompdf_out.pdf", array("Attachment" => true));
exit(0);

how TCPDF prevent the extra blank page

I have create class to make page by using TCPDF.
I need to convert HTML to pdf, so I using writeHTML and AcceptPageBreak().
The $html is Dynamically changed, could be very long.
class MY_TCPDF extends TCPDF{
public function makePage($html){
$head_image="header.jpg";
$this->SetMargins(PDF_MARGIN_LEFT, 70, PDF_MARGIN_RIGHT);
$this->setPrintHeader(false);
$this->AddPage();
// get the current page break margin
$bMargin = $this->getBreakMargin();
// get current auto-page-break mode
$auto_page_break = $this->getAutoPageBreak();
// disable auto-page-break
$this->SetAutoPageBreak(false, 0);
// set bacground image
$img_file = $head_image;
$this->Image($img_file, 0, 0, 210, 68, '', '', '', false, 300, '', false, false, 0);
// restore auto-page-break status
//$this->SetAutoPageBreak($auto_page_break, PDF_MARGIN_BOTTOM);
// set the starting point for the page content
$this->setPageMark();
$this->writeHTML($html, true, false, true, false, '');
$this->lastPage();
ob_start();
//Close and output PDF document
$this->Output('my.pdf', 'I');
ob_end_flush();
}
public function AcceptPageBreak() {
$this->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
$this->AddPage();
return false;
}
}
The problem is I genenrate PDF, but alway has a extra blank page in the end of the PDF.
I tried use $this->delete($this->getPage()) ,but it only remove last page which has content and the extra blank page remain. this seems writeHTML will create a page break after it.
how to prevent this extra blank page?
Try this deletePage function
$lastPage = $this->getPage();
$this->deletePage($lastPage);
Instead Delete use deletePage
I had the same Problem:
I fixed it with:
class TCPDFextended extends \TCPDF {
public function Output($name = 'doc.pdf', $dest = 'I')
{
$this->tcpdflink = false;
return parent::Output($name, $dest);
}
}
You should check your $html variable.
1) If it could contains any <html />, <head />, <title />, <body /> tag then please remove them and just take html contents after and before <body />.
2) You should avoid any css, js link file within $html content.
3) Finally you should use $html=utf8_encode($html); just before $this->writeHTML($html, true, false, true, false, '');.
4) You may need to adjust your MARGIN_LEFT, MARGIN_TOP, MARGIN_RIGHT and MARGIN_BOTTOM to solve such problems. Please check $this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); and $this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);.
Hopefully it can solve your problem.
My answer is similar to #kanti. I think we can set the default to false even before the Output generation.
Background. The extra page we see is basically
"If true print TCPDF meta link".
so by default the TCPDF::$tcpdflink = true , is set true.
All we need is
class My_PDF extends TCPDF {
public function changeTheDefault($tcpdflink) {
$this->tcpdflink = $tcpdflink;
}
}
call your public function later when you need it. ...
$get_pdf = new My_PDF (your_parameters);
$get_pdf->changeTheDefault(false); # changes the default to false
Good Luck.
Check also the height of your enclosing div.
It should not be 100%.
Try to remove any height property from the CSS style of the enclosing div ( I mean the div which encloses all the content).
The problem is the 4th parameter (unicode = true) in your create_pdf.php file. This parameter is passed into tcpdf.php on line 1838
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'ISO-8859-1', false);
change it to false.

Categories