Horde_Pdf_Writer::multiCell PHP Method

multiCell() public method

They can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other. Text can be aligned, centered or justified. The cell block can be framed and the background painted.
See also: setFont()
See also: setDrawColor()
See also: setFillColor()
See also: setLineWidth()
See also: cell()
See also: write()
See also: setAutoPageBreak()
public multiCell ( float $width, float $height, string $text, mixed $border, string $align = 'J', integer $fill )
$width float Width of cells. If 0, they extend up to the right margin of the page.
$height float Height of cells.
$text string String to print.
$border mixed Indicates if borders must be drawn around the cell block. The value can be either a number: - 0: no border (default) - 1: frame or a string containing some or all of the following characters (in any order): - L: left - T: top - R: right - B: bottom
$align string Sets the text alignment. Possible values are: - L: left alignment - C: center - R: right alignment - J: justification (default value)
$fill integer Indicates if the cell background must: - 0: transparent (default) - 1: painted
    public function multiCell($width, $height, $text, $border = 0, $align = 'J', $fill = 0)
    {
        $cw = $this->_current_font['cw'];
        if ($width == 0) {
            $width = $this->w - $this->_right_margin - $this->x;
        }
        $wmax = ($width - 2 * $this->_cell_margin) * 1000 / $this->_font_size;
        $s = str_replace("\r", '', $text);
        $nb = strlen($s);
        if ($nb > 0 && $s[$nb - 1] == "\n") {
            $nb--;
        }
        $b = 0;
        if ($border) {
            if ($border == 1) {
                $border = 'LTRB';
                $b = 'LRT';
                $b2 = 'LR';
            } else {
                $b2 = '';
                if (strpos($border, 'L') !== false) {
                    $b2 .= 'L';
                }
                if (strpos($border, 'R') !== false) {
                    $b2 .= 'R';
                }
                $b = strpos($border, 'T') !== false ? $b2 . 'T' : $b2;
            }
        }
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $ns = 0;
        $nl = 1;
        while ($i < $nb) {
            // Get next character.
            $c = $s[$i];
            if ($c == "\n") {
                // Explicit line break.
                if ($this->_word_spacing > 0) {
                    $this->_word_spacing = 0;
                    $this->_out('0 Tw');
                }
                $this->cell($width, $height, substr($s, $j, $i - $j), $b, 2, $align, $fill);
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                $ns = 0;
                $nl++;
                if ($border && $nl == 2) {
                    $b = $b2;
                }
                continue;
            }
            if ($c == ' ') {
                $sep = $i;
                $ls = $l;
                $ns++;
            }
            $l += $cw[$c];
            if ($l > $wmax) {
                // Automatic line break.
                if ($sep == -1) {
                    if ($i == $j) {
                        $i++;
                    }
                    if ($this->_word_spacing > 0) {
                        $this->_word_spacing = 0;
                        $this->_out('0 Tw');
                    }
                    $this->cell($width, $height, substr($s, $j, $i - $j), $b, 2, $align, $fill);
                } else {
                    if ($align == 'J') {
                        $this->_word_spacing = $ns > 1 ? ($wmax - $ls) / 1000 * $this->_font_size / ($ns - 1) : 0;
                        $this->_out(sprintf('%.3F Tw', $this->_word_spacing * $this->_scale));
                    }
                    $this->cell($width, $height, substr($s, $j, $sep - $j), $b, 2, $align, $fill);
                    $i = $sep + 1;
                }
                $sep = -1;
                $j = $i;
                $l = 0;
                $ns = 0;
                $nl++;
                if ($border && $nl == 2) {
                    $b = $b2;
                }
            } else {
                $i++;
            }
        }
        // Last chunk.
        if ($this->_word_spacing > 0) {
            $this->_word_spacing = 0;
            $this->_out('0 Tw');
        }
        if ($border && strpos($border, 'B') !== false) {
            $b .= 'B';
        }
        $this->cell($width, $height, substr($s, $j, $i), $b, 2, $align, $fill);
        $this->x = $this->_left_margin;
    }

Usage Example

Example #1
0
 public function run()
 {
     extract($this->_params, EXTR_REFS);
     $driver = $GLOBALS['injector']->getInstance('Jonah_Driver');
     if (!$story_id) {
         try {
             $story_id = $GLOBALS['injector']->getInstance('Jonah_Driver')->getLatestStoryId($channel_id);
         } catch (Exception $e) {
             $this->_exit($e->getMessage());
         }
     }
     try {
         $story = $driver->getStory($story_id, !$browser->isRobot());
     } catch (Exception $e) {
         $this->_exit($e->getMessage());
     }
     // Convert the body from HTML to text if necessary.
     if (!empty($story['body_type']) && $story['body_type'] == 'richtext') {
         $story['body'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($story['body'], 'html2text');
     }
     // Set up the PDF object.
     $pdf = new Horde_Pdf_Writer(array('format' => 'Letter', 'unit' => 'pt'));
     $pdf->setMargins(50, 50);
     // Enable automatic page breaks.
     $pdf->setAutoPageBreak(true, 50);
     // Start the document.
     $pdf->open();
     // Start a page.
     $pdf->addPage();
     // Publication date.
     if (!empty($story['published_date'])) {
         $pdf->setFont('Times', 'B', 14);
         $pdf->cell(0, 14, $story['published_date'], 0, 1);
         $pdf->newLine(10);
     }
     // Write the header in Times 24 Bold.
     $pdf->setFont('Times', 'B', 24);
     $pdf->multiCell(0, 24, $story['title'], 'B', 1);
     $pdf->newLine(20);
     // Write the story body in Times 14.
     $pdf->setFont('Times', '', 14);
     $pdf->write(14, $story['body']);
     // Output the generated PDF.
     $browser->downloadHeaders($story['title'] . '.pdf', 'application/pdf');
     echo $pdf->getOutput();
 }
All Usage Examples Of Horde_Pdf_Writer::multiCell