Constructing Multi-Line PDF Documents with PHP 5
If you’re a PHP developer interested in developing web applications that deliver the contents of their database tables in PDF format, then hopefully this group of articles will be what you’re looking for. Welcome to the second article of the series “Building PDF documents with PHP 5.” Composed of five instructive tutorials, this series shows you the basics for creating PDF files directly from your own PHP 5 scripts, and complements the theoretical concepts with copious hands-on examples.
Having introduced you to the main subject of this series, now let me refresh your memory of the topics that were discussed in the previous tutorial. In that article, we learned that creating primitive PDF documents using PHP is a process that’s reduced to spawning a new instance of the “PDFlib” class that comes bundled with the popular “PDFLib” library, and then calling one or more of its pertinent methods to open a new PDF document and a page, which usually need to include some basic contents in them, like text strings and images as well.
With regard to specifying the dimensions of the PDF page that will be used for constructing the corresponding document, the “PDFLib” package offers the handy “begin_page_ext()” method. It’s useful for creating PDF pages of different sizes, including some popular formats, like A3, A4, and A5.
In addition, you saw how to include some basic texts into several PDF files by using a combination of the “set_text_pos()” and “show()” methods. Naturally these are packaged with the “PDFLib” library. In this way you learned some of the most common tasks associated with the creation of PDF files.
However, to be frank, I’m just starting to scratch the surface when it comes to building PDF files with PHP 5. There are many other helpful methods, provided by aforementioned “PDFLib” package. Thus, in this second part of the series I’ll provide you with some examples that illustrate how to build PDF files that contain multiple lines of text; remember, in the preceding article I worked only with single-line strings. Additionally, I’ll teach you how to position these texts on a given PDF page.
With the preliminaries out of the way, it’s time to continue learning more about building PDF files with PHP 5. Let’s go!
Review: building basic PDF documents using PHP 5
As usual with my articles on PHP development, before I move forward and show you how to include multiple lines of text into a given PDF document, I’m going to give you a good review. I want you to recall how to utilize some basic methods included with the “PDFLib” library to build a few simple PDF files.
Below I listed the source code of two examples shown in the first installment of the series. They demonstrate how to create a couple of PDF documents, in A4 and A5 format respectively. Their respective signatures are as follows:
// 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->get_errmsg());
}
$pdf->set_info("Creator","pdf_example.php");
$pdf->set_info("Author","Alejandro Gervasio");
$pdf->set_info("Title","Example on using PHP to create PDF docs");
$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();
}
// example creating a basic A5 PDF document with PHP 5
try {
// create new instance of the 'PDFlib' class
$pdf=new PDFlib();
// open new PDF file; insert a file name to create the PDF document on disk */
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!");
// 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();
}
So far, so good. After looking at the two previous code samples, I’m pretty sure that you now remember the basics of building simple PDF documents that include single lines of text. So now we’ll go one step further and see how to create PDF files that display multiple lines.
As you may guess, this interesting process will be discussed in the next section, so click on the link that appears below and keep reading.
Displaying multiple lines of text on a single PDF document
As I anticipated in the previous section, the “PDFlib” library also incorporates a handy method, called “continue_text().” As its name clearly implies, it allows you to “continue” an existing line of text previously included into a given PDF document. This makes it possible to build several blocks of text that are composed of multiple lines.
To demonstrate the implementation of this brand new method, below I listed an illustrative hands-on example. It first builds a basic PDF file, and then includes into it a few lines of trivial text.
Having said that, please take a look at the corresponding code sample, which is as follows:
// example creating a basic PDF document with PHP and multiple lines using the 'continue_text()' method
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!");
$pdf->continue_text('This is another line of text');
$pdf->continue_text('This is another line of text');
$pdf->continue_text('This is another line of text');
// 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 this specific case I used the aforementioned “continue_text()” method that comes packaged with the “PDFLib” library to build a simple PDF file, which this time displays multiple lines of basic text. Obviously, the functionality offered by the method in question is indeed remarkable, since most PDF files display, in one form or another, more than one line of strings.
To complement the above example, below I included an image that depicts the output generated by the code sample that you learned previously. It is as follows:

Pretty easy to grasp, right? At this point you hopefully learned how to build basic PDF documents that are capable of displaying multiple lines of simple text, directly from inside your PHP 5 scripts. So what’s the next step? Well, as you probably realized, the prior example used the “set_text_pos()” method to define the corresponding X,Y coordinates where the text should be displayed on the pertinent PDF document.
Thus, considering the functionality provided by this method, in the last section of this tutorial I’m going to show you how to use it to display chunks of text at different positions on the same PDF file.
To see how this will be achieved, please jump ahead and read the next few lines. I’ll be there, waiting for you.
Building a multi-line PDF document using the set_text_pos() method
As you learned in the previous section, including multiple lines of text in a given PDF file is actually a no-brainer process, since it only requires calling the handy “continue_text()” method as many times as necessary. However, it should be noticed that before displaying any strings on the file in question I utilized the “set_text_pos()” method to specify what X,Y coordinates should be used to start showing the pertinent text. This sounds pretty logical, right?
You already understand how to put multiple lines of text in a specific PDF file. Therefore, the last example in this article will focus on demonstrating how to display several lines of text in a given PDF document, which will be positioned at different X,Y coordinates via the aforementioned “set_text_pos()” method.
This being said, here’s the corresponding code sample, so have a look at it, please:
// example creating a basic PDF document with PHP and multiple lines using the 'set_text_pos()' method
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!");
$pdf->set_text_pos(50,450);
$pdf->show('This is another line of text');
$pdf->set_text_pos(50,400);
$pdf->show('This is another line of text');
$pdf->set_text_pos(50,350);
$pdf->show('This is another line of text');
// 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();
}
Certainly, after analyzing the signature of the above example, you’ll have to agree with me that positioning different chunks of text in a concrete PDF file using a set of predefined X,Y coordinates is a very understandable process, particularly when the respective “set_text_pos()” method is utilized.
In addition to providing you with the previous code sample, I captured a screen shot below to show you the corresponding output generated by the example. Here it is:

Well, at this point I assume that you are much better prepared to build a few basic multi-line PDF documents with PHP 5, particularly if you’ve studied in detail all of the code samples shown previously. As usual with many other topics on PHP development, practice is the best way to learn how to build dynamic PDF files, so I recommend that you create your own testing examples.
Final thoughts
In this second part of the series, you hopefully learned the basics for building basic PDF files that include multiple lines of text, via the proper combination of the useful “set_text_pos()”, “show()” and continue_text()” methods respectively.
In the next tutorial, things will get even more interesting, since you’ll learn how to include some basic images into a given PDF document, in addition to building text flows.
Now that you’ve been warned about the bunch of topics that will be covered in the upcoming tutorial, you won’t want to miss it!
This article was originally posted at DevShed.com
Contributed by Alejandro Gervasio