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;
45 // current options for generated file..
47 $GLOBALS['_XML_SVGTOPDF']['options'] = array(
56 static function debug($s,$e=0) {
57 if (!$GLOBALS['_XML_SVGTOPDF']['options']['debug']) {
60 echo "<PRE>".print_R($s,true). "</PRE>";
69 * Static ! not sure why!?
72 static function construct($svg, $data=array())
74 if (is_object($data)) {
75 $data = (array) $data;
78 $t = new XML_SvgToPDF;
80 $t->language = $data['language'];
81 require_once 'XML/Tree/Morph.php';
83 $x = new XML_Tree_Morph(
88 'svg' => array($t, 'buildObject'),
89 'image' => array($t, 'buildObject'),
90 'text' => array($t, 'buildObject'),
91 'tspan' => array($t, 'buildObject'),
92 'rect' => array($t, 'buildObject'),
93 'g' => array($t, 'buildObject'),
94 'path' => array($t, 'buildObject'),
95 'sodipodi:namedview' => array($t, 'buildNull'),
96 'defs' => array($t, 'buildNull'),
101 $tree = $x->getTreeFromFile();
102 //echo '<PRE>'; print_R($tree); exit;
103 $tree = $t->buildobject($tree);
104 // echo '<PRE>'; print_R($tree); exit;
106 // $tree = $t->parseSvg($svg);
107 // echo "<PRE>";print_r($tree);exit;
113 //echo "<PRE>";print_r($tree);exit;
114 $w = empty($tree->width) ? 0 : $tree->width;
115 $h = empty($tree->height) ? 0 : $tree->height;
116 $orientation = (preg_replace('/[^0-9.]+/','', $w)*1) > (preg_replace('/[^0-9.]+/','', $h)*1) ? 'L' : 'P';
117 //var_dump($orientation);exit;
118 $GLOBALS['_XML_SVGTOPDF']['options']['file'] = $svg;
120 if ($data['language'] == 'big5') {
121 //die("trying chinese");
124 require_once 'Fpdf/Unicode.php';
126 $pdf=new FPDF_Unicode($orientation ,'mm','A4');
129 $pdf->AddUniCNShwFont();
134 require_once 'Fpdf/tFPDF.php' ;
136 $pdf=new tFPDF($orientation ,'mm','A4');
140 $pdf->setAutoPageBreak(false);
141 $pdf->AliasNbPages();
142 // convert data to array.
143 if (is_object($data)) {
144 $data = (array) $data;
146 // assoc. array of key => no of fields per page.
147 $perPage = $tree->calcPerPage();
148 //list($var,$perpage) = $tree->calcPerPage();
149 //if (empty($data) || !@$var || !@count($data[$var])) {
151 // print_r($data['transactions'][0]);
152 // $data['transactions'][0]->desc = 'abcdefghijklmnopqrstuvwxyz Z';
153 // print_r($data['transactions'][0]->desc);
154 // no dynamic blocks:
155 if (!$perPage || empty($data)) {
157 $tree->writePDF($pdf,$data);
162 // build blocks of data for each page.
166 // $originalData = $data;
167 //$alldata = $data[$var];
169 // while (count($alldata)) {
170 //print_r($perPage);exit;
175 while (true == $haveData ) {
180 // replaces the properties that have 'page data'
183 foreach($perPage as $k=>$v) {
184 if (empty($data[$k])) {
185 $page_data[$k] = array();
189 $page_data[$k] = self::fetchRows($data,$k,$v);
192 //$page_data[$k] = array_splice ( $data[$k], 0,$v);
197 if ($page && !$haveData) {
202 $t->debug("<B>PAGE $page<B>");
206 $tree->writePDF($pdf,$page_data);
209 //$tree->writePDF($pdf,$data);
216 static function fetchRows(&$original_data, $key, $rows) {
218 while ($rows > -1 && !empty($original_data[$key])) {
219 $addrow = array_shift($original_data[$key]);
220 $rows -= !empty($addrow->userows) ? $addrow->userows : 1;
222 array_unshift($original_data[$key],$addrow);
235 function parseSvg($svgFileName)
237 libxml_use_internal_errors(true);
238 $d = new DOMDocument();
239 $d->load($svgFileName);
241 return $this->parseNode($d->documentElement);
244 function parseXMLNode($n)
246 // do children first..
247 //print_r(array("PARSENODE:",$n));
252 if ($n->childNodes->length) {
253 foreach($n->childNodes as $cn) {
254 if ($cn->nodeType != XML_ELEMENT_NODE) {
257 $child = $this->parseNode($cn);
258 if (is_array($child) && count($child)) {
259 $children = array_merge($children, $child);
262 if (is_object($child)) {
263 $children[] = $child;
269 if (!in_array($n->tagName, array('svg','image','text', 'tspan', 'rect', 'g', 'path'))) {
273 // fixme.. this is the Dom Version..
274 $ret = $this->buildObject($n,$children);
284 function buildNull($node) {
287 function buildObject($node )
289 require_once 'XML/SvgToPdf/Base.php';
291 return XML_SvgToPDF_Base::factory($node);