how to pass variable data from an array into fpdf in php - 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);

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

While printing data using fpdf library Data is not passing in codeigniter

I Have form in that i am displaying data.When I click on the print button I am going to save the data in pdf format for printing for that I am using FPDF Library.The problem is when i send the data from the controller to view only headers displaying like below But Data is not displaying. I have written my view file like below.I have got stuck since yesterday.I Have getting the data in controller but not passing to the view.I can't understand why it is happening .The Way i am doing wrong or what please any help Would be Appreciated.Thanks in Advance.
My controller Code
public function printpage(){
$this->load->library('Myfpdf');
$data['result']=$this->Vendor_Model->displaydata();
if(!empty(array_filter($data))){
$this->load->view('Savedata',$data);
}else{
redirect('Vendorcontroller/showeditview');
}
}
My Modal code for getting the data from the database
public function displaydata(){
$query=$this->db->query('SELECT `vndr_id`, s.state as state,`vndr_name`, `vndr_address`, `vndr_pincode`, `vndr_telephone`, `vndr_mobile`, `vndr_mailid`, `vndr_country`, `vndr_gsttin`, `vndr_cstno`, `vndr_totaldebit`, `vndr_totalcredit`, `vndr_bankname`, `vndr_acno`, `vndr_ifsccode` FROM `vendors` vndr INNER JOIN states s ON vndr_state=s.state_id order by vndr_id');
if($query->num_rows()>0){
return $query->result_array();
}
}
My view code for print the data as pdf format
class printview extends FPDF
{
function Header()
{
$this->SetFont('Arial','B',15);
$this->Cell(276,5,"VendorDetails",0,0,'C');
$this->ln(15);
$this->SetFont('Arial','B',10);
$this->Cell(55,10,"Name",1,0,'C');
$this->Cell(90,10,"Address",1,0,'C');
$this->Cell(50,10,"Telephone",1,0,'C');
$this->Cell(70,10,"Email",1,0,'C');
$this->ln();
}
function Footer(){
$this->SetY(-15);
$this->SetFont('Arial','I',8);
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
function viewdata(){
if(isset($result)){
foreach($result as $values){
$this->SetFont('Arial','',10);
$this->Cell(55,10,$values['vndr_name'],1,0,'L');
$this->Cell(90,10,$values['vndr_address'].','.$values['state'].'-'.$values['vndr_pincode'],1,0,'L');
$this->Cell(50,10,$values['vndr_telephone'].','.$values['vndr_mobile'],1,0,'L');
$this->Cell(70,10,$values['vndr_mailid'],1,0,'L');
$this->ln();
}
}
}
}
$this->pdf = new Printview();
$this->pdf->SetMargins(15, 10, 20);
$this->pdf->AliasNbPages();
$this->pdf->AddPage('L','A4',0);
$this->pdf->viewdata();
$this->pdf->Output();
This is Fdf library class
require('fpdf/fpdf.php');
class Myfpdf extends fpdf
{
function __construct(){
parent::__construct();
$ci=& get_instance();
}
}
Try to pass the $result data as a parameter :
$this->pdf->viewdata($result);
And also on the method :
function viewdata($result){
...
}
I Have changed this way it is working fine now.
This is controller
-------------------------
public function printpage(){
$this->load->library('Myfpdf');
$data['result']=$this->Vendor_Model->displaydata();
if(!empty(array_filter($data))){
$this->load->view('Savedata',$data);
}else{
redirect('Vendorcontroller/showeditview');
}
}
This is view file
function viewdata($result){
if(isset($result)){
foreach($result as $values){
$this->SetFont('Arial','',10);
$this->Cell(55,10,$values['vndr_name'],1,0,'L');
$this->Cell(90,10,$values['vndr_address'].','.$values['state'].'-'.$values['vndr_pincode'],1,0,'L');
$this->Cell(50,10,$values['vndr_telephone'].','.$values['vndr_mobile'],1,0,'L');
$this->Cell(70,10,$values['vndr_mailid'],1,0,'L');
$this->ln();
}
}
}
}
$this->pdf = new Printview();
$this->pdf->SetMargins(15, 10, 20);
$this->pdf->AliasNbPages();
$this->pdf->AddPage('L','A4',0);
$this->pdf->viewdata($result);
$this->pdf->Output();

How to divide page in two column FPDF

I want to divide page in to two column using FPDF. I am using below code which created pdf in two cloumn but issue is that the gap is some bit more between column. I want to reduce gap between the column only.
My code here
class PDF extends FPDF
{
protected $col = 0; // Current column
protected $y0; // Ordinate of column start
function Header()
{
}
function Footer()
{
}
function SetCol($col)
{
// Set position at a given column
$this->col = $col;
$x = 10+$col*50;
$this->SetLeftMargin($x);
$this->SetX($x);
}
function AcceptPageBreak()
{
if($this->col<2)
{
// Go to next column
$this->SetCol($this->col+2);
// Set ordinate to top
$this->SetY($this->y0);
// Keep on page
return false;
}
else
{
// Go back to first column
$this->SetCol(0);
// Page break
return true;
}
}
function ChapterBody($file)
{
$txt = file_get_contents($file);
$this->SetFont('Times','',12);
$this->MultiCell(100, 7, $txt);
$this->Ln();
$this->SetCol(0);
}
function PrintChapter($num, $title, $file)
{
// Add chapter
$this->AddPage();
$this->ChapterBody($file);
}
}
$pdf = new PDF();
$title = '20000 Leagues Under the Seas';
$pdf->SetTitle($title);
$pdf->SetAuthor('Jules Verne');
$pdf->PrintChapter(1,'A RUNAWAY REEF','20k_c1.txt');
$pdf->Output();
In the below image you can see
You are skipping to third column. See bellow with two columns per page:
function AcceptPageBreak()
{
if($this->col<1)
{
// Go to next column
$this->SetCol($this->col+1);
// Set ordinate to top
$this->SetY($this->y0);
// Keep on page
return false;
}
else
{
// Go back to first column
$this->SetCol(0);
// Page break
return true;
}
}

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

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.

Categories