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

Displaying a basic text flow - 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!

  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



As I stated in the section that you just read, the PDFlib package provided PHP developers with a bunch of handy methods aimed at displaying in a specific PDF document, several blocks of text, called "text flows."

Basically, these blocks can be easily placed at any position within the pertinent document. It's also possible to specify their respective width and height values, which can be useful if you want to include strings that have predefined dimensions.

However, let me put aside this boring theory for a moment and show you a practical example of how to include a basic text flow into a primitive PDF file. The corresponding code sample is listed below, so have a close look at it:


// example creating a basic PDF document and display text flow

// create new instance of the 'PDFlib' class

  $pdf=new PDFlib();

// open new PDF file


throw new PDFlibException("Error creating PDF document. ".$pdf-



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

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






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

// create text flow

$textflow=$pdf->create_textflow('This is a sample
string','fontname=Tahoma fontsize=30 encoding=winansi');

// display text flow


// delete text flow


// end page


// end document


// get buffer contents


// get length of 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();



As you can see, the above example uses three new methods, called "create_text_flow()", "fit_text_flow()", and "delete_text_flow()" to build a simple block of text, and then display this block at a specified position within the PDF file, and finally delete it from the respective buffer.

In addition, it's worth noting here how the "fit_text_flow()" method used in this case to specify the respective width and height values should be assigned to the text flow just created.

And finally, to extend the explanation of including a basic text flow into a given PDF file, below I included a screen shot that shows the output generated by the previous script: 

All right, having demonstrated how to include one basic text flow into a simple PDF file using the PDFlib package in conjunction with PHP 5, I hope that you'll have a strong enough foundation to start developing your own testing examples, with the purpose of acquiring a solid background in building PDF documents with PHP.

Final thoughts

That's all for the moment. In this third part of the series I showed you how to use a group of additional methods bundled with the "PDFlib" library, how to include images into a given PDF file, and introduced simple blocks of texts, called "text flows."

In the upcoming article, I'm going to teach you how to display a few basic shapes within a PDF file, which can be useful if you want to build PDF documents that contain richer contents.

To learn more about these interesting topics, don't miss the next part of the series!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: