Home arrow PHP arrow Page 3 - Building PDF Documents with PHP 5

Working with different page sizes - PHP

PHP is a versatile language with some surprising capabilities. It can even create PDF documents on the fly, with the right third-party PDF library. If you can't imagine why you'd want to do that, think about the various formats used by applications when retrieving data from databases -- and keep reading.

TABLE OF CONTENTS:
  1. Building PDF Documents with PHP 5
  2. Building basic PDF documents with PHP 5 using an object-oriented approach
  3. Working with different page sizes
  4. Creating a basic A5 PDF document using PHP 5
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
October 30, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous section, you hopefully learned how to build an A4 PDF document by using some methods that come integrated with the already familiar “PDFlib” package. I’m going to develop yet another practical example, this time to create an A3 document, which also will contain some basic text.

The corresponding code sample is as follows:

// example creating a basic A3 PDF document with PHP 5

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(842,1190,"");

 

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

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

  $pdf->set_text_pos(50,800);

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

// 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, building A3 PDF documents with PHP 5 doesn’t differ too much from creating one that has an A4 format. The above script, which is responsible for constructing the document, uses the same methods that you learned in the previous section, but in this case the “begin_page_ext()” method specifies that the document being created should fit the A3 format, that is 842px X 1190px respectively.

In addition, to complete the above explanation, I included an image (actually, its real size has been changed for editing reasons) that shows the output generated by the previous script:

All right, after studying the previous example, you’ll have to agree with me that building some basic PDF files with PHP 5 is indeed a no-brainer process that can be tackled with minor problems. As you saw, the procedure is reduced to spawning an instance of the corresponding “PDFlib” class, and then calling some of its numerous methods, depending on what kind of content you want to include into the file.

Assuming that you grasped how the previous example works, in the last section of this tutorial I’m going to provide you with an additional code sample, which will show you how to create the same PDF document that you saw earlier, but this time in A5 format.

To see how this last example will be developed, jump ahead and read the next few lines.



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