5 *install Fpdf as a PEAR package by:
6 pear install http://www.akbkhome.com:81/svn/akpear/Fpdf/Fpdf-1.51.tgz
13 'name' => = "Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx";
16 'name' => = "Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx";
19 'name' => = "Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx";
22 $pdf = XML_SvgToPDF::construct(dirname(__FILE__).'/test.svg',$data);
28 expects an svg file (probably made by sodipodi)
30 a) it has a text component with
34 b) a non-printable rectangle (which is the bounding box)
35 c) some text {xxxx}{yyyy} which is replaced with
36 address[0....][xxxx] = 'somevalue;
37 address[0....][yyyy] = 'somevalue;
44 require_once 'XML/SvgToPdf/Base.php';
46 // current options for generated file..
48 $GLOBALS['_XML_SVGTOPDF']['options'] = array(
57 static function debug($s,$e=0)
59 if (!$GLOBALS['_XML_SVGTOPDF']['options']['debug']) {
62 echo "<PRE>".print_R($s,true). "</PRE>";
71 * Static ! not sure why!?
74 static function construct($svg, $data=array())
76 if (is_object($data)) {
77 $data = (array) $data;
80 $t = new XML_SvgToPDF;
82 $t->language = @$data['language'];
84 $x = new XML_Tree_Morph(
89 'svg' => array(&$t, 'buildObject'),
90 'image' => array(&$t, 'buildObject'),
91 'text' => array(&$t, 'buildObject'),
92 'tspan' => array(&$t, 'buildObject'),
93 'rect' => array(&$t, 'buildObject'),
94 'g' => array(&$t, 'buildObject'),
95 'path' => array(&$t, 'buildObject'),
96 'sodipodi:namedview' => array(&$t, 'buildNull'),
97 'defs' => array(&$t, 'buildNull'),
102 $tree = $x->getTreeFromFile();
104 $tree = $t->buildobject($tree);
107 $tree = $t->parseSvg($svg);
108 //echo "<PRE>";print_r($tree);exit;
114 //echo "<PRE>";print_r($tree);exit;
115 $orientation = (preg_replace('/[^0-9.]+/','', $tree->width)*1) > (preg_replace('/[^0-9.]+/','', $tree->height)*1) ? 'L' : 'P';
116 //var_dump($orientation);exit;
117 $GLOBALS['_XML_SVGTOPDF']['options']['file'] = $svg;
120 //die("trying chinese");
121 require_once 'Fpdf/tFPDF.php' ;
123 $pdf = new tFPDF($orientation ,'mm','A4');
124 $pdf->AddFont('ARIALUNI','','ARIALUNI.ttf',true);
125 //print_R($pdf);exit;
129 $pdf->setAutoPageBreak(false);
130 $pdf->AliasNbPages();
131 // convert data to array.
132 if (is_object($data)) {
133 $data = (array) $data;
136 // assoc. array of key => no of fields per page.
137 $perPage = $tree->calcPerPage();
138 //list($var,$perpage) = $tree->calcPerPage();
139 //if (empty($data) || !@$var || !@count($data[$var])) {
141 // print_r($data['transactions'][0]);
142 // $data['transactions'][0]->desc = 'abcdefghijklmnopqrstuvwxyz Z';
143 // print_r($data['transactions'][0]->desc);
144 // no dynamic blocks:
147 if (!$perPage || empty($data)) {
149 $tree->writePDF($pdf,$data);
153 // build blocks of data for each page.
157 // $originalData = $data;
158 //$alldata = $data[$var];
160 // while (count($alldata)) {
161 //print_r($perPage);exit;
166 while (true == $haveData ) {
171 // replaces the properties that have 'page data'
174 foreach($perPage as $k=>$v) {
175 if (empty($data[$k])) {
176 $page_data[$k] = array();
180 $page_data[$k] = self::fetchRows($data,$k,$v);
183 //$page_data[$k] = array_splice ( $data[$k], 0,$v);
188 if ($page && !$haveData) {
193 $t->debug("<B>PAGE $page<B>");
197 $tree->writePDF($pdf,$page_data);
199 //$tree->writePDF($pdf,$data);
206 static function fetchRows(&$original_data, $key, $rows) {
208 while ($rows > -1 && !empty($original_data[$key])) {
209 $addrow = array_shift($original_data[$key]);
210 $rows -= !empty($addrow->userows) ? $addrow->userows : 1;
212 array_unshift($original_data[$key],$addrow);
225 function parseSvg($svgFileName)
227 $d = new DOMDocument();
228 $d->load($svgFileName);
230 return $this->parseNode($d->documentElement);
233 function parseNode($n)
235 // do children first..
236 //print_r(array("PARSENODE:",$n));
238 if ($n->childNodes->length) {
239 foreach($n->childNodes as $cn) {
240 if ($cn->nodeType != XML_ELEMENT_NODE) {
243 $child = $this->parseNode($cn);
244 if (is_array($child) && count($child)) {
245 $children = array_merge($children, $child);
248 if (is_object($child)) {
249 $children[] = $child;
255 if (!in_array($n->tagName, array('svg','image','text', 'tspan', 'rect', 'g', 'path'))) {
259 $ret = $this->buildObject($n,$children);
269 function buildNull($node) {
272 function buildObject($node, $children)
274 $class = 'XML_SvgToPDF_'.$node->tagName;
276 if (strlen(trim($node->content)) && (@$this->language)) {
277 $node->language = $this->language;
282 //echo "look for $class?";
283 if (!class_exists($class)) {
285 $file = dirname(__FILE__) . '/SvgToPdf/'.ucfirst(strtolower($node->tagName)). '.php';
286 $this->debug("loading $file");
287 if (file_exists($file)) {
288 require_once 'XML/SvgToPdf/'.ucfirst(strtolower($node->tagName)) . '.php';
291 // now if it doesnt exist..
292 if (!class_exists($class)) {
293 $this->debug("can not find $class");
294 $class = 'XML_SvgToPDF_Base';
297 $r->children = $children;
298 $r->fromXmlNode($node);