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- } $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(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.
blog comments powered by Disqus |
|
|
|
|
|
|
|