Generate PDF with PHP

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
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Generate PDF with PHP

Post by klevis miho »

How can I generate a pdf file with php?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Generate PDF with PHP

Post by greyhoundcode »

Two options that come to mind are FPDF and PDFLib.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Generate PDF with PHP

Post by klevis miho »

Are those in the core php? Or should I download them?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Generate PDF with PHP

Post by greyhoundcode »

FPDF is a PHP class, so all you would need to do is download it and include/require it. PDFLib is an extension (its available on PECL I think) so unless it's already set up as part of your installation you'd need to install it.

Also, there is a free version of PDFLib but the main dish itself is commercial.

See the links in my first post for more info.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Generate PDF with PHP

Post by klevis miho »

I found that Joomla has a TCPDF library in it's core installation (because I'm using joomla).
And that this TCPDF is an extended version of FPDF.

Will try it, and give my opinion.

Thanks.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Generate PDF with PHP

Post by internet-solution »

Other option is mPDF. It can generate PDF from HTML / css + UTF-8 supported. However, it can be very slow compared to FPDF.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Generate PDF with PHP

Post by greyhoundcode »

internet-solution wrote:Other option is mPDF. It can generate PDF from HTML / css + UTF-8 supported. However, it can be very slow compared to FPDF.
I wasn't aware of that one, will need to check it out. Also, for the reference of anyone who is interested, another useful set of classes are found in the FPDI project, which extends FPDF so that existing PDF files can be loaded and edited.
tungt84
Forum Newbie
Posts: 2
Joined: Thu Mar 18, 2010 2:53 am

Re: Generate PDF with PHP

Post by tungt84 »

I think TCPDF is good. TCP support UTF-8 Unicode, Xhtml, css :D
Here is a sample.

Code: Select all

//Include lib
require_once('../config/lang/eng.php');
require_once('../tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 021');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
...................
// set font
$pdf->SetFont('helvetica', '', 9);

// add a page
$pdf->AddPage();

$html='some html content here';
//Write html content
$pdf->writeHTML($html, true, 0, true, 0);
...............................................
//Close and output PDF document
$pdf->Output('example_021.pdf', 'I');
full sample herehttp://www.megaupload.com/?d=TDQV7W2A
Post Reply