Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using FPDF To Create a PDF File From MySQL Database Entries

Posted on 2009-04-01
5
8,341 Views
Last Modified: 2013-12-12
Hey guys,

I have absolutely no knowledge of how to use FPDF, so I'm hoping that someone can guide me through a step-by-step process of writing the PHP code to create a PDF from entries in a MySQL database.  Here's a picture of how I'd like the file to look:

http://i39.tinypic.com/99dkpz.jpg

Is it possible to generate a table like this?  The header can be text or an image (whichever is easier) and the data in the table will extend for however many entries  there are in the database.

Thanks in advance!
0
Comment
Question by:tcra
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:dischob
ID: 24041602
The picture link doesn't work ...
0
 

Author Comment

by:tcra
ID: 24041741
Sorry about that.  I've uploaded the file below.  Hope it works for you.
pdf.jpg
0
 
LVL 1

Accepted Solution

by:
dischob earned 500 total points
ID: 24042211
See code snippet.
Maybe it needs some fine tuning, but it gives an idea of how it works...

Remark:
If you have a header / footer that should return on every page, extend the FPDF class...

MYPDF extends FPDF
{
     function header()
    {
        ...
    }

    //same for footer - instead of $pdf-> you use $this->
}
<?php
$pdf = new FPDF();
$pdf->open();
$pdf->AddPage();
$pdf->SetAutoPageBreak(false);
$pdf->SetFillColor(0, 0, 0); //black
$pdf->SetDrawColor(0, 0, 0); //black
 
//header 1
$pdf->setXY(10, 20);
//Set the font you want. To add a font look at tutorial 7 on fpdf.org
$pdf->setFont("times","B","18");
$pdf->Cell(190, 20, "PDF Output Header", 1, 0, "C", 1);
 
//header 2
$pdf->setXY(10, 40);
$pdf->setFont("times","B","16");
$pdf->Cell(190, 20, "Sub-heading 1", 1, 0, "C", 1);
 
//header 3
$pdf->setXY(10, 60);
$pdf->setFont("times","B","14");
$pdf->Cell(190, 20, "Sub-heading 1", 1, 0, "C", 1);
 
//table header
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("times","B","12");
$pdf->setXY(10, 80);
$pdf->Cell(80, 10, "Database Field 1", 1, 0, "C", 1);
$pdf->Cell(60, 10, "Database Field 2", 1, 0, "C", 1);
$pdf->Cell(25, 10, "Database Field 3", 1, 0, "C", 1);
$pdf->Cell(25, 10, "Database Field 4", 1, 0, "C", 1);
 
//gegevens van database
$y = $pdf->GetY();
$x = 10;
$pdf->setXY($x, $y);
 
$sql = ...
query ...
while (row ...)
{
    	$pdf->Cell(80, 10, $row..., 1);
	$pdf->Cell(60, 10, $row..., 1);
	$pdf->Cell(25, 10, $row..., 1);
	$pdf->Cell(25, 10, $row..., 1);
	
	$y += 10;
	
	if ($y > 180)
	{
		$pdf->AddPage();
		$y = 20;
	}
	
	$pdf->setXY($x, $y);
}
 
$pdf->Output();

Open in new window

0
 

Author Closing Comment

by:tcra
ID: 31565404
Great solution, thanks a lot!  A few tweaks and it's good to go.
0
 

Expert Comment

by:awl100
ID: 24467256
Many thanks for the code above, it got me to a final solution. I have posted it with some fixes I had to implement to get it to work on my system, some fine tuning and finally some extra features. Principle is the same, with the following enhancements:

- Calls the fpdf.php class.
- Uses headers and footers to allow you to format your pages with a image in the header and pages numbers in the footer (as per fpdf method)
- Sets PDF document properties
- Has report date
- Tidied up formatting, font sizes and table sizes
- Uses a call to the database connection script.
- Adds the code for the while loop
- Adds the row request codes.
- Updated page breaks position

Could probably be improved even further...but wanted to share this working solution.



<?php
 
require('fpdf.php');
 
class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image('logo.png',10,8,33);
   
}
 
//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 = new PDF();
$pdf->open();
$pdf->AddPage();
$pdf->AliasNbPages();   // necessary for x of y page numbers to appear in document
$pdf->SetAutoPageBreak(false);
 
// document properties
$pdf->SetAuthor('INSERT AUTHOR');
$pdf->SetTitle('INSERT DOC TITLE');
 
$pdf->SetFont('Arial','B',14);
$pdf->Cell(40,10,'                        TITLE FOR REPORT');
 
// Add date report ran
$pdf->SetFont('Arial','I',10);
$date =  date("F j, Y");
$pdf->Cell(40,30,'Report date: '.$date);
 
$pdf->SetDrawColor(0, 0, 0); //black
 
//table header
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40); 
$pdf->Cell(40, 10, "Class Date", 1, 0, "L", 1);   // CHANGE THESE TO REPRESENT YOUR FIELDS
$pdf->Cell(18, 10, "Class Hrs", 1, 0, "L", 1);
$pdf->Cell(25, 10, "Teacher Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Location Name", 1, 0, "L", 1); 
$pdf->Cell(55, 10, "Comment", 1, 0, "L", 1); 
 
$y = 50;
$x = 10;  
 
$pdf->setXY($x, $y);
 
$pdf->setFont("Arial","","9");
 
require_once('../../Connections/YOURCONNECTSCRIPT.php');  // configure to point to your connection script.
mysql_select_db($database_YOURDBNAME, $YOURHANDLE);
$query_result = "SELECT * FROM YOURTABLEHERE"; 
$result = mysql_query($query_result, $blueocean) or die(mysql_error());
 
while($row = mysql_fetch_array($result))
{
        $pdf->Cell(40, 8, $row['class_date'], 1);   // CHANGE THESE TO REPRESENT YOUR FIELDS
        $pdf->Cell(18, 8, $row['class_hrs'], 1);
        $pdf->Cell(25, 8, $row['teacher_name'], 1);
        $pdf->Cell(30, 8, $row['location_name'], 1);
        $pdf->Cell(55, 8, $row['comment'], 1);
 
        $y += 8;
        
        if ($y > 260)    // When you need a page break
		{
            $pdf->AddPage();
            $y = 40;
			
		}
        
        $pdf->setXY($x, $y);
}
 
$pdf->Output();

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question