Solved

Using FPDF To Create a PDF File From MySQL Database Entries

Posted on 2009-04-01
5
7,588 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now