Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fpdf multicell issue

Tags:

fpdf

how we display fpdf multicell in equal heights having different amount of content

like image 469
gireesh Avatar asked Nov 17 '09 11:11

gireesh


People also ask

What is MultiCell in FPDF?

The MultiCell function facilitates the generation of complex table structures within the FPDF PDF generating library. When used properly, the application can handle variable length data within each cell of the table. Simplistically stated, the MultiCell function is all about geometries.

How do you do a line break in FPDF?

What about PDF Forms? With a text form field, a "\r" (carriage return) character can be used to separate two lines. (Two consecutive carriage returns can be used to separate two lines with an empty line.) PDFOne Java also allows the use of "\n" (newline) character but it will be replaced with a "\r" character.

How do I write text in FPDF?

This method prints text from the current position. When the right margin is reached (or the \n character is met) a line break occurs and text continues from the left margin. Upon method exit, the current position is left just at the end of the text. It is possible to put a link on the text.

How do you wrap text in FPDF?

php'); class PDF extends FPDF { function WordWrap(&$text, $maxwidth) { $text = trim($text); if ($text==='') return 0; $space = $this->GetStringWidth(' '); $lines = explode("\n", $text); $text = ''; $count = 0; foreach ($lines as $line) { $words = preg_split('/ +/', $line); $width = 0; foreach ($words as $word) { $ ...


1 Answers

Great question.

I solved it by going through each cell of data in your row that will be multicelled and determine the largest height of all these cells. This happens before you create your first cell in the row and it becomes the new height of your row when you actually go to render it.

Here's some steps to achieve this for just one cell, but you'll need to do it for every multicell:

This assumes a $row of data with a String attribute called description.

  1. First, get cell content and set the column width for the cell.

    $description = $row['desciption'];           // MultiCell (multi-line) content.
    
    $column_width = 50;
    
  2. Get the width of the description String by using the GetStringWidth() function in FPDF:

    $total_string_width = $pdf->GetStringWidth($description);
    
  3. Determine the number of lines this cell will be:

    $number_of_lines = $total_string_width / ($column_width - 1);
    
    $number_of_lines = ceil( $number_of_lines );  // Round it up.
    

    I subtracted 1 from the $column_width as a kind of cell padding. It produced better results.

  4. Determine the height of the resulting multi-line cell:

    $line_height = 5;                             // Whatever your line height is.
    
    $height_of_cell = $number_of_lines * $line_height; 
    
    $height_of_cell = ceil( $height_of_cell );    // Round it up.
    
  5. Repeat this methodology for any other MultiCell cells, setting the $row_height to the largest $height_of_cell.

  6. Finally, render your row using the $row_height for your row's height.

  7. Dance.

like image 143
Joshua Pinter Avatar answered Sep 20 '22 21:09

Joshua Pinter