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/Tree/Morph.php';
45 require_once 'Fpdf/tFPDF.php';
46 require_once 'XML/SvgToPdf/Base.php';
48 // current options for generated file..
50 $GLOBALS['_XML_SVGTOPDF']['options'] = array(
59 static function debug($s,$e=0) {
60 if (!$GLOBALS['_XML_SVGTOPDF']['options']['debug']) {
63 echo "<PRE>".print_R($s,true). "</PRE>";
72 * Static ! not sure why!?
75 static function construct($svg, $data=array())
77 if (is_object($data)) {
78 $data = (array) $data;
81 $t = new XML_SvgToPDF;
83 $t->language = $data['language'];
85 $x = new XML_Tree_Morph(
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'),
103 $tree = $x->getTreeFromFile();
105 $tree = $t->buildobject($tree);
108 $tree = $t->parseSvg($svg);
109 //echo "<PRE>";print_r($tree);exit;
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;
122 if ($data['language'] == 'big5') {
123 //die("trying chinese");
124 require_once 'Fpdf/tFPDF.php' ;
126 $pdf = new tFPDF($orientation ,'mm','A4');
128 // we originally used ARIALUNI.ttf'
130 $font = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf';
132 if (!file_exists('/usr/share/fonts/truetype/msttcorefonts/Arial.ttf')) {
133 die("install msttcorefonts package");
136 //$pdf->AddFont('ARIALUNI','',$font,true);
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);
141 // $pdf->SetFont('ARIALUNI','',14);
142 // require_once 'Fpdf/Chinese-unicode.php';
144 // $pdf=new PDF_Unicode($orientation ,'mm','A4');
145 //// $pdf->AddGBFont();
146 //// $pdf->AddBig5Font();
147 // $pdf->AddUniCNSFont('Uni');
148 //$pdf->AddUniCNSFont('Uni');
152 $pdf=new tFPDF($orientation ,'mm','A4');
156 $pdf->setAutoPageBreak(false);
157 $pdf->AliasNbPages();
158 // convert data to array.
159 if (is_object($data)) {
160 $data = (array) $data;
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])) {
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)) {
173 $tree->writePDF($pdf,$data);
178 // build blocks of data for each page.
182 // $originalData = $data;
183 //$alldata = $data[$var];
185 // while (count($alldata)) {
186 //print_r($perPage);exit;
191 while (true == $haveData ) {
196 // replaces the properties that have 'page data'
199 foreach($perPage as $k=>$v) {
200 if (empty($data[$k])) {
201 $page_data[$k] = array();
205 $page_data[$k] = self::fetchRows($data,$k,$v);
208 //$page_data[$k] = array_splice ( $data[$k], 0,$v);
213 if ($page && !$haveData) {
218 $t->debug("<B>PAGE $page<B>");
222 $tree->writePDF($pdf,$page_data);
224 //$tree->writePDF($pdf,$data);
231 function fetchRows(&$original_data, $key, $rows) {
233 while ($rows > -1 && !empty($original_data[$key])) {
234 $addrow = array_shift($original_data[$key]);
235 $rows -= !empty($addrow->userows) ? $addrow->userows : 1;
237 array_unshift($original_data[$key],$addrow);
250 function parseSvg($svgFileName)
252 libxml_use_internal_errors(true);
253 $d = new DOMDocument();
254 $d->load($svgFileName);
256 return $this->parseNode($d->documentElement);
259 function parseNode($n)
261 // do children first..
262 //print_r(array("PARSENODE:",$n));
267 if ($n->childNodes->length) {
268 foreach($n->childNodes as $cn) {
269 if ($cn->nodeType != XML_ELEMENT_NODE) {
272 $child = $this->parseNode($cn);
273 if (is_array($child) && count($child)) {
274 $children = array_merge($children, $child);
277 if (is_object($child)) {
278 $children[] = $child;
284 if (!in_array($n->tagName, array('svg','image','text', 'tspan', 'rect', 'g', 'path'))) {
288 $ret = $this->buildObject($n,$children);
298 function buildNull($node) {
301 function buildObject($node, $children) {
302 $class = 'XML_SvgToPDF_'.$node->tagName;
304 if (strlen(trim($node->content)) && (@$this->language)) {
305 $node->language = $this->language;
310 //echo "look for $class?";
311 if (!class_exists($class)) {
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';
319 // now if it doesnt exist..
320 if (!class_exists($class)) {
321 $this->debug("can not find $class");
322 $class = 'XML_SvgToPDF_Base';
325 $r->children = $children;
326 $r->fromXmlNode($node);