Horde_Pdf_Writer::write PHP Method

write() public method

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. Example: Begin with regular font $pdf->setFont('Arial', '', 14); $pdf->write(5, 'Visit '); Then put a blue underlined link $pdf->setTextColor(0, 0, 255); $pdf->setFont('', 'U'); $pdf->write(5, 'www.fpdf.org', 'http://www.fpdf.org');
See also: setFont()
See also: addLink()
See also: multiCell()
See also: setAutoPageBreak()
public write ( float $height, string $text, mixed $link = '' )
$height float Line height.
$text string String to print.
$link mixed URL or identifier returned by {@link addLink()}.
    public function write($height, $text, $link = '')
    {
        $cw = $this->_current_font['cw'];
        $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);
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while ($i < $nb) {
            // Get next character.
            $c = $s[$i];
            if ($c == "\n") {
                // Explicit line break.
                $this->cell($width, $height, substr($s, $j, $i - $j), 0, 2, '', 0, $link);
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                if ($nl == 1) {
                    $this->x = $this->_left_margin;
                    $width = $this->w - $this->_right_margin - $this->x;
                    $wmax = ($width - 2 * $this->_cell_margin) * 1000 / $this->_font_size;
                }
                $nl++;
                continue;
            }
            if ($c == ' ') {
                $sep = $i;
                $ls = $l;
            }
            $l += isset($cw[$c]) ? $cw[$c] : 0;
            if ($l > $wmax) {
                // Automatic line break.
                if ($sep == -1) {
                    if ($this->x > $this->_left_margin) {
                        // Move to next line.
                        $this->x = $this->_left_margin;
                        $this->y += $height;
                        $width = $this->w - $this->_right_margin - $this->x;
                        $wmax = ($width - 2 * $this->_cell_margin) * 1000 / $this->_font_size;
                        $i++;
                        $nl++;
                        continue;
                    }
                    if ($i == $j) {
                        $i++;
                    }
                    $this->cell($width, $height, substr($s, $j, $i - $j), 0, 2, '', 0, $link);
                } else {
                    $this->cell($width, $height, substr($s, $j, $sep - $j), 0, 2, '', 0, $link);
                    $i = $sep + 1;
                }
                $sep = -1;
                $j = $i;
                $l = 0;
                if ($nl == 1) {
                    $this->x = $this->_left_margin;
                    $width = $this->w - $this->_right_margin - $this->x;
                    $wmax = ($width - 2 * $this->_cell_margin) * 1000 / $this->_font_size;
                }
                $nl++;
            } else {
                $i++;
            }
        }
        // Last chunk.
        if ($i != $j) {
            $this->cell($l / 1000 * $this->_font_size, $height, substr($s, $j, $i), 0, 0, '', 0, $link);
        }
    }

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::write