ASP: use FPDF to create a PDF with a dynamic table

Following my two previous articles about FPDF (Create PDF files with FPDF and FPDF MultiCell with multiple lines and positioning), today I would like to share a way to create a table dynamically, getting the items from a database, and displaying those items one after the other.

As usual, we have to make some assumptions. First of all we have a database from which we will get the records for the table. As an example, we can imagine our table is something like:

FRUITIMAGE_URL
orange/images/orange.jpg
apple/images/apple.jpg
pear/images/pear.jpg

Our goal is to create the PDF with the above table. We will create the table dynamically, and the table will have two columns: one for the fruits and one with the image of the related fruit.

If you are interested in the following explanation, but you haven't read the previous articles about FPDF, I suggest you read them before continuing, because here I assume you already know how to create a basic PDF file using FPDF.


The code
The main issue when creating the above table is the positioning of elements. Basically we need to position the MultiCell dynamically. We already saw something similar in my article about MultiCell positioning however, in this post we will approach the issue in a different way.
We need to use the recordset index to dynamically place the elements of the table. So, we need to determine the distance from the top margin before anything else. As an example, our top margin will be 50mm.

Ok let's see the code. In our ASP page, after creating the recordset (which is called FruitList) and after declaring all the FPDF parameters, we create a variable and give it a value of 50mm.
Dim topmargin
topmargin = 50
We declare the variables for the repeat region:
Dim FL_index
FL_index = 0
And we create the table:
While (NOT FruitList.EOF)
pdf.SetXY 8,topmargin
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If (fs.FileExists(Server.MapPath("(FruitList.Fields.Item("IMAGE_URL").value))))=true then
   pdf.Image (FruitList.Fields.Item("IMAGE_URL").value),8,topmargin,0,10
else
   pdf.Image "/Images/noimg.jpg",8,topmargin,0,10
end if
set fs=nothing
pdf.SetFillColor 136,136,136
pdf.MultiCell 55,10,(FruitList.Fields.Item("FRUIT").value),1,2,1
FL_index=FL_index+1
topmargin=topmargin+(FL_index*10)
FruitList.MoveNext()
Wend
A few notes to the above code:
1) after opening the repeat region, we set the coordinates using the topmargin variable;
2) we then check for the existence of the image (just to be sure) and, if so, we display it, otherwise we display a generic noimg image;
3) the SetFillColor command is completely optional. I inserted it just to show you what you can do to change the MultiCell background color;
4) we insert the fruit into a MultiCell which has a width of 55mm and and height of 10mm (I say mm because I assume you set that as parameter with CreatePDF when the FPDF class is initialized);
5) we increase the index and the topmargin, before moving to the next record. The topmargin is increased multiplying the index by 10 (1x10, 2x10 and so on), because our MultiCell has an height of 10mm.

I hope everything is clear and that you find the example useful. Let me know what you think about it.