xref: /plugin/siteexport/inc/mpdf.php (revision d98cce67fe0e842937bedec2772b204f7feee514)
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        function GetFullPath(&$path,$basepath='') {
82
83            // Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
84
85            $path = str_replace("\\","/",$path); //If on Windows
86            $path = preg_replace('/^\/\//','http://',$path);    // mPDF 5.6.27
87            $regexp = '|^./|';    // Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
88            $path = preg_replace($regexp,'',$path);
89
90            if ( preg_match("/^.+\/\.\.\//", $path) ) {
91                // ../ not at the beginning
92                $newpath = array();
93                $oldpath = explode('/', $path);
94
95                foreach( $oldpath as $slice ) {
96                    if ( $slice == ".." && count($newpath) > 0 ) {
97                        array_pop($newpath);
98                        continue;
99                    }
100
101                    $newpath[] = $slice;
102                }
103
104                $path = implode('/', $newpath);
105            }
106
107            parent::GetFullPath($path, $basepath);
108
109            $regex = "/^(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
110            if ( preg_match($regex, $path, $matches) ) {
111                $path = preg_replace($regex, "\\1", $path);
112            }
113
114        }
115
116        /*
117          Only when the toc is being generated
118        */
119        function MovePages($target_page, $start_page, $end_page = -1) {
120            parent::MovePages($target_page, $start_page, $end_page);
121        }
122
123        function OpenTag($tag, $attr, &$ahtml, &$ihtml) {
124            switch ($tag) {
125                case 'BOOKMARK':
126                case 'TOCENTRY':
127                    if ($attr['CONTENT']) {
128                        // resolve double encoding
129                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
130                    }
131                    break;
132            }
133            return parent::OpenTag($tag, $attr, $ahtml, $ihtml);
134        }
135    }
136
137    if (file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php') && !class_exists('cssmgr', false)) {
138//*
139        require_once(DOKU_PLUGIN . 'siteexport/inc/patchCSSmgr.php');
140        $objPatch = new CSSMgrPatch(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php');
141        if ($objPatch->redefineFunction(file_get_contents(DOKU_PLUGIN . 'siteexport/inc/readCSS.patch'))) {
142            eval($objPatch->getCode());
143        }
144/*/
145//*/
146    }
147
148}
149