#native_company# #native_desc#
#native_cta#

Creating and Manipulating PDFs with PHP and FPDF Page 2

By W. Jason Gilmore
on March 10, 2011

Adding Images

To add an image to your PDF file, use the Image() method, as demonstrated here:

<!--p

  require 'fpdf.php';

  $pdf=new FPDF('P', 'pt', 'A4');
  $pd-->AddPage();

  $pdf->SetFont('Times', 'B', 16);
  $pdf->Cell(0,10,'Easy PayPal with PHP', 0, 2, 'C');

  $pdf->Image('easypaypalwithphp.jpg');

  $pdf->Output();

?>

The Image() method is surprisingly powerful in the sense that although it accepts quite a few input parameters, all are optional except for the image file name. It will automatically determine the image’s dimensions and image type (JPEG or PNG for instance), however you can override these defaults using other available parameters. See the Image() documentation for all the details.

Adding a Watermark

When making sensitive or copyright material available to customers, it’s often useful to include a watermark, which identifies the individual downloading the material. I call this SRM, or Shame Rights Management, because if the customer ever decides to upload the material to a file sharing service, he does so knowing that his name, e-mail address, or other identifying details are embedded in the document.
You can easily embed information in the footer of every generated PDF page using a slick FPDF feature, which merely requires you to extend the FPDF class and override the Footer() method, within it defining what you would like to appear in the page footer. FPDF will automatically detect this method and execute it each time a new page is created. For instance, the following example will embed the e-mail address [email protected] into the PDF footer:

<!--p

  require 'fpdf.php';

  class WJGPDF extends FPDF
  {

    function Footer()
    {

      $thi-->SetY(-25);

      $this->SetFont('Times', 'B', 12);  

      $this->Cell(0,20,'Licensed to [email protected]', 0, 0, 'C');

    }

  }

  $pdf=new WJGPDF('P', 'pt', 'A4');
  $pdf->AddPage();

  $pdf->SetFont('Times', 'B', 16);
  $pdf->Cell(0,10,'Easy PayPal with PHP', 0, 2, 'C');

  $pdf->Output();

?>

Conclusion

As you can see, the FPDF library packs quite a punch, and I’ve hardly scratched the surface in terms of what it can do! Be sure to check out the FPDF website for tutorials, documentation, and other useful information.

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including “Easy PHP Websites with the Zend Framework”, “Easy PayPal with PHP”, and “Beginning PHP and MySQL, Fourth Edition”. Follow him on Twitter at @wjgilmore.