58700aa5bcc28d7edcd3a5f2aa6bc9ec5f0ccd35
[pear] / XML / SvgToPdf.php
1 <?php
2
3 /* usage:
4      
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/Tree/Morph.php';
45 require_once 'Fpdf/tFPDF.php'; 
46 require_once 'XML/SvgToPdf/Base.php';
47
48 // current options for generated file..
49
50 $GLOBALS['_XML_SVGTOPDF']['options'] = array(
51     'debug' => 0,
52     );
53  
54         
55 class XML_SvgToPDF {
56
57     var $language;
58     
59     static function debug($s,$e=0) {
60         if (!$GLOBALS['_XML_SVGTOPDF']['options']['debug']) {
61             return;
62         }
63         echo "<PRE>".print_R($s,true). "</PRE>";
64         if ($e) { 
65             exit; 
66         }
67     }
68     
69     
70     /**
71      * 
72      * Static ! not sure why!?
73      * 
74      */
75     static function construct($svg, $data=array()) 
76     {  
77         if (is_object($data)) {
78             $data = (array) $data;
79         }
80       
81         $t = new XML_SvgToPDF;
82         
83         $t->language =  $data['language'];
84        /*
85         $x = new XML_Tree_Morph( 
86                     $svg,
87                     array(
88                        'debug' => 0,
89                        'filter' => array(
90                            'svg'    => array(&$t, 'buildObject'),
91                            'image'    => array(&$t, 'buildObject'),
92                            'text'    => array(&$t, 'buildObject'),
93                            'tspan'   => array(&$t, 'buildObject'),
94                            'rect'   => array(&$t, 'buildObject'),
95                            'g'   =>  array(&$t, 'buildObject'),
96                            'path'   =>  array(&$t, 'buildObject'),
97                            'sodipodi:namedview' =>  array(&$t, 'buildNull'),
98                            'defs' =>  array(&$t, 'buildNull'),
99                         )
100                     )
101                  );
102         
103         $tree = $x->getTreeFromFile();
104               
105         $tree = $t->buildobject($tree);
106         */
107          //echo "<PRE>";
108         $tree = $t->parseSvg($svg);
109         //echo "<PRE>";print_r($tree);exit;
110         
111         
112         
113         
114         
115         //echo "<PRE>";print_r($tree);exit;
116         $w = empty($tree->width) ? 0 : $tree->width;
117         $h = empty($tree->height) ? 0 : $tree->height;
118         $orientation =  (preg_replace('/[^0-9.]+/','', $w)*1) > (preg_replace('/[^0-9.]+/','', $h)*1) ? 'L' : 'P';
119         //var_dump($orientation);exit;
120         $GLOBALS['_XML_SVGTOPDF']['options']['file'] = $svg;
121
122         if ($data['language'] == 'big5') {
123           //die("trying chinese");
124             require_once  'Fpdf/tFPDF.php' ;
125
126             $pdf = new tFPDF($orientation ,'mm','A4');
127             
128             // we originally used ARIALUNI.ttf'
129             
130             $font = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf';
131             
132             if (!file_exists('/usr/share/fonts/truetype/msttcorefonts/Arial.ttf')) {
133                 die("install msttcorefonts package");
134             }
135             
136             //$pdf->AddFont('ARIALUNI','',$font,true);
137             
138             $pdf->AddFont('ARIALUNI','','/usr/share/fonts/truetype/msttcorefonts/Arial.ttf',true);
139             $pdf->AddFont('ARIALUNI','B','/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf',true);
140             
141 //            $pdf->SetFont('ARIALUNI','',14);
142 //            require_once 'Fpdf/Chinese-unicode.php';
143 //
144 //            $pdf=new PDF_Unicode($orientation ,'mm','A4');
145 ////            $pdf->AddGBFont();
146 ////            $pdf->AddBig5Font();
147 //            $pdf->AddUniCNSFont('Uni');
148             //$pdf->AddUniCNSFont('Uni'); 
149             //AddUniCNShwFont
150             $pdf->open();            
151         } else {
152             $pdf=new tFPDF($orientation ,'mm','A4');
153             $pdf->open();
154         }
155
156         $pdf->setAutoPageBreak(false);
157         $pdf->AliasNbPages();
158         // convert data to array.
159         if (is_object($data)) {
160             $data = (array) $data;
161         }
162         // assoc. array of key => no of fields per page.
163          $perPage = $tree->calcPerPage();
164         //list($var,$perpage) = $tree->calcPerPage();
165         //if (empty($data) || !@$var || !@count($data[$var])) {
166 //         print_r("<PRE>");
167 //          print_r($data['transactions'][0]);
168 //          $data['transactions'][0]->desc = 'abcdefghijklmnopqrstuvwxyz Z';
169 //           print_r($data['transactions'][0]->desc);
170         // no dynamic blocks:
171          if (!$perPage || empty($data)) {
172             $pdf->addPage();
173             $tree->writePDF($pdf,$data);
174             $t->debug($tree);
175             return $pdf;
176         }
177         
178         // build blocks of data for each page.
179         $haveData = true;
180         $page = 0;
181         
182        //    $originalData = $data;
183         //$alldata = $data[$var];
184
185        //  while (count($alldata))  {
186         //print_r($perPage);exit;
187         
188         
189         
190          
191         while (true == $haveData ) {
192             $page_data = $data;
193             $haveData = false;
194             //print_r($perPage);
195             
196             // replaces the properties that have 'page data'
197             
198             
199             foreach($perPage as $k=>$v) {
200                 if (empty($data[$k])) {
201                     $page_data[$k] = array();
202                     continue;
203                 }
204                 $haveData = true;
205                 $page_data[$k] = self::fetchRows($data,$k,$v);
206                 
207                 
208                 //$page_data[$k] = array_splice ( $data[$k], 0,$v);
209             }
210             
211            
212              
213             if ($page && !$haveData) {
214                 break;
215             }
216             $page++;
217                 
218             $t->debug("<B>PAGE $page<B>");
219          
220             
221             $pdf->addPage();
222             $tree->writePDF($pdf,$page_data);
223             
224             //$tree->writePDF($pdf,$data);
225         }
226        
227         $t->debug($tree);
228         return $pdf;
229     }
230     
231     function fetchRows(&$original_data, $key, $rows) {
232         $ret = array();
233         while ($rows > -1 && !empty($original_data[$key])) {
234             $addrow = array_shift($original_data[$key]);
235             $rows -= !empty($addrow->userows) ? $addrow->userows : 1;
236             if ($rows < 0) {
237                 array_unshift($original_data[$key],$addrow);
238                 break;
239             }
240             $ret[] = $addrow;
241             
242         }
243         return $ret;
244         
245         
246     }
247     
248     
249     
250     function parseSvg($svgFileName)
251     {
252         libxml_use_internal_errors(true);
253         $d = new DOMDocument();
254         $d->load($svgFileName);
255        // print_r($d);
256         return $this->parseNode($d->documentElement);
257     }
258     
259     function parseNode($n)
260     {
261         // do children first..
262         //print_r(array("PARSENODE:",$n));
263         if (empty($n)) {
264             return array();
265         }
266         $children = array();
267         if ($n->childNodes->length) {
268             foreach($n->childNodes as $cn) {
269                 if ($cn->nodeType != XML_ELEMENT_NODE) {
270                     continue;
271                 }
272                 $child = $this->parseNode($cn);
273                 if (is_array($child) && count($child)) {
274                     $children = array_merge($children, $child);
275                     continue;
276                 } 
277                 if (is_object($child)) {
278                     $children[] = $child;
279                 }
280                 continue;
281                 
282             }
283         }
284         if (!in_array($n->tagName, array('svg','image','text', 'tspan', 'rect', 'g', 'path'))) {
285             return $children;
286             
287         }
288         $ret = $this->buildObject($n,$children);
289         
290         return $ret;
291     }
292       
293     
294     
295     
296     
297     
298     function buildNull($node) {
299         return;
300     }
301     function buildObject($node, $children) {
302         $class = 'XML_SvgToPDF_'.$node->tagName;
303         /*
304         if (strlen(trim($node->content)) && (@$this->language)) {
305             $node->language = $this->language;
306         }
307         */
308  
309
310         //echo "look for $class?";
311         if (!class_exists($class)) {
312             // try loading it..
313             $file = dirname(__FILE__) . '/SvgToPdf/'.ucfirst(strtolower($node->tagName)). '.php';
314             $this->debug("loading  $file");
315             if (file_exists($file)) {
316                 require_once 'XML/SvgToPdf/'.ucfirst(strtolower($node->tagName)) . '.php';
317             }
318         }
319         // now if it doesnt exist..
320         if (!class_exists($class)) {
321             $this->debug("can not find $class");
322            $class = 'XML_SvgToPDF_Base';
323         }
324         $r = new $class;
325         $r->children = $children;
326         $r->fromXmlNode($node);
327         return $r;
328     }
329     
330     
331
332
333 }