xref: /plugin/siteexport/inc/mpdf.php (revision 876523d1ff88fbe6aff7c288eec4e84e8d8f1e34)
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/mpdf/mpdf.php') ) {
15
16    global $conf;
17    if(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'].'/dwpdf/'.rand(1,1000).'/');
18    if(!defined('_MPDF_TTFONTDATAPATH')) define('_MPDF_TTFONTDATAPATH',$conf['cachedir'].'/mpdf_ttf/');
19
20    require_once(DOKU_PLUGIN . 'dw2pdf/mpdf/mpdf.php');
21
22    class siteexportPDF extends mpdf {
23
24        private $debugObj = false;
25
26		function __construct($debug) {
27            global $INPUT;
28            global $conf;
29
30            $dw2pdf = plugin_load('action', 'dw2pdf');
31
32		    // decide on the paper setup from param or config
33            $pagesize    = $INPUT->str('pagesize', $dw2pdf->getConf('pagesize'), true);
34            $orientation = $INPUT->str('orientation', $dw2pdf->getConf('orientation'), true);
35
36            io_mkdir_p(_MPDF_TTFONTDATAPATH);
37            io_mkdir_p(_MPDF_TEMP_PATH);
38
39            $format = $pagesize;
40            if($orientation == 'landscape') $format .= '-L';
41
42            switch($conf['lang']) {
43                case 'zh':
44                case 'zh-tw':
45                case 'ja':
46                case 'ko':
47                    $mode = '+aCJK';
48                    break;
49                default:
50                    $mode = 'UTF-8-s';
51
52            }
53
54            // we're always UTF-8
55            parent::__construct($mode, $format);
56            $this->ignore_invalid_utf8 = true;
57            $this->tabSpaces = 4;
58            $this->debugObj = $debug;
59            $this->debug = $debug !== false;
60            $this->shrink_tables_to_fit = 1; // Does not shrink tables by default, only in emergency
61            $this->use_kwt = true; // avoids page-breaking in H1-H6 if a table follows directly
62        }
63
64        function message($msg, $vars=null, $lvl=1)
65        {
66            if ( $this->debugObj !== false ) {
67                $this->debugObj->message($msg, $vars, $lvl);
68            }
69        }
70
71        function Error($msg)
72        {
73            if ( $this->debugObj !== false && method_exists($this->debugObj, 'runtimeException') ) {
74                $this->debugObj->runtimeException($msg);
75            } else {
76                parent::Error($msg);
77            }
78        }
79
80//*
81        // Nothing
82/*/
83        var $previousPage = '';
84        var $currentPage = '';
85        var $skipAddPage = false;
86
87        function Footer(){
88
89            $currentPage = $this->pages[count($this->pages)];
90            $this->skipAddPage = $this->previousPage && $this->pages[count($this->pages)] == $this->currentPage;
91
92            if ( $this->skipAddPage ) {
93                $this->message("HAS TO REMOVE PAGE:", count($this->pages));
94            }
95
96            $this->currentPage = $currentPage;
97            parent::Footer();
98        }
99        function AddPage($orientation='',$condition='', $resetpagenum='', $pagenumstyle='', $suppress='',$mgl='',$mgr='',$mgt='',$mgb='',$mgh='',$mgf='',$ohname='',$ehname='',$ofname='',$efname='',$ohvalue=0,$ehvalue=0,$ofvalue=0,$efvalue=0,$pagesel='',$newformat='')
100        {
101
102            if ( $skipAddPage ) { return; }
103
104            $count = count($this->pages);
105            $stack = array();
106            $trace = debug_backtrace();
107            foreach( $trace as $entry ) {
108                $vars = substr(implode(',', $entry['args']), 0, 20);
109                $stack[] = "{$entry['function']}({$vars}) | {$entry['file']} | {$entry['line']}";
110            }
111
112            array_unshift($stack, "(({$this->y}+{$this->divheight}>{$this->PageBreakTrigger}) || ({$this->y}+h>{$this->PageBreakTrigger}) ||
113		({$this->y}+(h*2)+{$this->blk[$this->blklvl]['padding_bottom']}+{$this->blk[$this->blklvl]['margin_bottom']}>{$this->PageBreakTrigger} && {$this->blk[$this->blklvl]['page_break_after_avoid']})) and !{$this->InFooter} and AcceptPageBreak())");
114
115            $this->message("Is Adding Page $count: $orientation,$condition, $resetpagenum, $pagenumstyle,$suppress,$mgl,$mgr,$mgt,$mgb,$mgh,$mgf,$ohname,$ehname,$ofname,$efname,$ohvalue,$ehvalue,$ofvalue,$efvalue,$pagesel,$newformat", $stack, 1);
116
117            parent::AddPage($orientation,$condition, $resetpagenum, $pagenumstyle,$suppress,$mgl,$mgr,$mgt,$mgb,$mgh,$mgf,$ohname,$ehname,$ofname,$efname,$ohvalue,$ehvalue,$ofvalue,$efvalue,$pagesel,$newformat);
118        }
119//*/
120
121        function GetFullPath(&$path,$basepath='') {
122
123        	// Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
124
125			$path = str_replace("\\","/",$path); //If on Windows
126			$path = preg_replace('/^\/\//','http://',$path);	// mPDF 5.6.27
127			$regexp = '|^./|';	// Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
128			$path = preg_replace($regexp,'',$path);
129
130        	if ( preg_match("/^.+\/\.\.\//", $path) ) {
131        		// ../ not at the beginning
132	        	$newpath = array();
133	        	$oldpath = explode('/', $path);
134
135	        	foreach( $oldpath as $slice ) {
136		        	if ( $slice == ".." && count($newpath) > 0 ) {
137			        	array_pop($newpath);
138			        	continue;
139		        	}
140
141		        	$newpath[] = $slice;
142	        	}
143
144	        	$path = implode('/', $newpath);
145        	}
146
147        	parent::GetFullPath($path, $basepath);
148
149        	$regex = "/^(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
150        	if ( preg_match($regex, $path, $matches) ) {
151        		$path = preg_replace($regex, "\\1", $path);
152        	}
153
154        }
155
156        /*
157          Only when the toc is being generated
158        */
159        function MovePages($target_page, $start_page, $end_page=-1) {
160            parent::MovePages($target_page, $start_page, $end_page);
161        }
162
163        function OpenTag($tag,$attr,&$ahtml,&$ihtml) {
164            switch($tag) {
165                case 'BOOKMARK':
166                case 'TOCENTRY':
167                    if ( $attr['CONTENT'] ) {
168                        // resolve double encoding
169                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
170                    }
171                    break;
172            }
173            return parent::OpenTag($tag,$attr,$ahtml,$ihtml);
174        }
175    }
176
177    if ( file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php') && !class_exists('cssmgr', false)) {
178//*
179        require_once(DOKU_PLUGIN . 'siteexport/inc/patchCSSmgr.php');
180        $objPatch = new CSSMgrPatch(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php');
181        if ( $objPatch->redefineFunction(file_get_contents(DOKU_PLUGIN . 'siteexport/inc/readCSS.patch')) ) {
182            eval($objPatch->getCode());
183        }
184/*/
185//*/
186    }
187
188}
189