Creating PDFs with PHP

Hi Experts,

Can anyone tell me a good and easy way to create PDFs through PHP?

For the majority, I'll be needing to create PDFs for reports based on a database query. The reports would consist of columns, rows, and row groupings.

I've came across http://www.fpdf.org/, but just reading their site, it says that it can be slower than PDFlib (I'm not sure what that is), and it doesn't provide HTML to PDF support. I'm not afraid of writing longer code, but does any one have any experience with fpdf, and knows if it is truly slow with more complex and lengthy reports?  What about PDFlib or others?

Thank you,.
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
I've used FPDF extensively. It is not slow at all, in my opinion. It might be slowER and more noticeable if you're generating massive amounts of reports that are huge, but I've easily generated 50+ page database-driven PDFs in seconds.

I should also mention that FPDF isn't really under active development anymore (at least not last time I checked). There's another project, TCPDF, which is pretty much the next generation of FPDF and is in more active development.
0
APD TorontoSoftware DeveloperAuthor Commented:
Have you used TCPDF, is it fast?
0
gr8gonzoConsultantCommented:
Yes, I've used both. It's pretty much the same level of performance. Unless you're doing something extremely complex, or unless you're on really slow hardware, you probably won't be able to tell a difference between the various libraries.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

APD TorontoSoftware DeveloperAuthor Commented:
Also (sorry), do you know if this will work under IIS?  I'm running PHP 5.6.5 under IIS7.5
0
gr8gonzoConsultantCommented:
Yes, it should. There are several examples in the TCPDF documentation that you should be able to run quickly to see what kind of things you can generate.
0
Ray PaseurCommented:
Here is my experimental example using TCPDF.  There are many parts of it that I do not understand (and that's why I usually choose FPDF instead).
<?php // demo/tcpdf_image_position.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// SYNTHESIZE THE PDF FILE NAME
$pdf_file_link    = 'storage/tcpdf_example.pdf';
$pdf_file_name    = getcwd() . DIRECTORY_SEPARATOR . $pdf_file_link;

// THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// EXTEND THE TCPDF OBJECT
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC HEADER AND FOOTER
    public function Header() {}
    public function Footer() {}
}

echo "<br />Starting PDF creation" . PHP_EOL;

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(0,0,0,TRUE);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);

// DO NOT DO THIS
// $pdf->setFillColor(255,255,224);


// CELL BORDERS, IF NEEDED
$bdr = '1TRBL';

// IF NO CELL BORDER IS NEEDED
$bdr = 0;

// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

// ADD AN IMAGE
// http://www.tcpdf.org/doc/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352
$img = 'images/ray_padded.png';

// I HAVE NO IDEA WHAT THIS IS DOING TO IMAGE SCALE - EXPERIMENT WITH IT
$pdf->setImageScale(1.53);

// GET THE IMAGE, I HOPE
$pdf->Image
( $img
, 0             // $x
, 0             // $y
, 0             // WIDTH
, 0             // HEIGHT
, 'PNG'         // TYPE
, '#'           // LINK URL
, 'T'           // SET POINTER TOP LEFT
, FALSE         // NO RESIZING
, 300           // DPI
, 'L'           // PALIGN
, FALSE         // ISMASK
, FALSE         // IMGMASK
, 0             // BORDER
, FALSE         // FIT TO BOX
, FALSE         // HIDDEN
, FALSE         // FIT ON PAGE
)
;

// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24            // $x,
, 24            // $y,
, 'HELLO WORLD'
, FALSE         // $fstroke           = false,
, FALSE         // $fclip             = false,
, TRUE          // $ffill             = true,
, $bdr          // $border            = whatever,
, 2             // $ln                = 0, 2=PUT CURSOR BELOW
, ''            // $align             = '', DEFAULT LEFT
, FALSE         // $fill              = false, CELL BACKGROUND
, ''            // $link              = '', NOT A LINK
, 0             // $stretch           = 0, NO TEXT STRETCH
, FALSE         // $ignore_min_height = false,
, 'A'           // $calign            = 'T', A=FONT TOP INSIDE CELL
, 'T'           // $valign            = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE          // $rtloff            = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;

// WRITE THE PDF FILE TO THE SERVER
echo "<br />Writing PDF\n";
$pdf->Output($pdf_file_name, 'F');

// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE PDF
echo '<br /><a target="my_PDF" href="' . $pdf_file_link . '"><strong>Print the PDF</strong></a>';

Open in new window

0
skijCommented:
You could use ZendPdf for this.  ZendPdf is part of the Zend Framework, which can be installed using the Microsoft Web Platform Installer.
0
gr8gonzoConsultantCommented:
Ray, your example looks fine at first glance. What parts don't you understand? To me, the syntax for FPDF and TCPDF is nearly identical...
0
APD TorontoSoftware DeveloperAuthor Commented:
I downloaded TCPDF, installed it into my C:\inetpub\wwwroot\tcpdf, wwwroot is of course IIS's root folder, and all examples in .../tcpdf/examples work.

However, if I copy .../wwwroot/tcpdf/examples/example_001.php into .../www/myproject/testpdf.php and change line 28 from
require_once('tcpdf_include.php');

to
require_once('../tcpdf/examples/tcpdf_include.php');

I still get
Fatal error: Class 'TCPDF' not found in C:\inetpub\wwwroot\test\testPDF.php on line 31

My goal is to keep TCPDF into my root, so I can access it by multiple of projects.

Thank you
0
Ray PaseurCommented:
@gr8gonzo: I agree - they are very similar.  Probably just my prejudice and allocation of test time, but I found FPDF eaier for my work.  Some things, for example, the setImageScale() method, I just never figured out!
0
gr8gonzoConsultantCommented:
The tcpdf_include.php file also requires/includes files that are relative to the current path, so if you moved the files around, it's probably looking in the wrong path. Try changing that require line into this block of code:

$currentDir = getcwd();
chdir("../tcpdf/examples");
require_once('tcpdf_include.php');
chdir($currentDir);

That should temporarily change the working directory over to the examples directory, load the include properly (and all of its dependencies) and then change back to the current directory.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
@Ray - I never had to screw with setImageScale beyond setting it to 1.53 like most people seem to do. To me, it was all about the dpi, since the PDF is measured in units like millimeters, so setImageScale was defining how many pixels-to-millimeters, but that was just an assumption I made a while ago. I didn't really mess with it because the 1.53 setting always seemed to produce accurate results. I just extended the base class with all my defaults and then used that extended class as my go-to template for new PDFs.
0
APD TorontoSoftware DeveloperAuthor Commented:
Worked!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.