f71ece23f7f2c5b9254f3d0af9503375cc578a1c
[pear] / XML / SvgToPdfAlt.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 'XML/SvgToPdfAlt/Base.php';
46
47 // current options for generated file..
48
49 $GLOBALS['_XML_SVGTOPDF']['options'] = array(
50     'debug' => 0,
51     );
52  
53         
54 class XML_SvgToPDFAlt {
55
56     var $language;
57     static function debug($s,$e=0) {
58         if (!$GLOBALS['_XML_SVGTOPDF']['options']['debug']) {
59             return;
60         }
61         echo "<PRE>".print_R($s,true). "</PRE>";
62         if ($e) { 
63             exit; 
64         }
65     }
66     static function construct($svg, $data=array()) {
67
68         $t = new XML_SvgToPDFAlt;
69         $t->language = @$data['language'];
70         if (empty($svg)) {
71             trigger_error(__CLASS__.':construct called without svg', E_USER_ERROR);
72         }
73        
74         $x = new XML_Tree_Morph( 
75                     $svg,
76                     array(
77                        'debug' => 0,
78                        'filter' => array(
79                            'svg'    => array($t, 'buildObject'),
80                            'image'    => array($t, 'buildObject'),
81                            'text'    => array($t, 'buildObject'),
82                            'tspan'   => array($t, 'buildObject'),
83                            'rect'   => array($t, 'buildObject'),
84                            'g'   =>  array($t, 'buildObject'),
85                            'path'   =>  array($t, 'buildObject'),
86                            'sodipodi:namedview' =>  array($t, 'buildNull'),
87                            'defs' =>  array($t, 'buildNull'),
88                            'metadata' =>  array($t, 'buildNull'),
89                            
90                         )
91                     )
92                  );
93
94         $tree = $x->getTreeFromFile();
95  
96         
97         //echo '<PRE>'.htmlspecialchars(print_r($tree,true));exit;
98         
99         $tree = $t->buildobject($tree);
100  //echo '<PRE>'.htmlspecialchars(print_r($tree,true));
101         //echo "<PRE>";print_r($tree);exit;
102         $orientation =  ($tree->width > $tree->height) ? 'L' : 'P';
103
104         $GLOBALS['_XML_SVGTOPDF']['options']['file'] = $svg;
105
106         if ($data['language'] == 'big5') {
107            
108             require_once 'FpdfAlt/Chinese.php';
109
110             $pdf=new FpdfAlt_Chinese($orientation ,'mm','A4');
111             $pdf->AddGBFont();
112             $pdf->AddBig5Font();
113             $pdf->AddUniCNShwFont(); 
114             $pdf->open();            
115         } else {
116             require_once 'Fpdf.php'; 
117
118             $pdf=new FPDF($orientation ,'mm','A4');
119             $pdf->open();
120         }
121
122         $pdf->setAutoPageBreak(false);
123         $pdf->AliasNbPages();
124         // convert data to array.
125         if (is_object($data)) {
126             $data = (array) $data;
127         }
128         // assoc. array of key => no of fields per page.
129         $perPage = $tree->calcPerPage();
130         
131         // no dynamic blocks:
132         if (!$perPage || empty($data)) {
133             $pdf->addPage();
134             $tree->writePDF($pdf,$data);
135             $t->debug($tree);
136             return $pdf;
137         }
138         
139         // build blocks of data for each page.
140         $haveData = true;
141         $page = 0;
142         while (true == $haveData ) {
143             $page_data = $data;
144             $haveData = false;
145             foreach($perPage as $k=>$v) {
146                 if (!$data[$k]) {
147                     $page_data[$k] = array();
148                     continue;
149                 }
150                 $haveData = true;
151                 $page_data[$k] = array_splice ( $data[$k], 0,$v);
152             }
153             $page++;
154             if (!$haveData) {
155                 break;
156             }
157                 
158             $t->debug("<B>PAGE $page<B>");
159             $pdf->addPage();
160            // echo '<PRE>'.htmlspecialchars(print_r($page_data,true));
161             $tree->writePDF($pdf,$page_data);
162         }
163         
164         $t->debug($tree);
165         return $pdf;
166     }
167     
168     function buildNull($node) {
169         return;
170     }
171     function buildObject($node) {
172         $class = 'XML_SvgToPDFAlt_'.$node->name;
173         
174         if (!empty($node->content) && strlen(trim($node->content)) && (@$this->language)) {
175             $node->language = $this->language;
176  
177         }
178  
179
180         //echo "look for $class?";
181         if (!class_exists($class) && !empty($node->name)) {
182             // try loading it..
183             $file = dirname(__FILE__) . '/SvgToPdfAlt/'.ucfirst(strtolower($node->name)). '.php';
184             $this->debug("loading  $file");
185             if (file_exists($file)) {
186                 require_once 'XML/SvgToPdfAlt/'.ucfirst(strtolower($node->name)) . '.php';
187             }
188         }
189         // now if it doesnt exist..
190         if (!class_exists($class)) {
191             $this->debug("can not find $class");
192            $class = 'XML_SvgToPDFAlt_Base';
193         }
194         //echo '<PRE>';print_r($node);
195         
196         $r = new $class;
197         $r->fromNode($node);
198         return $r;
199     }
200     
201     
202
203
204 }