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

Listing the source code of some previous examples - 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

Before I proceed to show you how to include some basic images into a given PDF document, first I'd like you to recall properly how to use the handy "set_text_pos()" and "continue_text()" methods that I explained in the previous article of the series, in order to build PDF files that contain multiple lines of text.

Regarding the utilization of these methods, below I listed a pair of illustrative examples that show how to implement them. Here are the corresponding code samples, so have a look at them, please:

// example creating a basic PDF document with PHP and multiple
lines using the 'continue_text()' method

  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!");

  $pdf->continue_text('This is another line of text');

  $pdf->continue_text('This is another line of text');

  $pdf->continue_text('This is another line of text');

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

}

// example creating a basic PDF document with PHP and multiple
lines using the 'set_text_pos()' method

  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!");

  $pdf->set_text_pos(50,450);

  $pdf->show('This is another line of text');

  $pdf->set_text_pos(50,400);

  $pdf->show('This is another line of text');

  $pdf->set_text_pos(50,350);

  $pdf->show('This is another line of text');

// 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 you can see, the two examples above demonstrate how to utilize the "set_text_pos()", "show()", and "continue_text()" methods that come integrated with the PDFlib library to build a couple of PDF documents that include some basic multi-line texts.

Of course, in this case, the "set_text_pos()" method makes it really easy to display a given string in a PDF file using a pair of X, Y coordinates, which can be expanded to scope two or more lines of text.

All right, at this stage you've hopefully recalled the complete sequence of steps required to build basic PDF files that contain simple strings. Considering this, I believe that it's time to continue learning more methods that come bundled with the PDFlib package.

In the upcoming section of this tutorial I'm going to show you how to incorporate a simple image into an existing PDF file to make it slightly more appealing. Of course, to see how this will be done, you have to click on the link below and keep reading.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: