Positioning Strings with the show_xy() Method in PDF Documents with PHP 5 - Completion (Page 4 of 4 )
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!
| 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. |