1<?php 2 3// phpcs:disable: PSR1.Methods.CamelCapsMethodName.NotCamelCaps 4 5namespace dokuwiki\plugin\dw2pdf\src; 6 7use Mpdf\Container\SimpleContainer; 8use Mpdf\Mpdf; 9use Mpdf\MpdfException; 10use Mpdf\File\LocalContentLoader as MpdfContenLoader; 11use Psr\Log\NullLogger; 12 13/** 14 * Wrapper around the mpdf library class 15 * 16 * This class overrides some functions to make mpdf make use of DokuWiki' 17 * standard tools instead of its own. 18 * 19 * @author Andreas Gohr <andi@splitbrain.org> 20 */ 21class DokuMpdf extends Mpdf 22{ 23 /** 24 * DokuPDF constructor. 25 * 26 * @param Config $config 27 * @param string $lang The language code to use for this document 28 * @throws MpdfException 29 */ 30 public function __construct(Config $config, string $lang) 31 { 32 $initConfig = $config->getMPdfConfig(); 33 $initConfig['mode'] = $this->lang2mode($lang); 34 35 $http = new HttpClient(); 36 37 $container = new SimpleContainer([ 38 'httpClient' => $http, 39 'assetFetcher' => new DokuAssetFetcher($this, new MpdfContenLoader(), $http, new NullLogger()) 40 ]); 41 42 parent::__construct($initConfig, $container); 43 $this->SetDirectionality($this->lang2direction($lang)); 44 45 // configure page numbering 46 // https://mpdf.github.io/paging/page-numbering.html 47 $this->PageNumSubstitutions[] = ['from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off']; 48 // add watermark text if configured 49 $this->SetWatermarkText($config->getWatermarkText()); 50 51 // let mpdf fix local links 52 $self = parse_url(DOKU_URL); 53 $url = $self['scheme'] . '://' . $self['host']; 54 if (!empty($self['port'])) { 55 $url .= ':' . $self['port']; 56 } 57 $this->SetBasePath($url); 58 } 59 60 /** 61 * Decode all paths, since DokuWiki uses XHTML compliant URLs 62 * 63 * @inheritdoc 64 */ 65 public function GetFullPath(&$path, $basepath = '') 66 { 67 $path = htmlspecialchars_decode($path); 68 parent::GetFullPath($path, $basepath); 69 } 70 71 /** 72 * Get the mode to use based on the given language 73 * 74 * @link https://mpdf.github.io/reference/mpdf-functions/construct.html 75 * @link https://mpdf.github.io/reference/mpdf-variables/useadobecjk.html 76 * @todo it might be more sensible to pass a language string instead 77 * @param string $lang 78 * @return string 79 */ 80 protected function lang2mode(string $lang): string 81 { 82 return match ($lang) { 83 'zh', 'zh-tw', 'ja', 'ko' => '+aCJK', 84 default => 'UTF-8-s', 85 }; 86 } 87 88 /** 89 * Return the writing direction based on the set language 90 * 91 * @param string $lang 92 * @return string 93 */ 94 protected function lang2direction(string $lang): string 95 { 96 return match ($lang) { 97 'ar', 'he' => 'rtl', 98 default => 'ltr', 99 }; 100 } 101} 102