Page break for new page using fpdf - php

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.

Related

SOLVED - fpdf header from the second page

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?

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);

How to remove Underline from anchor link of a pdf in fpdf

we are making a pdf report by using FPDF in which we have links.
our problem is that we are not able to remove underline and default color from the link we have sent in the pdf report. and we want to set a custom different color on different links.
below is what we are making.
$text22 = preg_replace('/\S*\b('. $searchphrase[$rr] .')\b\S*/i', '$1', $aaa);
$text22 = preg_replace('/\S*\b('. $searchphrase[$rr] .')\b\S*/i', '$1', $aaa);
$pdf->WriteHTML(utf8_decode($main));
below is our pdf report, now we have to remove underline from from the link and set custom color on that.
You may have to extend the FPDF class yourself and change the underline / colour in the PutLink function.
http://www.fpdf.org/en/script/script50.php
Edit
Here's a code sample of extending the FPDF class. In fact, as the WriteHTML function isn't in the FPDF class, this extends the class which has it in instead. This is one of many ways to make it work. In this example you have to specify the link colour in an additional attribute (data-color), as the class can't read CSS rules. You could of course write a regex to parse the CSS, and then translate the colours into r,g,b. But for a simpler example, I left that out.
<?php
// This class extends the Tutorial 6 class, which in turn extends the main FPDF class
class XPDF extends PDF
{
protected $clr = "";
function OpenTag($tag, $attr)
{
parent::OpenTag($tag, $attr);
if ($tag == "A")
{
if (isset($attr['DATA-COLOR']))
{
$this->clr = $attr['DATA-COLOR'];
}
else
{
$this->clr = "";
}
}
}
function CloseTag($tag)
{
parent::CloseTag($tag);
if ($tag == "A")
$this->clr = "";
}
function PutLink($URL, $txt)
{
// Put a hyperlink
if ($this->clr != "")
{
$clrs = explode(",", $this->clr);
$this->SetTextColor($clrs[0], $clrs[1], $clrs[2]);
}
else
{
$this->SetTextColor(0,0,255);
}
$this->SetStyle('U',true);
$this->Write(5,$txt,$URL);
$this->SetStyle('U',false);
$this->SetTextColor(0);
}
}
$html = 'This is some text and here is a link. To specify the pdf colour of a link, you need to specify it as RGB numbers in a data-attribute, like this or this.';
$pdf = new XPDF();
// First page
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->WriteHTML($html);
$pdf->Output();
?>

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()
{
}
}

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