Different Font-Styles
A simple demo showing the usage of different font styles (bold, italic, underline) with the use of a rich-text stamp.
PHP
<?php // load and register the autoload function require_once __DIR__ . '/../../../../../../bootstrap.php'; // create a writer $writer = new \SetaPDF_Core_Writer_Http('styled.pdf', true); // get a document instance $document = \SetaPDF_Core_Document::loadByFilename( $assetsDirectory . '/pdfs/lenstown/Laboratory-Report.pdf', $writer ); /* Font styles are done by using different font programs for each style. To * benefit from font subsetting, we need to create a callback that will create * the right font instances for us. See FontLoader.php for details: */ require_once $classesDirectory . '/FontLoader.php'; $fontLoader = new \com\setasign\SetaPDF\Demos\FontLoader($assetsDirectory); // now simply create a stam instance $stamp = new \SetaPDF_Stamper_Stamp_RichText($document, $fontLoader); $stamp->setDefaultFontFamily('DejaVuSans'); $stamp->setDefaultFontSize(10); // pass an HTML like text to format the output $stamp->setText(<<<HTML This document is licensed to <b><u>test@example.com</u></b> and was created on <i>www.setasign.com</i>. HTML ); // create a stamper instance $stamper = new \SetaPDF_Stamper($document); // pass the stamp instance $stamper->addStamp($stamp, [ 'position' => \SetaPDF_Stamper::POSITION_CENTER_TOP, 'translateY' => -5 ]); // stamp the document $stamper->stamp(); // save and send it to the client $document->save()->finish();
PHP
<?php namespace com\setasign\SetaPDF\Demos; /** * This is a simple, straight forward font-loader implementation. * It should give you an idea of how to create your own. */ class FontLoader { /** * @var \SetaPDF_Core_Font_Type0_Subset[] */ protected $loadedFonts = []; /** * @var string */ protected $assetsDirectory; /** * @param $assetsDirectory * @param $loadedFonts */ public function __construct($assetsDirectory, &$loadedFonts = []) { $this->assetsDirectory = $assetsDirectory; $this->loadedFonts = &$loadedFonts; } /** * This is the method that is called when a font is requested. * * @param \SetaPDF_Core_Document $document * @param string $fontFamily * @param string $fontStyle * @return \SetaPDF_Core_Font_Type0_Subset */ public function __invoke(\SetaPDF_Core_Document $document, $fontFamily, $fontStyle) { $cacheKey = $document->getInstanceIdent() . '_' . $fontFamily . '_' . $fontStyle; if (!array_key_exists($cacheKey, $this->loadedFonts)) { $dejaVufontPath = $this->assetsDirectory . '/fonts/DejaVu/ttf/DejaVuSans'; if ($fontFamily === 'DejaVuSans' && $fontStyle === 'B') { $font = new \SetaPDF_Core_Font_Type0_Subset($document, $dejaVufontPath . '-Bold.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'I') { $font = new \SetaPDF_Core_Font_Type0_Subset($document, $dejaVufontPath . '-Oblique.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'BI') { $font = new \SetaPDF_Core_Font_Type0_Subset($document, $dejaVufontPath . '-BoldOblique.ttf'); } else { $font = new \SetaPDF_Core_Font_Type0_Subset($document, $dejaVufontPath . '.ttf'); } $this->loadedFonts[$cacheKey] = $font; } return $this->loadedFonts[$cacheKey]; } }