Wednesday, 25 May 2011

ASP: FPDF MultiCell with multiple lines and positioning

   


As described in another post (ASP: Create PDF with FPDF), FPDF allows to create PDF files dynamically. I described how to create text boxes using MultiCell. I assume you already know how to do it, and I believe you might have encountered a little problem with the cells positioning. Let me explain it.

The positioning
When we position a multicell, we use a code like:
pdf.SetXY 8,156
pdf.setfillcolor 197,198,200
pdf.MultiCell 40,4,"Hello!",0,0,1
The above snippet is taken from the aforementioned article. Basically we set the X and Y position of the multicell, the background colour and then we place the multicell using different parameters (width, height, text, border etc.)

Now imagine that the text is not "Hello!" like in the example, but it is a long text, or better a text which lenght we do not know (because it is dynamic). When you are going to place another multicell, just after the first one, you do not know its exact position, because you don't know its height. So you can't set the X and Y position like:
pdf.SetXY 8,160
assuming that the multicell has an height set to 4. How to correctly place the new multicell?

The GetY()
We can use the pdf.GetY() function to get the Y position just after the first multicell, and then use that position to place the second multicell, the third multicell and so on.
Let's see a complete example:
pdf.SetXY 8,156
pdf.setfillcolor 197,198,200
pdf.MultiCell 40,4,"This is a very long text that won't fit inside the multicell. Because of this the multicell will expand vertically to accomodate all the text",0,0,1
y = pdf.GetY()
pdf.SetXY 8,y
pdf.Multicell 40,4,"This text will stay just after the end of the previous cell",0,0,1
You can use something like that just to be sure to place objects in the right place. Easy isn't it?

Let me know what you think about it.

30 comments:

  1. please let me know..how to generate pdf from html files....thx before

    ReplyDelete
  2. Iriandi,

    if you want to create a pdf file from HTML the only way is to print it using a virtual printer such as PDFCreator. If your page is created dynamically you can use JavaScript (for example using an xml file as source). FPDF works with asp or php.

    ReplyDelete
  3. Hi there, I have a challenge for you!
    I am in effect creating a table, it is four columns wide and multiple PAGES long.
    If my text in column 2 (say) slips into page 2 (say), then columns 3 and 4 also show on page 2 but at the Y-coordinate value from page 1. This is very hard to explain, but basically I get Page 1 of lovely neat data, but then page two has left over cell data, in column 2, then a big gap and then column 3 and 4 data at the bottome of the page. If it was still page 1 it would be perfectly positioned but because we have slipped into page 2 it is not correctly positioned.
    Does that make sense? Can you help? It's almost like I need to know the depth of the multicell before I 'write' it and if I know if it not going to fit on the page/or would result in Y-coordinate going from large to small, then I need to create a new page and start fresh.
    Thanks, Rachel

    ReplyDelete
    Replies
    1. Rachel,

      wow! Yes, what you write makes sense. Anyway, unfortunately there's no immediate solution. The only way to avoid the overflow is to make the table stay on 1 page. Or, if you like, to split it in 2. In order to know how many rows you have in the table, you can always count the resulting records, and then decide how to build your table. I would count how many records can stay on a page, and then create two tables: one from record 0 to the record that can fit in a page, and then create a new table that will display the remaining records.
      That's all I can suggest for the moment.
      Take care!

      Delete
    2. Thanks for this, but even if I know the number of rows in the dataset, I don't know how much 'space' it will take on a page, as some of the fields wrap and create more lines in the table than the total row number. I can't think of a solution either :~(

      Delete
  4. Marco,

    I just installed fpdf and played around with it and was able to get a simple "hello" page to show up. My objective is to show a PDF report (data is coming from a database) which has a question and answer format.Based on the choices selected by the user in the previous asp pages, the report could be 1 page or 2 or maybe 3 pages. Most of the questions are either 1 line or two lines, but the answers could be many lines depending on the question. My question is how would i use MultiCell, Xy postioning to place the content on the pdf?? Also, do i need to do a AddPage() for every new page? How do i know that a page is ending? Thanks for this wonderful site!!
    What i am looking for maybe is an example of how to use repeated region with recordset objects..

    ReplyDelete
    Replies
    1. Thanks for your comment. To place the multicell use set XY.
      Yes, you need to add new page every new page. There no way of knowing that the page is ending per se. You need to find a way if your content is dynamic.
      Search for the other article on FPDF and tables where I explain how to use them.
      Thanks again. I hope it helps.

      Delete
  5. This is what i have tried and it seems to work..would like to get any comments on how i can improve this code....it seems to add new page by itself..i added new page only once...here is the code...

    Set pdf=CreateJsObject("FPDF")
    pdf.CreatePDF "P","mm","A4"
    pdf.SetPath("fpdf/")
    pdf.SetFont "Arial","",12
    pdf.Open()
    pdf.AddPage()
    pdf.SetXY 8,30
    pdf.setfillcolor 197,198,200
    pdf.MultiCell 200,20,"My Report",0,0,1 'Report TITLE

    while not myrecordset.eof


    'get the values of the my fields from the table..(i have removed the code)

    y=pdf.GetY()
    pdf.SETXY 8,y
    pdf.Multicell 200,10,questionno & "." & qtitle,0,0,1
    y=pdf.GetY()
    pdf.SETXY 8,y
    pdf.Multicell 200,10,"-----------" & answer,0,0,1

    myrecordset.movenext
    wend
    myrecordset.close

    pdf.Close()
    pdf.Output filewrite

    ReplyDelete
  6. One more quick question...if i have a column in my recordset which has html tags such as bold or italics between the text, then is it possible to put them in pdf and still show those texts as bold or italics?

    Thanks.

    ReplyDelete
    Replies
    1. Unfortunately no. If you have HTML tags they won't be considered and showed as... tags. Try to use HTMLEncode when dealing with those situations either when you get the records or directly in the Multicell. I honestly don't remember if it works... sorry. Let me know how it goes... and thank you.

      Delete
  7. Thanks for this response as well as earlier response. I think was able to isolate the cases where it needed to be bold and then did another multicell for those. That worked well.

    ReplyDelete
  8. Do you how to insert line breaks into text in a multicell? I am trying to align my text to the bottom center of a cell, and haven't found any good way to set vertical alignment, so I thought adding line breaks above the text might work. I have tried using "\n" but that just prints out literally. I have tried vbcrlf, but that extends the cell a weird amount. I have tried some other things, but those haven't worked either.

    ReplyDelete
    Replies
    1. I'm sorry I don't think you can do it.

      Delete
    2. I've got the same problem. I'm using a database with names and addresses and want to add line breaks after each address line. Anybody found a way of doing it?


      Mick (Sheffield, UK)

      Delete
    3. Just a quick thought: create more than one multicell, one after the other (like a pile) without borders. That would create the effect of a line break.
      My two cents.

      Delete
    4. " & vbCrLf & "
      put this code between your text to go to next line

      Delete
    5. " & vbCrLf & "
      put this code between your text to go to next line

      Delete
  9. hey Marco, you seem to have good knowledge about FPDF.
    i'm a newbie on fpdf. i have to create a pdf but the problem is i gotta print multiple rows. Since i'm using MultiCell so each row is showing on the first one(ROW) instead of next row.
    you can mail me the answer at ammarferoze@ymail.com
    i would really appreciate the help. thanks

    ReplyDelete
    Replies
    1. Please read the article. Specifically where I explain positioning. That's where you find the answer.

      Delete
  10. hello friends. I congratulate you for the contributions. A short time ago I discovered FPDF ASP. the truth is very easy and versatile to work on it. I wanted to ask them if writing text vertically possible. Or text rotation. Thank you

    ReplyDelete
    Replies
    1. Thank you. 'll keep in touch to contribute to the blog

      Delete
  11. Thanks for a nice thingie.
    However: I'm from Sweden and havin huge problems with out native characters. They won't print correctly on the created PDF. I've tried to change coding in the web page (From ANSI) to UTF-8 which normally works, but now without any success. I'm talking about
    CHR 142 which prints Ä
    CHR 143 which prints Ã...
    CHR 153 which prints Ö-
    134 -> å
    132 -> ä
    148 -> ö

    It used to work before, but i noticed that there was something wrong when i opened a PDF with Firefox. The PDF is black. Nothing at all on it. MSIE gives me text, but with the above mentioned errors.
    I'm using a multi cell report which was printed out on a laser printer with label papers.
    Any hint would be appreciated.

    Best regards

    Roger Waara
    Sweden

    ReplyDelete
  12. Some additional information..
    I've been fiddeling around with different settings, and now it's even more odd in it's function:
    I added
    <%@ Language=VBScript codepage=1252 %>
    <% Response.Charset="UTF-8" %>
    The page itself is in ANSI
    and now i'm getting the correct native letters, BUT.. if i reload the page it's messed up again. I have to change the code with some minor change and save it again to get it to work. I load the page, correct letters, but the next time it's scrambled..

    R
    I've tried to block caching, but no difference..

    ReplyDelete
    Replies
    1. Just a small advice if I may. When playing with charset be careful and take a step at the time. Consider that the browser is playing a major role there and that it can mess things up a lot.

      Delete
  13. Yes, i know! ;) Tinkering with encodings and char-sets is nothing for the fainthearted. I always run into trouble with my native charcters, but with pure ASP-pages the only thing to do is to stick to UTF-8 and make sure that the page itself is coded in UTF-8.
    But this time it simply won't help..
    The browser itself? Yes, but the only selection i can do is to keep it on "Swedish", isn't it?

    Hoping for some advices.

    Best regards

    Roger

    ReplyDelete
  14. I've spent the last hour reproducing the problem over and over again..
    1. The PDF gets produced once. Correctly with correct letters.
    2. The second time I try it won't work. It gives me a black pdf-page with nothing on with an error message telling me something like "This document might not have been displayed properly" When i try to open it in ADOBE reader i'm getting the message that says that it wasn't possible to open the document properly because the file type isn't supported because it isn't decoded correctly"

    3. To get it to work i have to open the ASP-file and do something with in. Add a space and remove it immedeately.
    After save it works again. Once..

    Roger

    PS: BTW: Thanks for changing the CACHTA-images! ;)

    ReplyDelete
  15. Nobody?! Seriously?!

    Roger

    ReplyDelete

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.