subWrite

Add-On subWrite

Informations

Author:Wirus
License: FPDF

Description

This method prints text from the current position in the same way as Write(). An additional parameter allows to reduce or increase the font size; it's useful for initials. A second parameter allows to specify an offset so that text is placed at a superscripted or subscripted position.

subWrite(float h, string txt [, mixed link [, float subFontSize [, float subOffset]]])

h: line height
txt: string to print
link: URL or identifier returned by AddLink()
subFontSize: size of font in points (12 by default)
subOffset: offset of text in points (positive means superscript, negative subscript; 0 by default)

Source

<?php
require('fpdf.php');

class PDF extends FPDF
{
function subWrite($h, $txt, $link='', $subFontSize=12, $subOffset=0)
{
    // resize font
    $subFontSizeold = $this->FontSizePt;
    $this->SetFontSize($subFontSize);
    
    // reposition y
    $subOffset = ((($subFontSize - $subFontSizeold) / $this->k) * 0.3) + ($subOffset / $this->k);
    $subX        = $this->x;
    $subY        = $this->y;
    $this->SetXY($subX, $subY - $subOffset);

    //Output text
    $this->Write($h, $txt, $link);

    // restore y position
    $subX        = $this->x;
    $subY        = $this->y;
    $this->SetXY($subX,  $subY + $subOffset);

    // restore font size
    $this->SetFontSize($subFontSizeold);
}
}
?>

Example

<?php
require('subwrite.php');

$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);

$pdf->Write(5, 'Hello World!');
$pdf->SetX(100);
$pdf->Write(5, "This is standard text.\n");
$pdf->Ln(12);

$pdf->subWrite(10, 'H', '', 33);
$pdf->Write(10, 'ello World!');
$pdf->SetX(100);
$pdf->Write(10, "This is text with a capital first letter.\n");
$pdf->Ln(12);

$pdf->subWrite(5, 'Y', '', 6);
$pdf->Write(5, 'ou can also begin the sentence with a small letter. And word wrap also works if the line is too long!');
$pdf->SetX(100);
$pdf->Write(5, "This is text with a small first letter.\n");
$pdf->Ln(12);

$pdf->Write(5, 'The world has a lot of km');
$pdf->subWrite(5, '2', '', 6, 4);
$pdf->SetX(100);
$pdf->Write(5, "This is text with a superscripted letter.\n");
$pdf->Ln(12);

$pdf->Write(5, 'The world has a lot of H');
$pdf->subWrite(5, '2', '', 6, -3);
$pdf->Write(5, 'O');
$pdf->SetX(100);
$pdf->Write(5, "This is text with a subscripted letter.\n");

$pdf->Output();
?>
View the result here.

Download

ZIP | TGZ