Back to Top
The nuts and bolts for
Creative web development

Create PDF with PHP

Question:

How to generate PDF file from MySQL table with PHP?

Answer:


The PDF extension of PHP (PDFlib) provides the required functionality for generating PDF files.

The following sample code demonstrates how a PDF file can be created and filled with information retrieved from database:

<?php require_once('Connections/cnnTest.php'); ?>
<?php
// select data from database for the PDF
mysql_select_db($database_cnnTest, $cnnTest);
$query_rsReportData = "SELECT * FROM categories";
$rsReportData = mysql_query($query_rsReportData, $cnnTest) or die(mysql_error());
$row_rsReportData = mysql_fetch_assoc($rsReportData);
$totalRows_rsReportData = mysql_num_rows($rsReportData);
?>
<?php
// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "");

// Set Info
pdf_set_info($pdf, "Author", "Georgi Kralev");
pdf_set_info($pdf, "Title", "Report");
pdf_set_info($pdf, "Creator", "Georgi Kralev");
pdf_set_info($pdf, "Subject", "Report");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// path of your TTF font directory
$fontdir = "C:\WINDOWS\Fonts";

// Open .TTFs (true type fonts)

pdf_set_parameter($pdf, "FontOutline", "ArialItalic=$fontdir\ariali.ttf");
pdf_set_parameter($pdf, "FontOutline", "ArialBold=$fontdir\ARIALBD.TTF");
pdf_set_parameter($pdf, "FontOutline", "Arial=$fontdir\ARIAL.TTF");

// ------ Start output of the PDF Content ------//
// set the font - Arial Bold 15

$font = pdf_findfont($pdf, "ArialBold", "host",0); pdf_setfont($pdf, $font, 15); 
// output document title
pdf_show_xy($pdf, "Categories Report", 50, 788);
// draw a line
pdf_moveto($pdf, 20, 780);
pdf_lineto($pdf, 575, 780);
pdf_stroke($pdf);

// set the font - Arial Italic 12
$font = pdf_findfont($pdf, "ArialItalic", "host",0); pdf_setfont($pdf, $font, 12);
$y = 750;
// output data header
pdf_show_xy($pdf, "Category:", 50, $y);
$y -= 5;

// set the font - Arial 10
$font = pdf_findfont($pdf, "Arial", "host",0); pdf_setfont($pdf, $font, 10);

// output the data from Database
do
{
      $y -= 15;
        pdf_show_xy($pdf, $row_rsReportData['name'], 50, $y);
}
while
($row_rsReportData = mysql_fetch_assoc($rsReportData));

// ------ End output of the PDF Content ------//

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);

$buf = pdf_get_buffer($pdf);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=report.pdf");
echo $buf;

pdf_delete($pdf);
?>
<?php
mysql_free_result($rsReportData);
?>
 

My connection file is called cnnTest.php. The table from which I get the data is called categories.
I use the PDF extension functions to create the pdf, fill document info, set the fonts and write the information from the column name of table categories.

Note:
1. In order the code to work, the following line in your php.ini has to be uncommented:
extension=php_pdf.dll
2. For more information about PDFlib library check: PDF Functions

Georgi Kralev

Georgi Kralev is a native of Bulgaria and graduated from Technical University Sofia with a Master’s Degree in Computer systems and technologies.

Starting his professional career as desktop developer at Delphi, he moved into the field of .NET development (ASP.NET, windows forms, console applications, windows services, and web services). He also has experience with the following technologies: classical ASP, PHP, HTML / DHTML, XML, JavaScript).

He has received the EUR ING professional title from the European Federation of National Engineering Associations (FEANI)

He has achieved the following professional certificates:
• Microsoft Certified Professional Developer (MCPD) Enterprise Application Developer and Microsoft Certified Professional.
• Several Brainbench certificates in software development.
• Cisco Certified Network Associate

Personal web site for C# and .NET programming:
http://gdkralev.googlepages.com

See All Postings From Georgi Kralev >>

Share this Article

Comments

If there is more records not enough for 1 page ??

Please , lets think, If there is more category name (around 1000 records), Then according to current coding, Only One page printing. other not visible or not go to next page...

What is the additional coding that need to add above script if there is more records (eg: 1000) ?

 pelase

RE: RE: RE: RE: Fixed

I have,

ASP and or PHP to PDF is what coders are always looking for.
It's impossible to try and re-invent the wheel.

Cheers

 

RE: RE: RE: Fixed

If you find this FAQ useful you can rate it.
This helps us very much to determine what kind of Articles and FAQs the visitors of DMXzone.com want to read and appreciate.

RE: RE: Fixed

By the way, this example opens up so many new possibilities for my coding.

Now I can add Link to PDF along with the Printer friendly & Email this article.

Thank you!

See all 7 Comments