APD Toronto
asked on
Configuring TCPDF with Header and Footer
Hi Experts,
How can I get TCPDF to have a header and footer on every page? More so I need the header to be in html and would take up about 10% of the page.
Thank you
How can I get TCPDF to have a header and footer on every page? More so I need the header to be in html and would take up about 10% of the page.
Thank you
First Create HTML of your page, after that create PDF of that HTML.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Ray,
How would you in that case use your extended class to put your name of the top of every page?
Furthermore, are you able to also add the date to the header only on page 1, above your name?
How would you in that case use your extended class to put your name of the top of every page?
Furthermore, are you able to also add the date to the header only on page 1, above your name?
TCPDF has online man pages:
http://www.tcpdf.org/doc/code/classTCPDF.html
TCPDF has a lot of online examples:
http://www.tcpdf.org/examples.php
Expect that a lot of experimentation will be needed.
http://iconoun.com/demo/temp_adp_toronto.php
http://www.tcpdf.org/doc/code/classTCPDF.html
TCPDF has a lot of online examples:
http://www.tcpdf.org/examples.php
Expect that a lot of experimentation will be needed.
http://iconoun.com/demo/temp_adp_toronto.php
<?php // demo/temp_adp_toronto.php
/**
* http://www.experts-exchange.com/questions/28693143/Configuring-TCPDF-with-Header-and-Footer.html#a40857620
*
* Demonstrate how to put an image into a TCPDF document
* and write some text over part of the image
*
* See http://www.tcpdf.org/doc/code/annotated.html
*/
error_reporting(E_ALL);
// A DATE SETTING MAY BE REQUIRED - DEPENDS ON PHP INSTALLATION SETTINGS
date_default_timezone_set('America/Chicago');
// SYNTHESIZE THE PDF FILE URL AND FILE NAME
$pdf_file_link = 'storage/tcpdf_example.pdf';
$pdf_file_name = getcwd() . DIRECTORY_SEPARATOR . $pdf_file_link;
// LOAD THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// EXTEND THE TCPDF OBJECT SO WE CAN SUBSTITUTE OUR OWN METHODS
class PDF extends TCPDF
{
// NULLIFY AUTOMATIC FOOTER
public function Footer() {}
// ADD CUSTOM HEADER
public function Header()
{
$this->SetFont('helvetica', 'B', 20);
$this->Cell
( 0 // $w width
, 15 // $h height
, $this->headerText
, 0 // border
, 1 // move cursor to next line after
, 'L' // left-align text
, FALSE // no fill
, '' // link (none)
, 0 // stretch disabled
, FALSE // ignore min height
, 'T' // cell align
, 'C' // text aligh
)
;
}
}
// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);
// SET THE TEXT FOR THIS HEADER
$pdf->headerText = 'Anything you want ' . date('c');
// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);
// NO CELL BORDER IS NEEDED
$bdr = 0;
// ADD AN IMAGE
$img = 'images/ray_padded.png';
// I HAVE NO IDEA WHAT THIS IS DOING TO IMAGE SCALE - EXPERIMENT WITH IT
$pdf->setImageScale(1.53);
// GET THE IMAGE SETTINGS
$pdf->Image
( $img
, 0 // $x
, 30 // $y
, 0 // WIDTH
, 0 // HEIGHT
, 'PNG' // TYPE
, '#' // LINK URL
, 'T' // SET POINTER TOP LEFT
, FALSE // NO RESIZING
, 300 // DPI
, 'L' // PALIGN
, FALSE // ISMASK
, FALSE // IMGMASK
, 0 // BORDER
, FALSE // FIT TO BOX
, FALSE // HIDDEN
, FALSE // FIT ON PAGE
)
;
// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24 // $x,
, 30+24 // $y,
, 'This is Ray'
, FALSE // $fstroke = false,
, FALSE // $fclip = false,
, TRUE // $ffill = true,
, $bdr // $border = whatever,
, 2 // $ln = 0, 2=PUT CURSOR BELOW
, '' // $align = '', DEFAULT LEFT
, FALSE // $fill = false, CELL BACKGROUND
, '' // $link = '', NOT A LINK
, 0 // $stretch = 0, NO TEXT STRETCH
, FALSE // $ignore_min_height = false,
, 'A' // $calign = 'T', A=FONT TOP INSIDE CELL
, 'T' // $valign = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE // $rtloff = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;
// CHANGE THE TEXT FOR THIS HEADER
$pdf->headerText = 'New headerText';
// ADD ANOTHER PAGE
$pdf->AddPage('P', 'LETTER', TRUE);
// ADD SOME TEXT ON THIS PAGE
$my_text = 'This is the second page';
$pdf->Text
( 24 // $x,
, 30+24 // $y,
, $my_text
, FALSE // $fstroke = false,
, FALSE // $fclip = false,
, TRUE // $ffill = true,
, $bdr // $border = whatever,
, 2 // $ln = 0, 2=PUT CURSOR BELOW
, '' // $align = '', DEFAULT LEFT
, FALSE // $fill = false, CELL BACKGROUND
, '' // $link = '', NOT A LINK
, 0 // $stretch = 0, NO TEXT STRETCH
, FALSE // $ignore_min_height = false,
, 'A' // $calign = 'T', A=FONT TOP INSIDE CELL
, 'T' // $valign = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE // $rtloff = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;
// WRITE THE PDF FILE TO THE SERVER
$pdf->Output($pdf_file_name, 'F');
// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE PDF
echo '<a target="my_PDF" href="' . $pdf_file_link . '"><strong>Print the PDF</strong></a>';
ASKER
Hi Ray,
I tried running your code above on my machine, but my file structure is as follows.
http://localhost/test/pdf_header.php is your code
http://localhost/tcpdf/ is the tcpdf directory
with this said, I adjusted the beginning of your code as such. This code works in my other tests.
However, I am getting
Warning: require_once(tcpdf/config/ lang/eng.p hp): failed to open stream: No such file or directory in C:\inetpub\wwwroot\test\pd f_header.p hp on line 9
Fatal error: require_once(): Failed opening required 'tcpdf/config/lang/eng.php ' (include_path='.;C:\php\pe ar') in C:\inetpub\wwwroot\test\pd f_header.p hp on line 9
I tried running your code above on my machine, but my file structure is as follows.
http://localhost/test/pdf_header.php is your code
http://localhost/tcpdf/ is the tcpdf directory
with this said, I adjusted the beginning of your code as such. This code works in my other tests.
<?php
// SYNTHESIZE THE PDF FILE URL AND FILE NAME
$pdf_file_name = 'test.pdf';
// LOAD THE TCPDF CLASS AND CONFIGURATION
$currentDIR = getcwd();
chdir("../");
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
//Change DIR back
chdir($currentDIR);
However, I am getting
Warning: require_once(tcpdf/config/
Fatal error: require_once(): Failed opening required 'tcpdf/config/lang/eng.php
I'm going to step away from this and your other TCPDF questions. I just can't seem to help you. Even when I give you tested and working code examples, we don't seem to be making progress. Good luck with the project.
ASKER
So, my issue is that I'm not using your code and environment to the dot? Would it be easier if I just give yo my client?
I'm sure they will be thrilled that all their financial reports have your picture on every page
I'm sure they will be thrilled that all their financial reports have your picture on every page
Dear APD_Toronto,
I am working for you to create sample PDF which may help you.
I am working for you to create sample PDF which may help you.
ASKER
Thank you.
ASKER
With some experimenting I came up with the following code, but I am not getting "page 1" on my header as per lines 18-21. This is my main objective here.
<?php
//Temporarily Change directory to TCPDF to load files...
$currentDIR = getcwd();
chdir("../tcpdf/examples");
require_once('tcpdf_include.php');
//Change DIR back
chdir($currentDIR);
class MYPDF extends TCPDF {
//Page header
public function Header() {
// Title
$this->Ln();
$page = $this->getAliasNumPage();
if ($page == 1){
$this->Cell(0, 15, 'Page 1', 0, false, 'C', 0, '', 0, false, 'M', 'M');
$this->Ln();
}
$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}
// 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, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
$pdf = new MYPDF('L', 'mm', array(215.9, 279.4));
$pdf->SetTitle('Manifest');
$pdf->SetMargins(5, 5);
$pdf->SetAutoPageBreak(TRUE);
$pdf->SetPrintHeader(TRUE);
$pdf->SetHeaderMargin(10);
$pdf->SetFont('Helvetica', '', 9);
$pdf->AddPage();
$pdf->AddPage();
$pdf->AddPage();
$file = 'Manifest.pdf';
$pdf->Output($file);
?>