xref: /plugin/dw2pdf/DokuPDF.class.php (revision 22dc18034bb9b3edc073e0862a7f9ec9a5ca5931)
1<?php
2/**
3 * Wrapper around the mpdf library class
4 *
5 * This class overrides some functions to make mpdf make use of DokuWiki'
6 * standard tools instead of its own.
7 *
8 * @author Andreas Gohr <andi@splitbrain.org>
9 */
10
11use dokuwiki\plugin\dw2pdf\DokuImageProcessorDecorator;
12
13require_once __DIR__ . '/vendor/autoload.php';
14
15/**
16 * Class DokuPDF
17 * Some DokuWiki specific extentions
18 */
19class DokuPDF extends \Mpdf\Mpdf
20{
21
22    /**
23     * DokuPDF constructor.
24     *
25     * @param string $pagesize
26     * @param string $orientation
27     * @param int $fontsize
28     */
29    function __construct($pagesize = 'A4', $orientation = 'portrait', $fontsize = 11)
30    {
31        global $conf;
32        global $lang;
33        global $ID;
34
35        if (!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'] . '/dwpdf/' . rand(1, 1000) . '/');
36        io_mkdir_p(_MPDF_TEMP_PATH);
37
38        $format = $pagesize;
39        if ($orientation == 'landscape') {
40            $format .= '-L';
41        }
42
43        $docLang = $this->getDocumentLanguage($ID);
44        switch ($docLang) {
45            case 'zh':
46            case 'zh-tw':
47            case 'ja':
48            case 'ko':
49                $mode = '+aCJK';
50                break;
51            default:
52                $mode = 'UTF-8-s';
53
54        }
55
56        parent::__construct([
57            'mode' => $mode,
58            'format' => $format,
59            'default_font_size' => $fontsize,
60            'ImageProcessorClass' => DokuImageProcessorDecorator::class,
61            'tempDir' => _MPDF_TEMP_PATH, //$conf['tmpdir'] . '/tmp/dwpdf'
62            'SHYlang' => $docLang,
63        ]);
64
65        $this->autoScriptToLang = true;
66        $this->baseScript = 1;
67        $this->autoVietnamese = true;
68        $this->autoArabic = true;
69        $this->autoLangToFont = true;
70
71        $this->ignore_invalid_utf8 = true;
72        $this->tabSpaces = 4;
73
74        // assumed that global language can be used, maybe Bookcreator needs more nuances?
75        $this->SetDirectionality($lang['direction']);
76    }
77
78    /**
79     * Cleanup temp dir
80     */
81    function __destruct()
82    {
83        io_rmdir(_MPDF_TEMP_PATH, true);
84    }
85
86    /**
87     * Decode all paths, since DokuWiki uses XHTML compliant URLs
88     *
89     * @param string $path
90     * @param string $basepath
91     */
92    function GetFullPath(&$path, $basepath = '')
93    {
94        $path = htmlspecialchars_decode($path);
95        parent::GetFullPath($path, $basepath);
96    }
97
98    /**
99     * Get the language of the current document
100     *
101     * Uses the translation plugin if available
102     * @return string
103     */
104    protected function getDocumentLanguage($pageid)
105    {
106        global $conf;
107
108        $lang = $conf['lang'];
109        /** @var helper_plugin_translation $trans */
110        $trans = plugin_load('helper', 'translation');
111        if ($trans) {
112            $tr = $trans->getLangPart($pageid);
113            if ($tr) $lang = $tr;
114        }
115
116        return $lang;
117    }
118
119}
120