Advanced Styling
This PHP demo shows how to style the rich-text stamp in view to colors, background and borders for use in a PDF document.
PHP
<?php use setasign\SetaPDF2\Demos\FontLoader; use setasign\SetaPDF2\Core\DataStructure\Color\Rgb; use setasign\SetaPDF2\Core\Document; use setasign\SetaPDF2\Core\Writer\HttpWriter; use setasign\SetaPDF2\Stamper\Stamp\RichTextStamp; use setasign\SetaPDF2\Stamper\Stamper; // load and register the autoload function require_once __DIR__ . '/../../../../../../bootstrap.php'; $files = [ $assetsDirectory . '/pdfs/Brand-Guide.pdf', $assetsDirectory . '/pdfs/Fuchslocher-Example.pdf', ]; $path = displayFiles($files); $writer = new HttpWriter('stamped.pdf', true); $document = Document::loadByFilename($path, $writer); // create a stamper instance $stamper = new Stamper($document); require_once $classesDirectory . '/FontLoader.php'; $fontLoader = new FontLoader($assetsDirectory); // create a rich-text stamp instance $stamp = new RichTextStamp($document, $fontLoader); $stamp->setDefaultFontFamily('DejaVuSans'); $stamp->setText('Personalized for <b style="color:#0f0f0f;">john@example.com</b>'); // set the border color to gray (e.g. as a hex value) $stamp->setBorderColor('#c7c7c7'); // and width $stamp->setBorderWidth(1); // set background color to light-gray (e.g. as an array of RGB values (0 - 1) $stamp->setBackgroundColor([.95, .95, .95]); // set padding $stamp->setPadding(3); // set default text color by an explicit color instance $stamp->setDefaultTextColor(new Rgb(56/255, 101/255, 174/255)); // add the stamp to the stamper instance $stamper->addStamp($stamp, [ 'position' => Stamper::POSITION_CENTER_BOTTOM, 'translateY' => 15 ]); // stamp the document with all previously added stamps $stamper->stamp(); // save and finish the document instance $document->save()->finish();
PHP
<?php namespace setasign\SetaPDF2\Demos; use setasign\SetaPDF2\Core\Document; use setasign\SetaPDF2\Core\Font\Type0\Subset; /** * This is a simple, straight forward font-loader implementation. * It should give you an idea of how to create your own. */ class FontLoader { /** * @var 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 Document $document * @param string $fontFamily * @param string $fontStyle * @return Subset */ public function __invoke(Document $document, string $fontFamily, string $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 Subset($document, $dejavuFontPath . '-Bold.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'I') { $font = new Subset($document, $dejavuFontPath . '-Oblique.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'BI') { $font = new Subset($document, $dejavuFontPath . '-BoldOblique.ttf'); } else { $font = new Subset($document, $dejavuFontPath . '.ttf'); } $this->loadedFonts[$cacheKey] = $font; } return $this->loadedFonts[$cacheKey]; } }
