As you might guess, drawing basic shapes within a concrete PDF document is a process very similar to the one I described regarding how to include text flows and images. In this case, I’m going to use two brand new methods packaged with the PDFLib library, called “rect()” and “stroke()” respectively. The first one, as its name suggests, is responsible for drawing a basic rectangle, and the second one is tasked with filling it with a predefined foreground color. Having explained how these two methods work, it’s time for you to see a concrete application of them, so I suggest you to take a look at the following example, which shows how to draw a primitive rectangle in a sample PDF file. Having said that, the corresponding code sample is as follows: try{ // example creating a basic PDF document and display a // 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!"); // create rectangle $pdf->rect(100,100,150,150); // display rectangle $pdf->stroke(); // 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, the above hands-on example is very simple to grasp> It uses the aforementioned “rect()” method, obviously to draw a basic rectangle, and then displays it on the pertinent PDF document by calling the “stroke()” method. Not rocket science, right? In addition, if it's still not clear to you how the previous methods do their thing, you may want to look at the following image, which shows the output generated by the above script. The image in question is as follows:
Here you have it. Now you are hopefully learning the basic steps required to draw a simple rectangle on a given PDF document, which can be quite useful in those cases where you need to include this type of shape to make it more attractive. Anyway, as you saw earlier, including a few basic rectangles in a PDF file is in fact a straightforward task that can be performed with minor efforts. So far, so good. At this point, I showed you how to use the “PDFLib” library to draw a primitive rectangle on a sample PDF file. Nonetheless, I’d like to finish this tutorial by coding for you another example. It will demonstrate how to draw multiple rectangles on the same file. As you might have guessed, this example will be developed in detail in the last section of this article, so click on the link below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|