Building PDF Documents with PHP 5 - Creating a basic A5 PDF document using PHP 5
(Page 4 of 4 )
As I said in the section that you just read, the last code sample that I’m going to provide you in this tutorial is aimed at illustrating how to build a primitive PDF file with PHP 5, which will be rendered in A5 format. As you might guess, the document can be easily constructed by utilizing the same set of methods that you saw in the previous example, but in this case, the “begin_page_ext()” method indicates that the document to be created must fit the A5 format.
Having explained that, here’s the respective code sample:
// example creating a basic A5 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(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!");
// 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();
}
I’m not going to spend much time explaining how the above example works, since it looks very similar to the ones that you learned in earlier sections of this article. The only detail to stress here is with reference to the use of the ““begin_page_ext()” method, which instructs the script to build a page whose dimensions must fit the A5 format.
Below I included another screen shot that shows very clearly how the previous PDF file is outputted to the computer system:

That’s all for the moment. Hopefully, all of the code samples shown in this tutorial will serve to get you started building some basic PDF files in PHP 5 with minor hassles. Happy coding!
Final thoughts
In this first tutorial of the series I walked you through using some basic methods that come bundled with the “PDFlib” library to build a few basic PDF documents using PHP 5. As you learned from all the code samples shown here, the process is quite simple to grasp. Many of these methods are very intuitive, particularly when it comes to creating PDF files that contain only basic text.
In the next part of the series, I’m going to teach you how to build PDF files that include multiple lines of text, directly from your PHP 5 scripts, among other useful things. Now that you’ve been warned, you won’t want to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |