function construct($svg, $data = array()) { $t = new XML_SvgToPDF(); $x = new XML_Tree_Morph($svg, array('debug' => 0, 'filter' => array('svg' => array(&$t, 'buildObject'), 'text' => array(&$t, 'buildObject'), 'tspan' => array(&$t, 'buildObject'), 'rect' => array(&$t, 'buildObject'), 'g' => array(&$t, 'buildObject'), 'path' => array(&$t, 'buildObject'), 'sodipodi:namedview' => array(&$t, 'buildNull'), 'defs' => array(&$t, 'buildNull')))); $tree = $x->getTreeFromFile(); $tree = $t->buildobject($tree); $orientation = $tree->width > $tree->height ? 'L' : 'P'; $pdf = new FPDF($orientation, 'mm', 'A4'); $pdf->open(); $pdf->setAutoPageBreak(false); $pdf->AliasNbPages(); // convert data to array. if (is_object($data)) { $data = (array) $data; } // no data page.. if (empty($data)) { $pdf->addPage(); $tree->writePDF($pdf, $data); $t->debug($tree); return $pdf; } // work out how many pages... list($var, $perpage) = $tree->calcPerPage(); if ($var == '') { die('No dynamic group found'); } if (!isset($data[$var])) { die('Incorrect dynamic group name'); } $alldata = $data[$var]; $page = 0; while (count($alldata)) { $page++; $t->debug("<B>PAGE {$page}<B>"); $page_data = array_splice($alldata, 0, $perpage); $data[$var] = $page_data; $pdf->addPage(); $tree->writePDF($pdf, $data); } $t->debug($tree); return $pdf; }
function writePDF($pdf, $data) { //print_r(array("rect:", $this->x , $this->y , ':', $this->xx, $this->yy)); $x = $this->x + @$this->xx; $y = $this->y + @$this->yy; $pdf->setLineWidth($this->style['stroke-width']); $f = $this->toColor($this->style['fill']); if ($f) { $pdf->setFillColor($f[0], $f[1], $f[2]); } $l = $this->toColor(@$this->style['stroke']); if ($l) { $pdf->setDrawColor($l[0], $l[1], $l[2]); } // no fill, no line = dont draw... if (!$l && !$f) { return; } XML_SvgToPDF::debug("RECT:" . $x / 3.543307 . ',' . $y / 3.543307 . ',' . $this->width / 3.543307 . ',' . $this->height / 3.543307); $pdf->rect($x / 3.543307, $y / 3.543307, $this->width / 3.543307, $this->height / 3.543307, ($l ? 'D' : '') . ($f ? 'F' : '')); }
<?php require 'SvgToPDF.php'; //Print 15 labels $label = array(); for ($i = 1; $i <= 15; $i++) { $label[] = array('name' => "Name {$i}", 'address' => "Address {$i}", 'city' => "City {$i}"); } $pdf = XML_SvgToPDF::construct('ex.svg', array('label' => $label)); $pdf->Output();
function writePDF($pdf, &$data) { // g group = does it have a // look for if (empty($this->children)) { return; } // not dynamic.. -> just dump.. if (empty($this->settings)) { return $this->childrenWritePDF($pdf, $data); } $use = false; if (substr($this->settings['dynamic'], -2, 2) == '()') { $use = $data->{substr($this->settings['dynamic'], 0, -2)}(); } else { $use = empty($data[$this->settings['dynamic']]) ? '' : $data[$this->settings['dynamic']]; } if (empty($use)) { return; } // if use is a value - make it an array with a single element, so that the bounding box // additions apply.. if (!is_array($use)) { $use = array($use); } // echo "<PRE>";print_r($boundingbox ); $this->x = $x = $this->boundingbox->x; $this->y = $y = $this->boundingbox->y; $w = $this->boundingbox->width; $h = $this->boundingbox->height; //echo '<PRE>';print_r($this);exit; // shift... ** this does not handle groups!!! //print_R($use); $keys = array_keys($use); $kpos = 0; $kmax = count($keys); //XML_SvgToPDF::debug(array($x,$y,$w,$h)); //XML_SvgToPDF::debug($keys); XML_SvgToPDF::debug($this->settings); $yy = $y; for ($r = 0; $r < $this->settings['rows']; $r++) { $record = $use[$keys[$kpos]]; for ($c = 0; $c < $this->settings['cols']; $c++) { $record = $use[$keys[$kpos]]; $xx = $x + $c * $w; XML_SvgToPDF::debug(array($xx, $yy)); foreach (array_keys($this->children) as $k) { if (!$this->children[$k]) { continue; } // if (is_object($use[$keys[$kpos]]) && method_exists($use[$keys[$kpos]], 'loadSvg')) { // // set the defaults, as we cant do it in the thing now.. // $use[$keys[$kpos]]->loadSvg(); // } $this->children[$k]->xx = $xx; $this->children[$k]->yy = $yy; $this->children[$k]->maxWidth = $w - 20; $this->children[$k]->writePDF($pdf, $use[$keys[$kpos]]); } $kpos++; if ($kpos >= $kmax) { break 2; } } $yy += !empty($record->userows) ? $record->userows * $h : $h; } }
/** * * Static ! not sure why!? * */ static function construct($svg, $data = array()) { if (is_object($data)) { $data = (array) $data; } $t = new XML_SvgToPDF(); $t->language = @$data['language']; /* $x = new XML_Tree_Morph( $svg, array( 'debug' => 0, 'filter' => array( 'svg' => array(&$t, 'buildObject'), 'image' => array(&$t, 'buildObject'), 'text' => array(&$t, 'buildObject'), 'tspan' => array(&$t, 'buildObject'), 'rect' => array(&$t, 'buildObject'), 'g' => array(&$t, 'buildObject'), 'path' => array(&$t, 'buildObject'), 'sodipodi:namedview' => array(&$t, 'buildNull'), 'defs' => array(&$t, 'buildNull'), ) ) ); $tree = $x->getTreeFromFile(); $tree = $t->buildobject($tree); */ //echo "<PRE>"; $tree = $t->parseSvg($svg); //echo "<PRE>";print_r($tree);exit; //echo "<PRE>";print_r($tree);exit; $orientation = preg_replace('/[^0-9.]+/', '', $tree->width) * 1 > preg_replace('/[^0-9.]+/', '', $tree->height) * 1 ? 'L' : 'P'; //var_dump($orientation);exit; $GLOBALS['_XML_SVGTOPDF']['options']['file'] = $svg; if (@$data['language'] == 'big5') { //die("trying chinese"); require_once 'Fpdf/tFPDF.php'; $pdf = new tFPDF($orientation, 'mm', 'A4'); // we originally used ARIALUNI.ttf' $font = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf'; if (!file_exists('/usr/share/fonts/truetype/msttcorefonts/Arial.ttf')) { die("install msttcorefonts package"); } //$pdf->AddFont('ARIALUNI','',$font,true); $pdf->AddFont('ARIALUNI', '', '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf', true); $pdf->AddFont('ARIALUNI', 'B', '/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', true); // $pdf->SetFont('ARIALUNI','',14); // require_once 'Fpdf/Chinese-unicode.php'; // // $pdf=new PDF_Unicode($orientation ,'mm','A4'); //// $pdf->AddGBFont(); //// $pdf->AddBig5Font(); // $pdf->AddUniCNSFont('Uni'); //$pdf->AddUniCNSFont('Uni'); //AddUniCNShwFont $pdf->open(); } else { $pdf = new tFPDF($orientation, 'mm', 'A4'); $pdf->open(); } $pdf->setAutoPageBreak(false); $pdf->AliasNbPages(); // convert data to array. if (is_object($data)) { $data = (array) $data; } // assoc. array of key => no of fields per page. $perPage = $tree->calcPerPage(); //list($var,$perpage) = $tree->calcPerPage(); //if (empty($data) || !@$var || !@count($data[$var])) { // print_r("<PRE>"); // print_r($data['transactions'][0]); // $data['transactions'][0]->desc = 'abcdefghijklmnopqrstuvwxyz Z'; // print_r($data['transactions'][0]->desc); // no dynamic blocks: if (!$perPage || empty($data)) { $pdf->addPage(); $tree->writePDF($pdf, $data); $t->debug($tree); return $pdf; } // build blocks of data for each page. $haveData = true; $page = 0; // $originalData = $data; //$alldata = $data[$var]; // while (count($alldata)) { //print_r($perPage);exit; while (true == $haveData) { $page_data = $data; $haveData = false; //print_r($perPage); // replaces the properties that have 'page data' foreach ($perPage as $k => $v) { if (empty($data[$k])) { $page_data[$k] = array(); continue; } $haveData = true; $page_data[$k] = self::fetchRows($data, $k, $v); //$page_data[$k] = array_splice ( $data[$k], 0,$v); } if ($page && !$haveData) { break; } $page++; $t->debug("<B>PAGE {$page}<B>"); $pdf->addPage(); $tree->writePDF($pdf, $page_data); //$tree->writePDF($pdf,$data); } $t->debug($tree); return $pdf; }
function writePDF(&$pdf, &$data) { // g group = does it have a // look for if (!@$this->children) { return; } if (empty($this->settings)) { return $this->childrenWritePDF($pdf, $data); } $use = false; if (substr($this->settings['dynamic'], -2, 2) == '()') { $use = $data->{substr($this->settings['dynamic'], 0, -2)}(); } else { $use = @$data[$this->settings['dynamic']]; } if (!is_array($use) || !$use) { return $this->childrenWritePDF($pdf, $data); } $this->x = $x = $this->boundingbox->x; $this->y = $y = $this->boundingbox->y; $w = $this->boundingbox->width; $h = $this->boundingbox->height; // shift... ** this does not handle groups!!! //print_R($use); $keys = array_keys($use); $kpos = 0; $kmax = count($keys); //XML_SvgToPDF::debug(array($x,$y,$w,$h)); //XML_SvgToPDF::debug($keys); XML_SvgToPDF::debug($this->settings); for ($r = 0; $r < $this->settings['rows']; $r++) { $yy = $y + $r * $h; for ($c = 0; $c < $this->settings['cols']; $c++) { $xx = $x + $c * $w; XML_SvgToPDF::debug(array($xx, $yy)); foreach (array_keys($this->children) as $k) { if (!$this->children[$k]) { continue; } $this->children[$k]->xx = $xx; $this->children[$k]->yy = $yy; $this->children[$k]->maxWidth = $w - 20; $this->children[$k]->writePDF($pdf, $use[$keys[$kpos]]); } $kpos++; if ($kpos >= $kmax) { break 2; } } } }
function multiLine(&$pdf, $lines, $x, $y, $h, $align) { // now dealing with mm XML_SvgToPDF::debug("MULTILINE " . implode("\n", $lines) . " {$x}, {$y}, {$h}"); $yoffset = 0; foreach ($lines as $l => $v) { if (@$this->maxWidth && $pdf->getStringWidth($v) > $this->maxWidth / 3.543307) { $yoffset += $this->breakLines($pdf, $v, $x, $y + $l * $h + $yoffset, $h, $align); continue; } XML_SvgToPDF::debug("TEXT: {$x},{$y}, {$l} * {$h} + {$yoffset},{$v}"); $xoffset = 0; if ($align == 'M') { // center $xoffset = -1 * ($pdf->getStringWidth($v) / 2); } $pdf->text($xoffset + $x, $y + $l * $h + $yoffset, $v); } return $l * $h + $yoffset; }
function multiLine($pdf, $lines, $x, $y, $h, $align) { // now dealing with mm ///XML_SvgToPDF::debug("MULTILINE " .implode("\n",$lines) . " $x, $y, $h"); $yoffset = 0; $line = -1; foreach ($lines as $l => $v) { $line++; if ($this->maxWidth !== false && $pdf->getStringWidth($v) > $this->maxWidth / 3.543307) { $yoffset += $this->breakLines($pdf, $v, $x, $y + $l * $h + $yoffset, $h, $align); continue; } XML_SvgToPDF::debug("TEXT: {$x},{$y}, {$l} * {$h} + {$yoffset},{$v}"); $xoffset = 0; if ($align == 'M') { // center $xoffset = -1 * ($pdf->getStringWidth($v) / 2); } if ($align == 'E') { // right/end $xoffset = -1 * $pdf->getStringWidth($v); } if ($align == 'J') { // justified (eg. started with == $this->justify($pdf, $x, $y + $l * $h + $yoffset, $v, $this->justifyLen); continue; } $pdf->text($xoffset + $x, $y + $l * $h + $yoffset, $v); } return $l * $h + $yoffset; }