PHP PDF Generation - Your thoughts?

Hey guys,

I need to create some PDF files on the fly on our Virtual Private Server (located at Hostgator). I just tried to do some PDF examples from the PHP website itself and through the error messages I have received I have concluded that PHP support isn’t enabled by default on our VPS (should it have been?)

So now I come to Sitepoint to ask for tips on how to get this happening on our VPS. Any tips in the right direction would be a great help.

Perhaps someone could tell me if this is possible to do on a VPS.
PHP: Installation - Manual

I’m pretty sure it has been around since PHP4 :slight_smile:

Ok awesome ! I shall try that. Any tips or suggestions before I start?

Not really, no. FPDF is pretty self explanatory :slight_smile:

I would create your own extension classes though


class MyFPDF extends FPDF
{
}

just so you can easily add functionality later :slight_smile:

I always make the classes abstractions from what I want them to do, so for example


class InvoiceFPDF extends FPDF
{
   public function Header() // overwrites FPDF->Header
   {
       // ...
   }

   public function Footer() // overwrites FPDF->Footer
   {
       // ...
   }
   
   public function addRow($description, $price)
   {
      // ...
   }

   public function addTotals($exclVAT, $VAT)
   {
      // ...
   }
}

So you get an easy interface, like


$invoice=new InvoiceFPDF;

$exclVAT=$VAT=0;
foreach($myInvoice->rows as $row)
{
  $invoice->addRow($row['description'], $row['price']);
  $exclVAT+=$row['price'];
}
$VAT = 0.21 * $exclVAT;
$invoice->addTotals($exclVAT, $VAT);

It makes the PHP a lot easier to read because you don’t have to tell FPDF on a detailed level what to do every time; you just let the class figure that out.
Of course that’s just me, you’re free to do whatever you wish :slight_smile:

Nice tips man. Thank you. I didn’t know PHP could extend class support like that. Is that fairly new? I thought that was specifically a java thing.

I was referring to PDF in PHP support lol. PHP support is definitely working.

The PDF functions for PHP require you buy a license (or use the Lite version which doesn’t contain all functionality) and I don’t think I ever saw a host who had it enabled.

You could ask your hoster to install it, but I highly doubt they will. You can always try though, right? :slight_smile:

If they don’t want to install it, there’s always FPDF. It’s a bit old but still works a charm.

You should contact your host first and see why PHP isn’t enabled.
It should be.

Did you do a small php test, by creating a file like this:

<?php
echo “test”;
?>