According to the concepts that I deployed in the previous section, the PDFlib library provides PHP developers with some intuitive methods for including different types of images into a specific PDF file. Initially, PHP 4 offered a decent variety of native methods aimed at working with images and PDF documents in conjunction. Unfortunately, many of them have been deprecated. If you're still using this version of PHP and your php.ini file has been set up for working with PDF files, you're completely free to use these methods in accordance with your personal needs. In this case, though, I'm going to use only two methods for including images into a PDF document, called "load_image()" and "fit_image()" respectively, since they're pretty intuitive and easy to learn. However, you should take into account that these might not work as expected, depending on the respective versions of PHP and the "PDFlib" package that you have installed on your system. So be aware of this issue when building your PDF-related scripts. Now that I have clarified that point, please look at the following example. It shows how to include a basic image into a specific PDF file. The pertinent code sample is as follows: // example creating a basic PDF document and include a sample 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- } $pdf->set_info("Creator","example.php"); $pdf->set_info("Author","Alejandro Gervasio"); $pdf->set_info("Title","Example on using PHP to create PDF $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!"); // load image $img=$pdf->load_image("jpeg","sample_image.jpg",""); // display image on page $pdf->fit_image($img,390,575,""); // close image resource $pdf->close_image($img); // 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 shown in the prior example, including a simple image into a specific PDF file is reduced to loading the pertinent graphic via the brand new "load_image()" method, and then displaying it using another method, named "fit_image()". As you might have guessed, the first method comes in handy for loading images of different types (JPEG, GIF, PNG, etc.) and the second one simply shows it at a specified position within the document in question. Quite simple, isn't it? Besides, as I explained earlier, the PDFlib library comes equipped with some additional methods that can be used to display images on a given PDF file, but in my opinion the ones shown here are the most intuitive. If you're interested in learning how to use these additional methods, please visit the PHP official web site and read the PDF-related section. So far, so good. At this time you've hopefully grasped the logic required to display some basic images on a concrete PDF document, which indeed is a no-brainer process that can be tackled with minimal hassles. So assuming that you already learned this topic, let's move forward and see how to use some other useful methods that come integrated with the PDFlib library to display blocks of texts, called "text flows." Sounds quite interesting, doesn't it? So go ahead and read the next section. I'll be there, waiting for you.
blog comments powered by Disqus |
|
|
|
|
|
|
|