Home arrow PHP arrow Page 4 - Positioning Strings with the show_xy() Method in PDF Documents with PHP 5

Completion - PHP

As you know, more and more web sites try to engage their visitors by delivering their database contents in several formats, such as Adobe’s Portable Document Format (PDF). The good news regarding this topic is that you, as a PHP developer, can take advantage of this language’s versatility and get started quickly building PDF files directly from your own scripts.

TABLE OF CONTENTS:
  1. Positioning Strings with the show_xy() Method in PDF Documents with PHP 5
  2. Inserting Blocks of Text
  3. Going One Step Further
  4. Completion
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
November 27, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Completing the implementation of the “show_xy()” method: developing a final hands-on example

As I said earlier, the last example that I’m going to provide you in this article will be focused on illustrating yet another concrete implementation for the useful “show_xy()” method that you learned in the previous sections.

So, taking this into account, now take some time and analyze the following code sample, which uses the aforementioned method to display a primitive string of text within a sample PDF file, this time using a new pair of X,Y coordinates:

 

 

  try{

 

// example creating a basic PDF document and display a
basic text using the 'show_xy()' method

// create new instance of the 'PDFlib' class

 

  $pdf=new PDFlib();

 

// open new PDF file; insert a file name to create the PDF
document on disk */

 

   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->show_xy("PHP is great for creating PDFs!",10,400);

 

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

}

This time, I’m not going to bore you explaining how the above example works, since it’s very similar to the one shown in the prior section. Of course, the only difference to spot here is that the sample text has been displayed at a different position within the sample PDF document, something that’s clearly demonstrated by the following image:

And that’s all for now. The last code sample should give you a clear idea on how to use the “show_xy()” method bundled with the “PDFlib” library to display easily several strings of text using only a couple of X,Y coordinates.

As with many other topics about PHP development, the best way to master the creation of PDF files is practice, so don’t waste more time. Start coding your own examples!

Final thoughts

It’s hard to believe, but we've actually come to the end of this series. However, hopefully the overall experience has been instructive, and fun too. As you learned with the numerous examples that were developed in this group of articles, building basic PDF files with PHP 5 is indeed a no-brainer process, except for the annoyance of having to install third-party software on the web server, like the “PDFlib” library (although other options are available at no cost, like FPDF and TCPDF), which isn’t always as easy as expected.

See you in the next PHP development tutorial!



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