As I stated in the beginning of this article, building basic PDF files with PHP 5 is a straightforward process that can be easily learned even if you’re a novice PHP programmer. In simple terms, and assuming the corresponding PDFlib library has already been installed on your web server, creating a simple PDF document is reduced to spawning an instance of a bundled class, called “PDFlib”, and using some of its most common methods. Period. But let me dispose of the theory for an instant and show you this process from a practical point of view. Take a look at the following code sample. It first creates a primitive PDF file that contains a basic text, and then displays this content by using a default PDF application: // example creating a basic PDF (A4) document with PHP
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","pdf_example.php"); $pdf->set_info("Author","Alejandro Gervasio"); $pdf->set_info("Title","Example on using PHP to create PDF $pdf->begin_page_ext(595,842,"");
$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 in the above example, I first created a new instance of the aforementioned “PDFlib” class, and then used its “begin_document()” method to start an empty PDF document, which for now resides on the web server's memory. Next, I specified some basic information, like the document’s title and author, and after that I utilized the “begin_page_ext()” method to specify the dimensions of the page that will be used (in this case, I’m using an A4 page). Still with me? All right, the next thing that the previous example does is call in the proper sequence the “set_font()” and “set_text_pos()” methods to specify what font type should be used to include text into the page, and the X, Y coordinates where the text in question should be displayed. Also, the previous script uses the “show()” method to display a basic string on the PDF page. Then it closes the page and the document respectively, via the respective “end_page_ext()” and “end_document()” methods. Lastly, the script finishes its execution by echoing the buffer contents in PDF format via the corresponding HTTP headers. Quite simple, right? At this point you hopefully have grasped the logic that stands behind building basic PDF files with PHP 5. In simple words, you’ll always have to create an instance of the “PDFlib” class that you learned before, then open a new document and a new page (this is only applicable when using certain methods), and finally include some type of content into it, such as texts, images, etc. At the end of this procedure, the respective page and documents must be closed, and the contents of the buffer have to be displayed via the appropriate HTTP headers. To complement the previous explanation, below I included an illustrative screen shot that shows the output generated by the previous script. Here it is:
So far, so good, right? At this stage you learned the basics of creating some simple PDF files with PHP 5, so I think it's time to develop another practical example. This will help you acquire a more solid background in how to work with PDF documents from your PHP 5 scripts. In the next section I’ll show you how to use some of the methods that you saw earlier to create A3 documents in PDF format. Want to see how this will be done? All right, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|