tFPDF::Write PHP Метод

Write() публичный Метод

public Write ( $h, $txt, $link = '' )
    function Write($h, $txt, $link = '')
    {
        // Output text in flowing mode
        $cw =& $this->CurrentFont['cw'];
        $w = $this->w - $this->rMargin - $this->x;
        $wmax = $w - 2 * $this->cMargin;
        $s = str_replace("\r", '', $txt);
        if ($this->unifontSubset) {
            $nb = mb_strlen($s, 'UTF-8');
            if ($nb == 1 && $s == " ") {
                $this->x += $this->GetStringWidth($s);
                return;
            }
        } else {
            $nb = strlen($s);
        }
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while ($i < $nb) {
            // Get next character
            if ($this->unifontSubset) {
                $c = mb_substr($s, $i, 1, 'UTF-8');
            } else {
                $c = $s[$i];
            }
            if ($c == "\n") {
                // Explicit line break
                if ($this->unifontSubset) {
                    $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 2, '', 0, $link);
                } else {
                    $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', 0, $link);
                }
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                if ($nl == 1) {
                    $this->x = $this->lMargin;
                    $w = $this->w - $this->rMargin - $this->x;
                    $wmax = $w - 2 * $this->cMargin;
                }
                $nl++;
                continue;
            }
            if ($c == ' ') {
                $sep = $i;
            }
            if ($this->unifontSubset) {
                $l += $this->GetStringWidth($c);
            } else {
                $l += $cw[$c] * $this->FontSize / 1000;
            }
            if ($l > $wmax) {
                // Automatic line break
                if ($sep == -1) {
                    if ($this->x > $this->lMargin) {
                        // Move to next line
                        $this->x = $this->lMargin;
                        $this->y += $h;
                        $w = $this->w - $this->rMargin - $this->x;
                        $wmax = $w - 2 * $this->cMargin;
                        $i++;
                        $nl++;
                        continue;
                    }
                    if ($i == $j) {
                        $i++;
                    }
                    if ($this->unifontSubset) {
                        $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 2, '', 0, $link);
                    } else {
                        $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', 0, $link);
                    }
                } else {
                    if ($this->unifontSubset) {
                        $this->Cell($w, $h, mb_substr($s, $j, $sep - $j, 'UTF-8'), 0, 2, '', 0, $link);
                    } else {
                        $this->Cell($w, $h, substr($s, $j, $sep - $j), 0, 2, '', 0, $link);
                    }
                    $i = $sep + 1;
                }
                $sep = -1;
                $j = $i;
                $l = 0;
                if ($nl == 1) {
                    $this->x = $this->lMargin;
                    $w = $this->w - $this->rMargin - $this->x;
                    $wmax = $w - 2 * $this->cMargin;
                }
                $nl++;
            } else {
                $i++;
            }
        }
        // Last chunk
        if ($i != $j) {
            if ($this->unifontSubset) {
                $this->Cell($l, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 0, '', 0, $link);
            } else {
                $this->Cell($l, $h, substr($s, $j), 0, 0, '', 0, $link);
            }
        }
    }

Usage Example

Пример #1
0
<?php

// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory
// define("_SYSTEM_TTFONTS", "C:/Windows/Fonts/");
require 'tfpdf.php';
$pdf = new tFPDF();
$pdf->AddPage();
// Add a Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu', '', 'DejaVuSansCondensed.ttf', true);
$pdf->SetFont('DejaVu', '', 14);
// Load a UTF-8 string from a file and print it
$txt = file_get_contents('HelloWorld.txt');
$pdf->Write(8, $txt);
// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial', '', 14);
$pdf->Ln(10);
$pdf->Write(5, 'The file size of this PDF is only 12 KB.');
$pdf->Output();
All Usage Examples Of tFPDF::Write