Styles
A Style binds a font, a size and a color to a name of its own, and addText() takes that name in
place of a font name — so every heading in a document changes by changing one object. Styles live on
the Document alongside its fonts, and a style names a standard font and an embedded one the same way.
Registering a Style#
createStyle() names a font and a size on the document, and addText() picks up both at once:
use Pop\Pdf\Document;
use Pop\Pdf\Document\Page;
use Pop\Pdf\Document\Page\Text;
use Pop\Pdf\Document\Font;
$document = new Document(new Page(Page::LETTER));
$document->addFont(Font::HELVETICA_BOLD);
$document->createStyle('heading', font: Font::HELVETICA_BOLD, size: 20);
$page = $document->getPage(1);
$page->addText(new Text('Report'), 'heading', x: 100, y: 700);
createStyle() and addStyle() both register a style:
createStyle()builds theStylefor you from a name, an optional font and an optional sizeaddStyle()takes aStyleinstance directly, built withnew Style(...)orStyle::create(...)- registering a style under a name already taken is a no-op, the same as
addFont() getAvailableStyles()lists every registered name;getStyle()hands back the object itself
$document->getAvailableStyles(); // ['heading']
$style = $document->getStyle('heading');
$style->getFont(); // 'Helvetica-Bold'
$style->getSize(); // 20
At compile time, a $fontStyle argument is checked against the document's styles before its fonts:
- a style's size, if it has one, overrides whatever size the
Textitself was given - a style's font, if it has one, is what actually gets resolved
- a style's color, if it has one, overrides whatever
setFillColor()theTextwas given - a style built with no font (
hasFont()returnsfalse) contributes only a size and a color — its name then has to also be the name of a real, separately-registered font, riding on that font rather than supplying one
addStyles() registers several at once, the same batch shape addFonts() gives fonts:
use Pop\Pdf\Document\Style;
$document->addFonts([Font::HELVETICA_BOLD, Font::HELVETICA]);
$document->addStyles([
Style::create('heading', font: Font::HELVETICA_BOLD, size: 20),
Style::create('body', font: Font::HELVETICA, size: 11),
]);
$document->getAvailableStyles(); // ['heading', 'body']
A style still needs its font registered separately — naming a font in a Style doesn't register it
on the document, it only tells addText() which already-registered font to resolve once the style is
named.
Style Color#
A style can also carry a color, which becomes the fill color of every string drawn under that name:
use Pop\Color\Color\Rgb;
use Pop\Pdf\Document;
use Pop\Pdf\Document\Font;
use Pop\Pdf\Document\Page;
use Pop\Pdf\Document\Page\Text;
use Pop\Pdf\Document\Style;
$document = new Document(new Page(Page::LETTER));
$document->addFont(Font::HELVETICA_BOLD);
$document->addStyle(
Style::create('heading', font: Font::HELVETICA_BOLD, size: 20, color: new Rgb(0, 102, 204))
);
$page = $document->getPage(1);
$page->addText(new Text('Report'), 'heading', x: 100, y: 700);
Rgb, Cmyk and Grayscale all work, the same three Text itself takes. The
color is written inside its own graphics-state save and restore, so it paints the text it belongs to
and nothing drawn after it.
createStyle() takes no color argument — hand it a Style built with one, or set the color on a style
already registered:
$document->addFont(Font::HELVETICA);
$document->createStyle('body', font: Font::HELVETICA, size: 11);
$document->getStyle('body')->setColor(new Rgb(80, 80, 80));
$document->getStyle('body')->hasColor(); // true
Styling an Embedded Font#
An embedded font goes into a style by getName(), the same name addText() would take on its own:
use Pop\Color\Color\Rgb;
use Pop\Pdf\Document;
use Pop\Pdf\Document\Font;
use Pop\Pdf\Document\Page;
use Pop\Pdf\Document\Page\Text;
use Pop\Pdf\Document\Style;
$document = new Document(new Page(Page::LETTER));
$font = new Font(__DIR__ . '/fonts/DejaVuSans.ttf');
$document->embedFont($font);
$font->getName(); // 'DejaVuSans' — parsed from the file, not the filename
$document->addStyle(
Style::create('heading', font: $font->getName(), size: 20, color: new Rgb(0, 102, 204))
);
$page = $document->getPage(1);
$page->addText(new Text('Report'), 'heading', x: 72, y: 700);
Size and color resolve exactly as they do over a standard font — the only difference is downstream, in how the embedded font writes the string to the page. Embedding Fonts covers that side.
See Also#
- Fonts — registering the standard font a style names, and what a document has available
- Embedding Fonts — loading a font file whose
getName()a style can carry - Adding Text — the
$fontStyleargument everyaddText()call resolves against a registered font or style - Creating Documents — where a document's styles live alongside its fonts and pages