SOLVED - fpdf header from the second page - php

class PDF extends FPDF
{
var $cf;
function change( $cf) {
$this->cf = $cf;
}
// Page header
function Header()
{
$this->Cell(20,10,'CF / PI:'. $this->cf, 0, 1,'L');
// Line break
$this->Ln(2);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
$pdf->change($row['cf']);
With this code I can see the header from the second page though. the first page does not show anything.
I would like to see this variable that I have in the php file in the header on all pages.
how can I do?

Related

FPDF : set position from inner page

Using fpdf to create my document, it works perfectly but for one thing. There is a superposition a one line on the footer part.
Here is my Pdf class header function :
function Header() {
$this->setY(40);
$titre="MY TITLE";
$this->SetFont('Arial','B',16);
.....
doing all my stuff
.....
$this->Ln();
}
Here is the Footer method :
function Footer() {
$this->SetY(-25); --postionning footer at 2.5 cm from the bottom
....
doing my stuff
}
calling program:
$pdf = new Pdf();
$pdf->Open();
$pdf->addPage();
$request= "SELECT.....";
$result = $link->query($request) ;
while($row=$result->fetchRow()) {
$pdf->SetX(18);
foreach($row as $champ=>$valeur) {
....
displaying the fields in the document
...
}
$pdf->Ln();
}
My problem : it displays 1 line too much that superposes the Footer.
I would like to give a position between the footer and the inner page. Is that possible?
Thank you
Use the SetAutoPageBreak() method to increase the bottom margin, for example:
$pdf = new Pdf();
$pdf->SetAutoPageBreak(true, 40);

header and footer line in FPDI/TCPDF

I'm stamping my PDF documents with FPDI and TCPDF functions, and I'm trying to figure it out, how can I add a line below the text in Header, and upon the text in footer. Here is my code:
<?php
require_once('lib/tcpdf/tcpdf.php');
require_once('fpdi.php');
$fullPathToFile = "TestClanek6.pdf";
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile; //global
if(is_null($this->_tplIdx)) {
// number of pages
$this->numPages = $this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
if($this->page > 0) {
//$this->SetPrintHeader(true);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0);
$this->Write(15, "Vol. 1, No. 15, Year: 2015, duff");
$this->Image('logo.png', 100, 2, 75,7);
} //end of IF
$this->useTemplate($this->_tplIdx, 0, 0,200);
} //end of HEADER
function Footer() {
if($this->page > 0) {
$this->SetY(-20);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0,0,0);
$this->Write(0, "Page", '', 0, 'C');
} //end of if
} // end of footer
} //end of CLASS
// new PDF file
$pdf = new PDF();
$pdf->addPage();
if($pdf->numPages>0) {
for($i=1;$i<=$pdf->numPages;$i++) {
$pdf->endPage();
$pdf->_tplIdx = $pdf->importPage($i);
$pdf->AddPage();
//$pdf->SetPrintHeader(false);
//$pdf->SetPrintFooter(false);
}
}
$file_time = time();
$pdf->Output("$file_time.pdf", "F");//, "I");
echo "Link: '<a href=$file_time.pdf>Stamped article</a>'";
?>
I've tried a lot of things like setPrintHeader(), etc. but nothing what I've found works for me. Could i please somebody to help?
Thank you.
duff
You can use the Line method to draw a line in FPDF. If you want a straight horizontal line, just make sure the ordinates (y values) for the start and end of the line are the same. Something like this for example:
$pdf->Ln(15,$pdf->y,200,$pdf->y);
You would modify the values to suit your needs and insert it in the overridden methods for Header and Footer as appropriate for your application.
Better would be to leave the two methods (Header and Footer) empty. this way you would overwrite the drawing from super-class.
like this:
class EmptyFPDI extends FPDI
{
public function Header()
{
}
public function Footer()
{
}
}

TCPDF Multiple Footers

I am using TCPDF to generate PDF documents. The problem I have is that I am trying to add a different footer per page. Apparently, TCPDF provides a solution only for a single footer.
Each footer contains only basic html code without any styling.
Any ideas?
You can turn of the default footer like this:
$pdf->SetPrintFooter(false);
create your own one like this:
$footer = 'yau man footer stuff';
and then create your own footer function:
public function _footer($input) {
$text = $input;
$pdf->setY(-15); // or whatever location your footer should be displayed.
$pdf->Cell ( // or another method like Write(), WriteHTML() ..
$width = 0, // width of the cell, not the input
$height = 0, // height of the cell..
$x,
$y,
$text = '', // your input.
$border = 0,
$ln = 0,
$fill = false,
$reseth = true,
$align = '',
$autopadding = true
);
}
by calling $pdf->_footer($footer); you create your own footer
You can create custom Header and Footer by exporting TCPDF in class and you use that class anywhere!
class MYPDF extends TCPDF {
//Page header
public function Header() {
//Header code
}
// Page footer
public function Footer() {
//Footer code
//you have $this->PageNo() where you can add conditional UI
}
}
And you can use this class as
$o_pdf = new MyPDF([all params TCPDF requires]);
I hope this will help you! Happy coading!!!

Page break for new page using fpdf

I want to create second page using fpdf.The code is given below.In the code from the specified area i want to start new page.Here the pdf class is extends from fpdf.
class PDF extends FPDF
{
function header{
$this->Ln(20);
$this->SetFont('Times','',10);
$this->Cell(0,10,'Kondotty');
$this->Ln(10);
$this->SetFont('Times','',10);
$this->Cell(80,0,'07/10/2014');
$this->Cell(60,0,'Seal');
$this->Cell(0,0,'Head of Institution');
//END FIRST PAGE
// STARTING SECOND PAGE
$this->Ln(10);
$this->Cell(27);
$this->Cell(0,10,'Her conduct and character were found ...................................... during that period.');
$this->Ln(20);
$this->SetFont('Times','',10);
$this->Cell(0,10,'Kondotty');
$this->Ln(10);
$this->SetFont('Times','',10);
$this->Cell(80,0,'07/10/2014');
}
}
For example:
I declare $i = 0; at the beginning of my PDF and for every line I add in my foreach I count $i like $i++.
If you want a pagebreak after x lines for example you can just say:
if($i%30==0 && $i!=0) {
$this->addPage();
}
And a function to add a new page:
private function addPage()
{
$this->page = $this->pdf->newPage('A4');
I hope this will point you in the right direction.

how to pass variable data from an array into fpdf in php

I'm trying to generate a pdf using the FPDF class but the problem I'm having is how to pass variable data fetched from mysql database (queried data are stored in an array) into the pdf generated.
Here are the codes in the script meant to generate the pdf
<?php
#include_once("includes/db.php");
require('fpdf/fpdf.php');
#include_once("includes/course_info_query.php");
$obj = new trainingCourses();
$course_details = array();
if(isset($_GET['c_id'])){
$course_details = $obj->getPubCourseDetails($_GET['c_id']);
}
class PDF extends FPDF
{
public $course_details;
public function setData($input){
$this->course_details = $input;
}
function Header()
{
// Logo
$this->Image('s_pdf_logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',20);
// Move to the right
$this->Cell(40);
// Title
$this->Cell(30,10,$this->course_details['comp_name']);
// Draw an header line
$this->Line(10,26,200,26);
// Line break
$this->Ln(20);
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Begin with regular font
$this->SetFont('Arial','',9);
// Then put a blue underlined link
//$this->SetTextColor(0,0,255);
$this->SetFont('','U');
$this->Write(10,$this->course_details['comp_name'],'http://www.skills.com');
// Arial italic 8
$this->SetFont('Arial','I',9);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
}
function BodyTop()
{
// Course title cell
$this->Cell('',10,'',0,0,'C');
$this->Ln();
/* Build cells for the first row */
$this->SetFont('Arial','',10);
$this->SetY(40);
// First Row
$this->Cell(35,8,'Starting Date : ',0,0,'L');
$this->Cell(30,8,$this->course_details['event_date'],0,0,'C');
$this->SetX(150);
$this->Cell(25,8,'Course Fee : ',0,0,'L');
$this->Cell(20,8,$this->course_details['fee'],0,0,'C');
$this->Ln();
// Second Row
$this->Cell(35,8,'Seating Capacity : ',0,0,'L');
$this->Cell(30,8,$this->course_details['sit_capacity'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'Duration : ',0,0,'L');
$this->Cell(20,8,$this->course_details['duration'].' Day(s)',0,0,'L');
$this->Ln();
// Third Row
$this->Cell(35,8,'Venue : ',0,0,'L');
$this->Cell(30,8,$this->course_details['venue'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'City : ',0,0,'L');
$this->Cell(20,8,$this->course_details['city'],0,0,'L');
$this->Ln();
// Fourth Row
$this->Cell(35,8,'Other Details : ',0,0,'L');
$this->Cell(150,8,$this->course_details['other_det'],0,0,'L');
$this->Ln();
}
function CourseBody()
{
$this->SetY(80);
//$this->WriteHTML($html);
$this->Write(10,$this->course_details['desc']);
}
function PrintChapter()
{
$this->AddPage();
$this->BodyTop();
$this->CourseBody();
}
}
$pdf = new PDF();
$pdf->setData($course_details);
//$pdf->Header();
$pdf->SetAuthor($this->course_details['comp_name']);
$pdf->PrintChapter();
$pdf->Output();
?>
I hope to get some help with this...Thanks
It's just basic OOP (object oriented programming) really.
Add a variable to your PDF Class as such:
public $data;
Add a function within your PDF Class which accepts a parameter and use this function to set the variable above:
public function setData($input){
$this->data = $input;
}
And then you'll be able to access $this->data from within your PDF Class. Don't forget to actually call the function that you just defined (just after the constructor).
EDIT:
To set the data:
$pdf = new PDF();
$pdf->setData($course_details);
Then within your PDF class $this->data will be your array. You might want to do the following so you can understand the format of your array:
print_r($this->data);

Categories