Rich-Text Stamp
Table of Contents
Introduction
The rich-text stamp class lets you stamp text that is styled by a subset of HTML and CSS onto existing PDF pages. Internally it encapsulates a rich-text block instance.
Following HTML tags are interpreted as you know from HTML:
-
<b>
/<strong>
for bold text -
<i>
/<em>
for italic text -
<u>
for underline text -
<sup>
for superscript -
<sub>
for subscript -
<br>
/</br>
for a line-break
You can use any other HTML tag such as <span>
or <div>
, too. Internally these tags are only parsed for their style
attibute.
You can use the style
attribute to define CSS styles for an element to any tag. Following CSS styles are supported:
-
font-family: (string)
-
font-size: (float|integer)(pt|%)
-
color: #RRGGBB
hexadecimal notation line-height: (float|integer)(%)
unitless or percentual value
Font Handling
As each style (normal, bold, italic, italic+bold) of a font requires an individual font instance it is needed to have a kind of font-loading logic implemented. By default, the instance uses the standard font Helvetica
and loads the different styles automatically by a predefined font-loader callable.
To use individual fonts and make use of TTF font subsets you have to pass your own font-loader which is a callable with following signature:
/** * @param SetaPDF_Core_Document $document * @param string $fontFamily The font family as passed to the font-family property. * @param string $fontStyle An empty string = normal, 'B' = bold, 'I' = italic, 'BI' = bold+italic. */ function ( SetaPDF_Core_Document $document, string $fontFamily, string $fontStyle ): ?SetaPDF_Core_Font_FontInterface
The font instances created in this callable are bound to the document instance, and you need to take care of caching the instances appropriately to avoid an unneeded overhead. You also should make sure that the callable shares the font instances through different richt-text block or richt-text stamps instances.
A font-loader for e.g. DejaVuSans could look like:
$loadedFonts = []; $fontLoader = function (\SetaPDF_Core_Document $document, $fontFamily, $fontStyle) use (&$loadedFonts) { $cacheKey = $document->getInstanceIdent() . '_' . $fontFamily . '_' . $fontStyle; if (!array_key_exists($cacheKey, $loadedFonts)) { $font = null; if ($fontFamily === 'DejaVuSans' && $fontStyle === '') { $font = new \SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'B') { $font = new \SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans-Bold.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'I') { $font = new \SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans-Oblique.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'BI') { $font = new \SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans-BoldOblique.ttf'); } $loadedFonts[$cacheKey] = $font; } return $loadedFonts[$cacheKey]; };
and need to be passed to the constructor as the second argument.
Create an Instance
An instance of a rich-text stamp could be created by simply passing the document instance you initiate the stamper instance with to its constructor. The dependency to the document instance is required because of dynamic font loading:
$stamp = new \SetaPDF_Stamper_Stamp_RichText($document);
If you use your own font-loader, it needs to be passed as the second parameter:
$stamp = new \SetaPDF_Stamper_Stamp_RichText($document, $fontLoader);
You should define the default font-family afterwards, too:
$stamp->setDefaultFontFamily('DejaVuSans');
Configure Properties
Beside the general stamp properties the rich-text stamp class offers following properties to configured the text appearance in various ways:
Text
Text related properties could be controlled by following methods:
getDefaultFontFamily()
getDefaultFontSize()
getDefaultLineHeight()
getText()
setDefaultFontFamily()
setDefaultFontSize()
setDefaultLineHeight()
setText()
Layout
To layout a richt-text stamp following methods are available:
getAlign()
Get the text alignment.
getHeight()
Get the height of this stamp.
getPaddingBottom()
Get the bottom padding.
getPaddingLeft()
Get the left padding.
getPaddingRight()
Get the right padding.
getPaddingTop()
Get the top padding.
getTextWidth()
Returns the width of the rich-text stamp without padding.
setAlign()
Set the text alignment.
setPadding()
Set the padding.
setPaddingBottom()
Set the bottom padding.
setPaddingLeft()
Set the left padding.
setPaddingRight()
Set the right padding.
setPaddingTop()
Set the top padding.
setTextWidth()
Set the width of the text in the rich-text stamp. Padding is not included in this width.
Border and Background
getBackgroundColor()
Get the background color object.
getBorderColor()
Get the border color object.
getBorderWidth()
Get the border width.
setBackgroundColor()
Set the background color.
setBorderColor()
Set the border color.
setBorderWidth()
Set the border width.
Examples
Following some examples showing you some ideas of how to style a rich-text stamp: