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