Drawing Basic Rectangles in PDF Documents with PHP 5

If you’re a PHP developer who’s searching for a friendly guide on how to build dynamic PDF files within your PHP 5-based applications, then look no further, and start reading this series now!

Welcome to the fourth part of the series that began with “Building PDF documents with PHP 5.” Since PHP 5 comes packaged with robust capabilities for delivering database contents in a considerable variety of formats, including Adobe’s PDF (Portable Document Format), this series teaches you how to create this type of file with only minor hassles, directly from your own scripts.

If you already read the two preceding tutorials of the series, you’ve acquired a solid background in building simple PDF documents with PHP 5. In those articles I covered the basics of this process, including the adequate implementation of the “set_text_pos()”, “show()”, and “continue_text()” methods that come integrated with the PDFLib library. I did this in order to display both single and multi-line texts, as well as incorporate some basic images into a given PDF document.

Creating dynamic PDF files is a process reduced to spawning an instance of the already familiar PDFLib class, bundled with the homonymous library, and then calling the appropriate set of methods in accordance with the kind of tasks that must be performed on the file in question, such as opening a new document and a new page, adding some lines of text, including images, you name it.

In addition, you’ll certainly recall that in the previous installment of the series I showed you how to create different blocks of text inside a determined PDF document, which in this context are called “text flows.” As you saw, these text flows are useful when you want to incorporate several paragraphs that have predefined dimensions — that have specific widths and heights respectively.

Regarding the incorporation of text flows into a given PDF document, I showed a concrete example of how to use a combination of the handy “create_text_flow()”, “fit_text_flow()”, and “delete_text_flow()” methods, to display a single block of text at a specific position within the pertinent document. Certainly this is a procedure that can be performed effortlessly, mostly because of the easy learning curve offered by the aforementioned methods.

All right, now that you know how to include basic blocks of text into a simple PDF file, in addition to incorporating some images, the question that comes up here is: what’s the next step to take? Well, in this fourth part of the series I’m going to show you how to draw a few basic shapes, once a PDF document has been opened, like empty and filled rectangles, which can be useful if you want to decorate the document with these kinds of forms.

Now, let’s get rid of the boring preliminaries and start learning more about how to draw a few simple rectangles in a specific PDF file. Let’s get going!

{mospagebreak title=Adding an image and a text flow to a PDF file}

Since my personal experience always dictates that it’s much better to refresh topics covered in a previous article before learning new ones, I’m going to list two hands-on examples that were developed in the preceding tutorial regarding the incorporation of a simple image and text flow to a given PDF file.

Here are the corresponding code samples, so have a close look at them, please:

// example creating a basic PDF document and include a sample
image

  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-
>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->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();

}

// example creating a basic PDF document and display a text flow

  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-
>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->set_text_pos(50,500);

  $pdf->show("PHP is great for creating PDFs!");

// create text flow

$textflow=$pdf->create_textflow(‘This is a sample
string’,’fontname=Tahoma fontsize=30 encoding=winansi’);

// display text flow

  $pdf->fit_textflow($textflow,50,450,400,220,”);

// delete text flow

  $pdf->delete_textflow($textflow);

// 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’ll probably recall, the first example demonstrates very clearly how to include a “sample_image.jpg” image into a concrete PDF file, while the second one shows how to create and display a basic text flow in the same file, using some of the most relevant methods that come bundled with the PDFLib library.

Of course, in both cases the incorporation of additional content to a specific PDF file is always performed after it has been opened, via the respective “begin_document()” method that you saw before. Now, do the above hands-on examples ring any bells to you? I’m sure they do!

Okay, assuming that including images and blocks of text into a sample PDF document is now a familiar process to you, it’s time to continue exploring many other methods packaged with the PDFLib library. In the next section I’m going to teach you how to draw a few simple rectangles on a given PDF file, so if you’re interested in finding out more on this process, please click on the link that appears below and keep on reading.

{mospagebreak title=Drawing some basic rectangles}

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
basic rectangle

// 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-
>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->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.

{mospagebreak title=Displaying multiple rectangles on the same PDF file}

As I explained in the previous section, it’s perfectly feasible to display more than one rectangle in the same PDF file by using the “rect()” and “stroke()” methods that were reviewed earlier. To demonstrate this concept as clearly as possible, below I coded another hands-on example that utilizes the methods to include two different rectangles in the sample PDF document.

The respective code sample is as follows:

try{

// example creating a basic PDF document and display a
basic rectangle

// 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-
>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->set_text_pos(50,500);

$pdf->show("PHP is great for creating PDFs!");

// create rectangle

$pdf->rect(30,100,150,150);

// create another rectangle

$pdf->rect(200,100,150,300);

// display both rectangles

$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();

}

See how easy it is to display multiple rectangles in the same PDF file? I guess you do! In this specific situation, the “rect()” method is called twice to define the respective rectangles at different locations, and then they’re displayed by using the corresponding “stroke()” method.

You may want to have a look at the output produced by the above example. Below I included a screen shot that shows how the pertinent rectangles are displayed in the same PDF document:

Also, as usual with many of my articles on PHP development, you’re free to tweak the source code in all of the code samples shown here to acquire more practice in building PDF files with PHP 5. Happy coding!

Final thoughts

In this fourth installment of the series, you hopefully learned how to draw a few simple rectangles in a sample PDF file, which can be useful if you want to add some decorative effects to the file in question.

Nonetheless, this educational journey surrounding the creation of PDF documents with PHP 5 has not ended yet, since there are some other useful methods bundled with the handy PDFlib library that need to be properly reviewed.

Thus, in the last tutorial of the series, I’m going to show you how to display several blocks of text at different positions within a given PDF file, by using another useful method called “show_xy()”.

Now that you know what the last article will be about, are you going to miss it? I hope not!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye