Dynamically generate PDF

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
woot
Forum Newbie
Posts: 8
Joined: Tue Feb 19, 2013 8:35 am

Dynamically generate PDF

Post by woot »

Hello there.

I'm trying to find a way to export the content of a MySQL table to PDF format but the main issue I'm facing is trying to do it dynamically. I'm following the logic of the 5th tutorial provided in the Tutorials section on the site but I can't seem to be able to achieve it. Is there a way to do it efficiently with FPDF?

Here's the code I'm using:

(excluding the the contents of the ImprovedTable and FancyTable methods, which are basically the same as the ones provided in the example)

Code: Select all

//Simple table 
function BasicTable($header,$data) 
{ 
  //Header 
  $w=array(20,30,55,25,20,20); 
  //Header 
  for($i=0; $i<count($header); $i++) 
  $this->Cell($w[$i],7,$header[$i],1,0,'C'); 
  $this->Ln(); 
  //Data 
  foreach ($data as $eachResult) 
  { 
    $this->Cell(20,6,$eachResult["id"],1); 
    $this->Cell(30,6,$eachResult["Name"],1); 
    //This is where my problem lies. Is there a way not to have to hardcode column names like this in the array? 
  } 
} 

 $pdf=new PDF(); 
 //Column titles 
 //$header = array('id','Name'); 
 $header = array(); //creating an empty array to be filled with field names retrieved from MySQL query executed below 

 //Data loading 

 //*** Load MySQL Data ***// 
 $objConnect = Database::getConnection(); 
 $query = "SELECT * FROM users"; 
 $objQuery = $objConnect->query($query); 
 $colNames = $objQuery->fetch_fields();
 //filling in $header array with column names (this part works and renders correctly on the PDF file) 
 foreach ($colNames as $colName) 
 { 
  $header[] = $colName->name; 
 }  
 $resultData = array(); 
 for ($i=0; $i<$objQuery->num_rows; $i++) 
 { 
  $result = $objQuery->fetch_array(); 
  array_push($resultData,$result); 
 }  
 //************************// 
 $pdf->SetFont('Arial','',10); 

 //*** Table 1 ***// 
 $pdf->AddPage(); 
 //$pdf->Image('logo.gif',80,8,33); //I'm not using a logo so far, so I commented this line 
 $pdf->Ln(35); 
 $pdf->BasicTable($header,$resultData); 

 //Prompt user to download 
 $pdf->Output("export.pdf","D"); 
Also, I'd like to know if it's possible to replace the name of the table (users in this snippet) with a variable like $currentTable for example, making it work for whatever table is selected at the moment?

Thanks in advance (and I hope someone has the patience to read all this).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Dynamically generate PDF

Post by Christopher »

I don't see how that code would work unless you did this:

Code: Select all

//Simple table 
class MyFPDF extends FPDF
{
function BasicTable($header,$data) 
{ 
  //Header 
  $w=array(20,30,55,25,20,20); 
  //Header 
  for($i=0; $i<count($header); $i++) 
  $this->Cell($w[$i],7,$header[$i],1,0,'C'); 
  $this->Ln(); 
  //Data 
  foreach ($data as $eachResult) 
  { 
    $this->Cell(20,6,$eachResult["id"],1); 
    $this->Cell(30,6,$eachResult["Name"],1); 
    //This is where my problem lies. Is there a way not to have to hardcode column names like this in the array? 
  } 
} 
}
 $pdf=new MyPDF(); 
(#10850)
User avatar
woot
Forum Newbie
Posts: 8
Joined: Tue Feb 19, 2013 8:35 am

Re: Dynamically generate PDF

Post by woot »

I didn't include that part in the paste but it's there because otherwise it would be, as you pointed out, impossible to instantiate an object of said class.
The problem is I'd like to get it working dynamically regardless of the number of columns, meaning I wouldn't have to hardcode the name of each key in the array to be output.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Dynamically generate PDF

Post by requinix »

Code: Select all

  foreach ($data as $eachResult) 
  { 
    $this->Cell(20,6,$eachResult["id"],1); 
    $this->Cell(30,6,$eachResult["Name"],1); 
    //This is where my problem lies. Is there a way not to have to hardcode column names like this in the array? 
  } 
If $eachResult is itself an array then you can foreach over it too.

Code: Select all

foreach ($eachResult as $key => $value)
  {
    $this->Cell($i * 10, 6, $value, 1);
  }
Set $i=2 to start at 20.
User avatar
woot
Forum Newbie
Posts: 8
Joined: Tue Feb 19, 2013 8:35 am

Re: Dynamically generate PDF

Post by woot »

Thank you, requinix.
I was able to (kind of) solve the problem using a different approach to produce a similar outcome to the code you provided.
Post Reply