1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* @author Nicolas CARPi <[email protected]> |
4
|
|
|
* @copyright 2012 Nicolas CARPi |
5
|
|
|
* @see https://www.elabftw.net Official website |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* @package elabftw |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Elabftw\Services; |
11
|
|
|
|
12
|
|
|
use function dirname; |
13
|
|
|
use Elabftw\Exceptions\FilesystemErrorException; |
14
|
|
|
use Elabftw\Interfaces\MpdfProviderInterface; |
15
|
|
|
use function is_dir; |
16
|
|
|
use function mkdir; |
17
|
|
|
use Mpdf\Mpdf; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get an instance of mpdf |
21
|
|
|
*/ |
22
|
|
|
class MpdfProvider implements MpdfProviderInterface |
23
|
|
|
{ |
24
|
|
|
public function __construct(private string $author, private string $format = 'A4', private bool $pdfa = true) |
25
|
|
|
{ |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getInstance(): Mpdf |
29
|
|
|
{ |
30
|
|
|
// we use a custom tmp dir, not the same as Twig because its content gets deleted after pdf is generated |
31
|
|
|
$tmpDir = dirname(__DIR__, 2) . '/cache/mpdf/'; |
32
|
|
|
if (!is_dir($tmpDir) && !mkdir($tmpDir, 0700, true) && !is_dir($tmpDir)) { |
33
|
|
|
throw new FilesystemErrorException("Could not create the $tmpDir directory! Please check permissions on this folder."); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// create the pdf |
37
|
|
|
$mpdf = new Mpdf(array( |
38
|
|
|
'format' => $this->format, |
39
|
|
|
'tempDir' => $tmpDir, |
40
|
|
|
'mode' => 'utf-8', |
41
|
|
|
)); |
42
|
|
|
|
43
|
|
|
// make sure we can read the pdf in a long time |
44
|
|
|
// will embed the font and make the pdf bigger |
45
|
|
|
$mpdf->PDFA = $this->pdfa; |
46
|
|
|
|
47
|
|
|
// make sure header and footer are not overlapping the body text |
48
|
|
|
$mpdf->setAutoTopMargin = 'stretch'; |
49
|
|
|
$mpdf->setAutoBottomMargin = 'stretch'; |
50
|
|
|
|
51
|
|
|
// set metadata |
52
|
|
|
$mpdf->SetAuthor($this->author); |
53
|
|
|
$mpdf->SetTitle('eLabFTW pdf'); |
54
|
|
|
$mpdf->SetSubject('eLabFTW pdf'); |
55
|
|
|
$mpdf->SetCreator('www.elabftw.net'); |
56
|
|
|
|
57
|
|
|
return $mpdf; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|