1<?php
2
3// phpcs:disable: PSR1.Methods.CamelCapsMethodName.NotCamelCaps
4
5use dokuwiki\plugin\dw2pdf\DokuImageProcessorDecorator;
6use Mpdf\Mpdf;
7use Mpdf\MpdfException;
8
9/**
10 * Wrapper around the mpdf library class
11 *
12 * This class overrides some functions to make mpdf make use of DokuWiki'
13 * standard tools instead of its own.
14 *
15 * @author Andreas Gohr <andi@splitbrain.org>
16 */
17class DokuPDF extends Mpdf
18{
19    /**
20     * DokuPDF constructor.
21     *
22     * @param string $pagesize
23     * @param string $orientation
24     * @param int $fontsize
25     *
26     * @throws MpdfException
27     * @throws Exception
28     */
29    public function __construct($pagesize = 'A4', $orientation = 'portrait', $fontsize = 11, $docLang = 'en')
30    {
31        global $conf;
32        global $lang;
33
34        if (!defined('_MPDF_TEMP_PATH')) {
35            define('_MPDF_TEMP_PATH', $conf['tmpdir'] . '/dwpdf/' . random_int(1, 1000) . '/');
36        }
37        io_mkdir_p(_MPDF_TEMP_PATH);
38
39        $format = $pagesize;
40        if ($orientation == 'landscape') {
41            $format .= '-L';
42        }
43
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        parent::__construct([
56            'mode' => $mode,
57            'format' => $format,
58            'default_font_size' => $fontsize,
59            'ImageProcessorClass' => DokuImageProcessorDecorator::class,
60            'tempDir' => _MPDF_TEMP_PATH, //$conf['tmpdir'] . '/tmp/dwpdf'
61            'SHYlang' => $docLang,
62        ]);
63
64        $this->autoScriptToLang = true;
65        $this->baseScript = 1;
66        $this->autoVietnamese = true;
67        $this->autoArabic = true;
68        $this->autoLangToFont = true;
69
70        $this->ignore_invalid_utf8 = true;
71        $this->tabSpaces = 4;
72
73        // assumed that global language can be used, maybe Bookcreator needs more nuances?
74        $this->SetDirectionality($lang['direction']);
75    }
76
77    /**
78     * Cleanup temp dir
79     */
80    public function __destruct()
81    {
82        io_rmdir(_MPDF_TEMP_PATH, true);
83    }
84
85    /**
86     * Decode all paths, since DokuWiki uses XHTML compliant URLs
87     *
88     * @param string $path
89     * @param string $basepath
90     */
91    public function GetFullPath(&$path, $basepath = '')
92    {
93        $path = htmlspecialchars_decode($path);
94        parent::GetFullPath($path, $basepath);
95    }
96}
97