fix image text
[pear] / XML / SvgToPdf.php
1 <?php
2
3 /* usage:
4        (used by Hebe)
5        *install Fpdf as a PEAR package by:
6         pear install http://www.akbkhome.com:81/svn/akpear/Fpdf/Fpdf-1.51.tgz
7      
8        
9         $data=array(
10             
11             'address' => array(
12                 array(
13                     'name' => = "Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx";
14                 ),
15                 array(
16                     'name' => = "Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx";
17                 ),
18                 array(
19                     'name' => = "Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx Xxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxx";
20                 ),
21         );
22         $pdf = XML_SvgToPDF::construct(dirname(__FILE__).'/test.svg',$data);
23         
24         $pdf->output();
25         
26         
27         
28         expects an svg file (probably made by sodipodi)
29         a block is the group, 
30         a) it has a text component with
31             dynamic=address
32             rows=7
33             cols=3
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;
38         
39         
40         
41         
42 */
43
44  require_once 'XML/SvgToPdf/Base.php';
45
46 // current options for generated file..
47
48 $GLOBALS['_XML_SVGTOPDF']['options'] = array(
49     'debug' => 0,
50     );
51  
52         
53 class XML_SvgToPDF {
54
55
56     var $language;
57     static function debug($s,$e=0)
58     {
59         if (!$GLOBALS['_XML_SVGTOPDF']['options']['debug']) {
60            return;
61         }
62         echo "<PRE>".print_R($s,true). "</PRE>";
63         if ($e) { 
64             exit; 
65         }
66     }
67     
68     
69     /**
70      * 
71      * Static ! not sure why!?
72      * 
73      */
74     static function construct($svg, $data=array()) 
75     {  
76         if (is_object($data)) {
77             $data = (array) $data;
78         }
79       
80         $t = new XML_SvgToPDF;
81         
82         $t->language = @$data['language'];
83        /*
84         $x = new XML_Tree_Morph( 
85                     $svg,
86                     array(
87                        'debug' => 0,
88                        'filter' => array(
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'),
98                         )
99                     )
100                  );
101         
102         $tree = $x->getTreeFromFile();
103               
104         $tree = $t->buildobject($tree);
105         */
106          //echo "<PRE>";
107         $tree = $t->parseSvg($svg);
108        //echo "<PRE>";print_r($tree);exit;
109         
110         
111         
112         
113         
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;
118
119          
120           //die("trying chinese");
121         require_once  'Fpdf/tFPDF.php' ;
122
123         $pdf = new tFPDF($orientation ,'mm','A4');
124         $pdf->AddFont('ARIALUNI','','ARIALUNI.ttf',true);
125         //print_R($pdf);exit;
126 // 
127         $pdf->open();            
128         
129          $pdf->setAutoPageBreak(false);
130         $pdf->AliasNbPages();
131         // convert data to array.
132         if (is_object($data)) {
133             $data = (array) $data;
134         }
135          
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])) {
140 //         print_r("<PRE>");
141 //          print_r($data['transactions'][0]);
142 //          $data['transactions'][0]->desc = 'abcdefghijklmnopqrstuvwxyz Z';
143 //           print_r($data['transactions'][0]->desc);
144         // no dynamic blocks:
145         
146         
147          if (!$perPage || empty($data)) {
148             $pdf->addPage();
149             $tree->writePDF($pdf,$data);
150             $t->debug($tree);
151             return $pdf;
152         }
153         // build blocks of data for each page.
154         $haveData = true;
155         $page = 0;
156         
157        //    $originalData = $data;
158         //$alldata = $data[$var];
159
160        //  while (count($alldata))  {
161         //print_r($perPage);exit;
162         
163         
164         
165          
166         while (true == $haveData ) {
167             $page_data = $data;
168             $haveData = false;
169             //print_r($perPage);
170             
171             // replaces the properties that have 'page data'
172             
173             
174             foreach($perPage as $k=>$v) {
175                 if (empty($data[$k])) {
176                     $page_data[$k] = array();
177                     continue;
178                 }
179                 $haveData = true;
180                 $page_data[$k] = self::fetchRows($data,$k,$v);
181                 
182                 
183                 //$page_data[$k] = array_splice ( $data[$k], 0,$v);
184             }
185             
186            
187              
188             if ($page && !$haveData) {
189                 break;
190             }
191             $page++;
192                 
193             $t->debug("<B>PAGE $page<B>");
194          
195             
196             $pdf->addPage();
197             $tree->writePDF($pdf,$page_data);
198             
199             //$tree->writePDF($pdf,$data);
200         }
201        
202         $t->debug($tree);
203         return $pdf;
204     }
205     
206     static function fetchRows(&$original_data, $key, $rows) {
207         $ret = array();
208         while ($rows > -1 && !empty($original_data[$key])) {
209             $addrow = array_shift($original_data[$key]);
210             $rows -= !empty($addrow->userows) ? $addrow->userows : 1;
211             if ($rows < 0) {
212                 array_unshift($original_data[$key],$addrow);
213                 break;
214             }
215             $ret[] = $addrow;
216             
217         }
218         return $ret;
219         
220         
221     }
222     
223     
224     
225     function parseSvg($svgFileName)
226     {
227         $d = new DOMDocument();
228         $d->load($svgFileName);
229        // print_r($d);
230         return $this->parseNode($d->documentElement);
231     }
232     
233     function parseNode($n)
234     {
235         // do children first..
236         //print_r(array("PARSENODE:",$n));
237         $children = array();
238         if ($n->childNodes->length) {
239             foreach($n->childNodes as $cn) {
240                 if ($cn->nodeType != XML_ELEMENT_NODE) {
241                     continue;
242                 }
243                 $child = $this->parseNode($cn);
244                 if (is_array($child) && count($child)) {
245                     $children = array_merge($children, $child);
246                     continue;
247                 } 
248                 if (is_object($child)) {
249                     $children[] = $child;
250                 }
251                 continue;
252                 
253             }
254         }
255         if (!in_array($n->tagName, array('svg','image','text', 'tspan', 'rect', 'g', 'path'))) {
256             return $children;
257             
258         }
259         $ret = $this->buildObject($n,$children);
260         
261         return $ret;
262     }
263       
264     
265     
266     
267     
268     
269     function buildNull($node) {
270         return;
271     }
272     function buildObject($node, $children)
273     {
274         $class = 'XML_SvgToPDF_'.$node->tagName;
275         /*
276         if (strlen(trim($node->content)) && (@$this->language)) {
277             $node->language = $this->language;
278         }
279         */
280  
281
282         //echo "look for $class?";
283         if (!class_exists($class)) {
284             // try loading it..
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';
289             }
290         }
291         // now if it doesnt exist..
292         if (!class_exists($class)) {
293             $this->debug("can not find $class");
294            $class = 'XML_SvgToPDF_Base';
295         }
296         $r = new $class;
297         $r->children = $children;
298         $r->fromXmlNode($node);
299         return $r;
300     }
301     
302     
303
304
305 }