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