xref: /plugin/siteexport/inc/mpdf.php (revision 0571ece201b9e3bc14846f6c88d943a4f1512014)
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            $this->useSubstitutions = true;
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            array_unshift($stack, "(({$this->y}+{$this->divheight}>{$this->PageBreakTrigger}) || ({$this->y}+h>{$this->PageBreakTrigger}) ||
114		({$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())");
115
116            $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);
117
118            parent::AddPage($orientation,$condition, $resetpagenum, $pagenumstyle,$suppress,$mgl,$mgr,$mgt,$mgb,$mgh,$mgf,$ohname,$ehname,$ofname,$efname,$ohvalue,$ehvalue,$ofvalue,$efvalue,$pagesel,$newformat);
119        }
120//*/
121
122        function GetFullPath(&$path,$basepath='') {
123
124            // Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
125
126            $path = str_replace("\\","/",$path); //If on Windows
127            $path = preg_replace('/^\/\//','http://',$path);	// mPDF 5.6.27
128            $regexp = '|^./|';	// Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
129            $path = preg_replace($regexp,'',$path);
130
131            if ( preg_match("/^.+\/\.\.\//", $path) ) {
132                // ../ not at the beginning
133                $newpath = array();
134                $oldpath = explode('/', $path);
135
136                foreach( $oldpath as $slice ) {
137                    if ( $slice == ".." && count($newpath) > 0 ) {
138                        array_pop($newpath);
139                        continue;
140                    }
141
142                    $newpath[] = $slice;
143                }
144
145                $path = implode('/', $newpath);
146            }
147
148            parent::GetFullPath($path, $basepath);
149
150            $regex = "/^(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
151            if ( preg_match($regex, $path, $matches) ) {
152                $path = preg_replace($regex, "\\1", $path);
153            }
154
155        }
156
157        /*
158          Only when the toc is being generated
159        */
160        function MovePages($target_page, $start_page, $end_page = -1) {
161            parent::MovePages($target_page, $start_page, $end_page);
162        }
163
164        function OpenTag($tag, $attr, &$ahtml, &$ihtml) {
165            switch ($tag) {
166                case 'BOOKMARK':
167                case 'TOCENTRY':
168                    if ($attr['CONTENT']) {
169                        // resolve double encoding
170                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
171                    }
172                    break;
173            }
174            return parent::OpenTag($tag, $attr, $ahtml, $ihtml);
175        }
176    }
177
178    if (file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php') && !class_exists('cssmgr', false)) {
179//*
180        require_once(DOKU_PLUGIN . 'siteexport/inc/patchCSSmgr.php');
181        $objPatch = new CSSMgrPatch(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php');
182        if ($objPatch->redefineFunction(file_get_contents(DOKU_PLUGIN . 'siteexport/inc/readCSS.patch'))) {
183            eval($objPatch->getCode());
184        }
185/*/
186//*/
187    }
188
189}
190