xref: /plugin/siteexport/inc/mpdf.php (revision fff1dd3d17e36f3dd5b1c1cdc5fbe9a72061a30f)
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->SetAutoFont(AUTOFONT_ALL);
57            $this->ignore_invalid_utf8 = true;
58            $this->tabSpaces = 4;
59            $this->debugObj = $debug;
60            $this->debug = $debug !== false;
61            $this->shrink_tables_to_fit = 1; // Does not shrink tables by default, only in emergency
62            $this->use_kwt = true; // avoids page-breaking in H1-H6 if a table follows directly
63        }
64
65        function message($msg, $vars=null, $lvl=1)
66        {
67            if ( $this->debugObj !== false ) {
68                $this->debugObj->message($msg, $vars, $lvl);
69            }
70        }
71
72        function Error($msg)
73        {
74            if ( $this->debugObj !== false && method_exists($this->debugObj, 'runtimeException') ) {
75                $this->debugObj->runtimeException($msg);
76            } else {
77                parent::Error($msg);
78            }
79        }
80
81//*
82        // Nothing
83/*/
84        var $previousPage = '';
85        var $currentPage = '';
86        var $skipAddPage = false;
87
88        function Footer(){
89
90            $currentPage = $this->pages[count($this->pages)];
91            $this->skipAddPage = $this->previousPage && $this->pages[count($this->pages)] == $this->currentPage;
92
93            if ( $this->skipAddPage ) {
94                $this->message("HAS TO REMOVE PAGE:", count($this->pages));
95            }
96
97            $this->currentPage = $currentPage;
98            parent::Footer();
99        }
100        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='')
101        {
102
103            if ( $skipAddPage ) { return; }
104
105            $count = count($this->pages);
106            $stack = array();
107            $trace = debug_backtrace();
108            foreach( $trace as $entry ) {
109                $vars = substr(implode(',', $entry['args']), 0, 20);
110                $stack[] = "{$entry['function']}({$vars}) | {$entry['file']} | {$entry['line']}";
111            }
112
113            $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);
114
115            parent::AddPage($orientation,$condition, $resetpagenum, $pagenumstyle,$suppress,$mgl,$mgr,$mgt,$mgb,$mgh,$mgf,$ohname,$ehname,$ofname,$efname,$ohvalue,$ehvalue,$ofvalue,$efvalue,$pagesel,$newformat);
116        }
117//*/
118
119        function GetFullPath(&$path,$basepath='') {
120
121        	// Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
122
123			$path = str_replace("\\","/",$path); //If on Windows
124			$path = preg_replace('/^\/\//','http://',$path);	// mPDF 5.6.27
125			$regexp = '|^./|';	// Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
126			$path = preg_replace($regexp,'',$path);
127
128        	if ( preg_match("/^.+\/\.\.\//", $path) ) {
129        		// ../ not at the beginning
130	        	$newpath = array();
131	        	$oldpath = explode('/', $path);
132
133	        	foreach( $oldpath as $slice ) {
134		        	if ( $slice == ".." && count($newpath) > 0 ) {
135			        	array_pop($newpath);
136			        	continue;
137		        	}
138
139		        	$newpath[] = $slice;
140	        	}
141
142	        	$path = implode('/', $newpath);
143        	}
144
145        	parent::GetFullPath($path, $basepath);
146
147        	$regex = "/^(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
148        	if ( preg_match($regex, $path, $matches) ) {
149        		$path = preg_replace($regex, "\\1", $path);
150        	}
151
152        }
153
154        /*
155          Only when the toc is being generated
156        */
157        function MovePages($target_page, $start_page, $end_page=-1) {
158            parent::MovePages($target_page, $start_page, $end_page);
159        }
160
161        function OpenTag($tag, $attr) {
162            switch($tag) {
163                case 'BOOKMARK':
164                case 'TOCENTRY':
165                    if ( $attr['CONTENT'] ) {
166                        // resolve double encoding
167                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
168                    }
169                    break;
170            }
171            return parent::OpenTag($tag, $attr);
172        }
173    }
174
175    if ( file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php') && !class_exists('cssmgr', false)) {
176//*
177        require_once(DOKU_PLUGIN . 'siteexport/inc/patchCSSmgr.php');
178        $objPatch = new CSSMgrPatch(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php');
179        if ( $objPatch->redefineFunction(file_get_contents(DOKU_PLUGIN . 'siteexport/inc/readCSS.patch')) ) {
180            eval($objPatch->getCode());
181        }
182/*/
183//*/
184    }
185
186}
187