Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using FPDF To Create a PDF File From MySQL Database Entries

Posted on 2009-04-01
5
Medium Priority
?
10,355 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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