public function loadXml(SimpleXMLElement $e)
 {
     $template = new Template();
     foreach ($e->blocks->children() as $type => $blockNode) {
         $blockClassName = "\\Tagliatelle\\Model\\Block\\" . ucfirst($type) . "Block";
         if (!class_exists($blockClassName)) {
             throw new RuntimeException("Unsupported block-type: " . $type);
         }
         $block = new $blockClassName();
         foreach ($blockNode->attributes() as $name => $value) {
             $setter = 'set' . ucfirst($name);
             $block->{$setter}($value);
         }
         $template->addBlock($block);
     }
     return $template;
 }
Example #2
0
 public function generate(Page $page, Template $template, $records, $label = 0)
 {
     $pdf = new TagliatelleFpdf('P', 'mm', 'A4');
     $pdf->AddPage();
     $pdf->SetFont('Arial', 'B', 16);
     $pdf->SetMargins(0, 0, 0, 0);
     $pdf->SetAutoPageBreak(false);
     foreach ($records as $record) {
         //echo $page->getLabelColumn($label) . '-' . $page->getLabelRow($label) . ' ';
         //echo $page->getLabelX($label) . '-' . $page->getLabelY($label) . "\n";
         $pdf->Rect($page->getLabelX($label), $page->getLabelY($label), $page->getLabelWidth(), $page->getLabelHeight());
         foreach ($template->getBlocks() as $block) {
             $reflect = new \ReflectionClass($block);
             $methodName = 'render' . $reflect->getShortName();
             $this->{$methodName}($block, $page, $pdf, $label, $record);
         }
         $label++;
         if ($label >= $page->getLabels()) {
             $pdf->AddPage();
             $label = 0;
         }
     }
     return $pdf->Output(null, 'S');
 }
Example #3
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use Tagliatelle\Model\Page;
use Tagliatelle\Model\Template;
use Tagliatelle\Model\Block\TextBlock;
use Tagliatelle\Model\Block\Ean13Block;
use Tagliatelle\Generator\FpdfGenerator;
$page = new Page();
$page->setWidth(200);
$page->setHeight(300);
$page->setMargin(10, 20, 10, 5);
$block = new TextBlock();
$block->setX(10);
$block->setY(10);
$block->setContent('Hello [name]');
$template = new Template();
$template->setName('Name badge');
$template->addBlock($block);
$rows = [['name' => 'Alice', 'nr' => '67890'], ['name' => 'Bob', 'nr' => '12345'], ['name' => 'Carol', 'nr' => '98989']];
$generator = new FpdfGenerator();
$data = $generator->generate($page, $template, $rows);
file_put_contents('output.pdf', $pdf);