xref: /plugin/siteexport/inc/mpdf.php (revision a4a5b91d8f969b6bfc5f121ab4ebde86367665d7)
1<?php
2/**
3 * Site Export Plugin - mPDF Extension
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14if ( file_exists(DOKU_PLUGIN . 'dw2pdf/DokuPDF.class.php') ) {
15
16	global $conf;
17	// if(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'].'/dwpdf/'.rand(1,1000).'/');
18
19    require_once(DOKU_PLUGIN . 'dw2pdf/DokuPDF.class.php');
20
21    class siteexportPDF extends DokuPDF {
22
23        var $debugObj = true;
24
25		function __construct($encoding, $debug=false) {
26
27            parent::__construct($encoding);
28            $this->debugObj = $debug;
29            $this->debug = true;
30            $this->shrink_tables_to_fit = 1; // Does not shrink tables by default, only in emergency
31            $this->use_kwt = true; // avoids page-breaking in H1-H6 if a table follows directly
32        }
33
34        function message($msg, $vars=null, $lvl=1)
35        {
36            if ( $this->debugObj !== false ) {
37                $this->debugObj->message($msg, $vars, $lvl);
38            }
39        }
40
41        function Error($msg)
42        {
43            if ( $this->debug !== false && $lvl == null && method_exists($this->debug, 'runtimeException') ) {
44                $this->debug->runtimeException($msg);
45            } else {
46                parent::Error($msg);
47            }
48        }
49
50        function GetFullPath(&$path,$basepath='') {
51
52        	// Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
53
54			$path = str_replace("\\","/",$path); //If on Windows
55			$path = preg_replace('/^\/\//','http://',$path);	// mPDF 5.6.27
56			$regexp = '|^./|';	// Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
57			$path = preg_replace($regexp,'',$path);
58
59        	if ( preg_match("/^.+\/\.\.\//", $path) ) {
60        		// ../ not at the beginning
61	        	$newpath = array();
62	        	$oldpath = explode('/', $path);
63
64	        	foreach( $oldpath as $slice ) {
65		        	if ( $slice == ".." && count($newpath) > 0 ) {
66			        	array_pop($newpath);
67			        	continue;
68		        	}
69
70		        	$newpath[] = $slice;
71	        	}
72
73	        	$path = implode('/', $newpath);
74        	}
75
76        	parent::GetFullPath($path, $basepath);
77
78        	$regex = "/(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
79        	if ( preg_match($regex, $path, $matches) ) {
80        		$path = preg_replace($regex, "\\1", $path);
81        	}
82        }
83
84        function OpenTag($tag, $attr) {
85            switch($tag) {
86                case 'BOOKMARK':
87                case 'TOCENTRY':
88                    if ( $attr['CONTENT'] ) {
89                        // resolve double encoding
90                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
91                    }
92                    break;
93            }
94            return parent::OpenTag($tag, $attr);
95        }
96
97/*        function GetFullPath(&$path,$basepath='') {
98        	$this->debugObj->message("GetFullPath: path before function", $path, 2);
99        	parent::GetFullPath($path,$basepath);
100        	$this->debugObj->message("GetFullPath: path AFTER function", $path, 2);
101        }
102*/
103/*
104        function _putannots($n) {
105            $nb=$this->page;
106            for($n=1;$n<=$nb;$n++)
107            {
108                $annotobjs = array();
109                if(isset($this->PageLinks[$n]) || isset($this->PageAnnots[$n])) {
110                    $wPt=$this->pageDim[$n]['w']*$this->k;
111                    $hPt=$this->pageDim[$n]['h']*$this->k;
112
113                    //Links
114                    if(isset($this->PageLinks[$n])) {
115                        foreach($this->PageLinks[$n] as $key => $pl) {
116                            $this->_newobj();
117                            $annot='';
118                            $rect=sprintf('%.3f %.3f %.3f %.3f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
119                            $annot .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.']';
120                            $annot .= ' /Contents '.$this->_UTF16BEtextstring($pl[4]);
121                            $annot .= ' /NM ('.sprintf('%04u-%04u', $n, $key).')';
122                            $annot .= ' /M '.$this->_textstring('D:'.date('YmdHis'));
123                            $annot .= ' /Border [0 0 0]';
124                            // mPDF 4.2.018
125                            if ($this->PDFA) { $annot .= ' /F 28'; }
126                            if (strpos($pl[4],'@')===0) {
127                                $p=substr($pl[4],1);
128                                //	$h=isset($this->OrientationChanges[$p]) ? $wPt : $hPt;
129                                $htarg=$this->pageDim[$p]['h']*$this->k;
130                                $annot.=sprintf(' /Dest [%d 0 R /XYZ 0 %.3f null]>>',1+2*$p,$htarg);
131                            }
132                            else if(is_string($pl[4])) {
133
134                                if ( preg_match( "#^(https?:/|file:)/#", $pl[4] )) {
135                                    $annot .= ' /A <</Type/Action/S/URI/URI'.$this->_textstring($pl[4]).'>> >>';
136                                } else {
137                                    $annot .= ' /A <</Type/Action/S/GoToR/F'.$this->_textstring($pl[4]).'>> >>';
138                                }
139                            }
140                            else {
141                                $l=$this->links[$pl[4]];
142                                // mPDF 3.0
143                                // may not be set if #link points to non-existent target
144                                if (isset($this->pageDim[$l[0]]['h'])) { $htarg=$this->pageDim[$l[0]]['h']*$this->k; }
145                                else { $htarg=$this->h*$this->k; } // doesn't really matter
146                                $annot.=sprintf(' /Dest [%d 0 R /XYZ 0 %.3f null]>>',1+2*$l[0],$htarg-$l[1]*$this->k);
147                            }
148                            $this->_out($annot);
149                            $this->_out('endobj');
150                        }
151                    }
152*/
153
154                    /*-- ANNOTATIONS --*/
155/*                    if(isset($this->PageAnnots[$n])) {
156                        foreach ($this->PageAnnots[$n] as $key => $pl) {
157                            $this->_newobj();
158                            $annot='';
159                            $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
160                            $x = $pl['x'];
161                            if ($this->annotMargin <> 0 || $x==0 || $x<0) {	// Odd page
162                                $x = ($wPt/$this->k) - $this->annotMargin;
163                            }
164                            $w = $h = ($this->annotSize * $this->k);
165                            $a = $x * $this->k;
166                            // mPDF 3.0
167                            $b = $hPt - ($pl['y']  * $this->k);
168                            $rect = sprintf('%.3f %.3f %.3f %.3f', $a, $b-$h, $a+$w, $b);
169                            $annot .= '<</Type /Annot /Subtype /Text /Rect ['.$rect.']';
170                            $annot .= ' /Contents '.$this->_UTF16BEtextstring($pl['txt']);
171                            $annot .= ' /NM ('.sprintf('%04u-%04u', $n, (2000 + $key)).')';
172                            $annot .= ' /M '.$this->_textstring('D:'.date('YmdHis'));
173                            $annot .= ' /CreationDate '.$this->_textstring('D:'.date('YmdHis'));
174                            $annot .= ' /Border [0 0 0]';
175                            // mPDF 4.2.018
176                            if ($this->PDFA) {
177                                $annot .= ' /F 28';
178                                $annot .= ' /CA 1';
179                            }
180                            else if ($pl['opt']['ca']>0) { $annot .= ' /CA '.$pl['opt']['ca']; }
181
182                            $annot .= ' /C [';
183                            if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c']))) {
184                                foreach ($pl['opt']['c'] as $col) {
185                                    $col = intval($col);
186                                    $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
187                                    $annot .= sprintf(" %.4f", $color);
188                                }
189                            }
190                            else { $annot .= '1 1 0'; }	// mPDF 4.2.026
191                            $annot .= ']';
192                            // Usually Author
193                            if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
194                                $annot .= ' /T '.$this->_UTF16BEtextstring($pl['opt']['t']);
195                            }
196                            if (isset($pl['opt']['subj'])) {
197                                $annot .= ' /Subj '.$this->_UTF16BEtextstring($pl['opt']['subj']);
198                            }
199                            $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
200                            if (isset($pl['opt']['icon']) AND in_array($pl['opt']['icon'], $iconsapp)) {
201                                $annot .= ' /Name /'.$pl['opt']['icon'];
202                            }
203                            else { $annot .= ' /Name /Note'; }
204                            // mPDF 4.2.027
205                            if (!empty($pl['opt']['popup'])) {
206                                $annot .= ' /Open true';
207                                $annot .= ' /Popup '.($this->n+1).' 0 R';				// mPDF 4.2.027
208                            }
209                            else { $annot .= ' /Open false'; }	// mPDF 4.2.027
210                            $annot .= ' /P 3 0 R';							// mPDF 4.2.027
211                            $annot .= '>>';
212                            $this->_out($annot);
213                            $this->_out('endobj');
214
215                            // mPDF 4.2.027
216                            if (!empty($pl['opt']['popup'])) {
217                                $this->_newobj();
218                                $annot='';
219                                if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][0])) { $x = $pl['opt']['popup'][0] * $this->k; }
220                                else { $x = $pl['x'] * $this->k; }
221                                if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][1])) { $y = $hPt - ($pl['opt']['popup'][1] * $this->k); }
222                                else { $y = $hPt - ($pl['y']  * $this->k); }
223                                if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][2])) { $w = $pl['opt']['popup'][2] * $this->k; }
224                                else { $w = 180; }
225                                if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][3])) { $h = $pl['opt']['popup'][3] * $this->k; }
226                                else { $h = 120; }
227                                $rect = sprintf('%.3f %.3f %.3f %.3f', $x, $y-$h, $x+$w, $y);
228                                $annot .= '<</Type /Annot /Subtype /Popup /Rect ['.$rect.']';
229                                $annot .= ' /M '.$this->_textstring('D:'.date('YmdHis'));
230                                if ($this->PDFA) { $annot .= ' /F 28'; }
231                                $annot .= ' /P 3 0 R';
232                                $annot .= ' /Parent '.($this->n-1).' 0 R';
233                                $annot .= '>>';
234                                $this->_out($annot);
235                                $this->_out('endobj');
236                            }
237                        }
238                    }
239*/                    /*-- END ANNOTATIONS --*/
240/*                }
241            }
242        }
243*/
244    }
245}