Home arrow PHP arrow Page 3 - Working with Images and Text Flows in PDF Files with PHP 5

Displaying a basic image - PHP

When it comes to developing database-driven web applications that deliver their contents to end users in a great variety of formats, PHP can be a killer scripting language. This becomes even more evident when these contents must be displayed through one or more PDF files, which have to be built dynamically before being sent to the client. So if you want to learn how to start building PDF documents in PHP 5, then you should begin reading this article now!

TABLE OF CONTENTS:
  1. Working with Images and Text Flows in PDF Files with PHP 5
  2. Listing the source code of some previous examples
  3. Displaying a basic image
  4. Displaying a basic text flow
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 13, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

According to the concepts that I deployed in the previous section, the PDFlib library provides PHP developers with some intuitive methods for including different types of images into a specific PDF file.

Initially, PHP 4 offered a decent variety of native methods aimed at working with images and PDF documents in conjunction. Unfortunately, many of them have been deprecated. If you're still using this version of PHP and your php.ini file has been set up for working with PDF files, you're completely free to use these methods in accordance with your personal needs.

In this case, though, I'm going to use only two methods for including images into a PDF document, called "load_image()" and "fit_image()" respectively, since they're pretty intuitive and easy to learn. However, you should take into account that these might not work as expected, depending on the respective versions of PHP and the "PDFlib" package that you have installed on your system. So be aware of this issue when building your PDF-related scripts.

Now that I have clarified that point, please look at the following example. It shows how to include a basic image into a specific PDF file. The pertinent code sample is as follows:

// example creating a basic PDF document and include a sample
image

  try {

// create new instance of the 'PDFlib' class

  $pdf=new PDFlib();

// open new PDF file

  if(!$pdf->begin_document("","")){

throw new PDFlibException("Error creating PDF document. ".$pdf-
>get_errmsg());

}

  $pdf->set_info("Creator","example.php");

  $pdf->set_info("Author","Alejandro Gervasio");

  $pdf->set_info("Title","Example on using PHP to create PDF
docs");

  $pdf->begin_page_ext(421,595,"");

 

   $font=$pdf->load_font("Helvetica-Bold","winansi","");

  $pdf->setfont($font,24.0);

  $pdf->set_text_pos(50,500);

  $pdf->show("PHP is great for creating PDFs!");

// load image

   $img=$pdf->load_image("jpeg","sample_image.jpg","");

// display image on page

  $pdf->fit_image($img,390,575,"");

// close image resource

  $pdf->close_image($img);

// end page

  $pdf->end_page_ext("");

// end document

  $pdf->end_document("");

// get buffer contents

  $buffer=$pdf->get_buffer();

// get length of buffer

    $len=strlen($buffer);

// display PDF document

  header("Content-type: application/pdf");

  header("Content-Length: $len");

  header("Content-Disposition: inline; filename=example.pdf");

   echo $buffer;

}

catch (PDFlibException $e){

  echo 'Error Number:'.$e->get_errnum()."n";

  echo 'Error Message:'.$e->get_errmsg();

 exit();

}

As shown in the prior example, including a simple image into a specific PDF file is reduced to loading the pertinent graphic via the brand new "load_image()" method, and then displaying it using another method, named "fit_image()". As you might have guessed, the first method comes in handy for loading images of different types (JPEG, GIF, PNG, etc.) and the second one simply shows it at a specified position within the document in question. Quite simple, isn't it?

Besides, as I explained earlier, the PDFlib library comes equipped with some  additional methods that can be used to display images on a given PDF file, but in my opinion the ones shown here are the most intuitive. If you're interested in learning how to use these additional methods, please visit the PHP official web site and read the PDF-related section.

So far, so good. At this time you've hopefully grasped the logic required to display some basic images on a concrete PDF document, which indeed is a no-brainer process that can be tackled with minimal hassles. So assuming that you already learned this topic, let's move forward and see how to use some other useful methods that come integrated with the PDFlib library to display blocks of texts, called "text flows."

Sounds quite interesting, doesn't it? So go ahead and read the next section. I'll be there, waiting for you.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: