Carles Andres
Carles Andres

Reputation: 1811

Linespacing in TCPDF MultiCell

I haven't found a way to control linespacing in TCPDF's MultiCell as of version 5.9. Nor have I found any method that helps to mimic the same behaviour by using Cell, such as some method that returns the portion of the text that doesn't fit inside a Cell's width.

Any ideas?

I wish I could switch to FPDF (which I prefer), but I can't.

Upvotes: 19

Views: 37544

Answers (2)

rubo77
rubo77

Reputation: 20817

for your second question, this could help:

<?php
class FontSizes {
    public $fontsize12 = 12;
    public $fontsize10 = 10;
}

/**
 * Fill a cell with text. It uses MultiCell to test if the text fits in the cell.
 * If not, the font size is reduced to fit, starting at 12 pt
 * and potentially reducing to 10 pt for multiline entries. If it still 
 * does not fit, it reduces the string and adds three dots ...
 * @param int $w Width of the cell
 * @param int $h Height of the cell
 * @param string $txt Text content
 * @param int $border Border
 * @param int $ln Line break
 * @param string $align Alignment (default is 'L' for left)
 * @param bool $fill Fill (default is false)
 */
private function autoFitCell($w, $h, $txt, $border, $ln, $align='L', $fill=false) {
    $fontSizes = new FontSizes(); // Create an instance of the FontSizes class
    $this->startTransaction();
    $this->SetFontSize($fontSizes->fontsize12);
    $this->SetY(0);
    $this->Multicell($w, $h, $txt, $border, $align, $fill, 1);
    $lineHeight = $this->getLastH();
    $this->rollbackTransaction(true);
    //vd(array($lineHeight, $this->medication_line_height));
    $threedots = "";
    if ($lineHeight > $this->medication_line_height) {
        $this->setCellHeightRatio(1);
        $this->SetFontSize($fontSizes->fontsize10);
        // Start another transaction and shorten until it fits within the box
        $loopbreaker = 1000;
        do {
            $fits_into_cell = true;
            $this->startTransaction();
            $this->SetY(0);
            $this->Multicell($w, $h, $txt . $threedots, $border, $align, $fill, 1);
            $lineHeight = $this->getLastH();
            $this->rollbackTransaction(true);
            if ($lineHeight > $this->medication_line_height) {
                $fits_into_cell = false;
                $txt = substr($txt, 0, strlen($txt) - 1);
                $threedots = "...";
            }
        } while (($loopbreaker-- > 0) and !$fits_into_cell);
    }
    $this->Multicell($w, $h, trim($txt) . $threedots, $border, $align, $fill, $ln, '', '', true, 0, false, true, $h, 'M'); // valign middle

    $this->SetFontSize($fontSizes->fontsize12);
    $this->setCellHeightRatio(1.2);
}

Upvotes: 0

KostaPC
KostaPC

Reputation: 654

$pdf->setCellHeightRatio(0.8);

in TCPDF source code documentation:

* Set the height of the cell (line height) respect the font height.
     * @param $h (int) cell proportion respect font height (typical value = 1.25).
     * @public
     * @since 3.0.014 (2008-06-04)

Upvotes: 61

Related Questions