Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
4
 
5
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
6
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
7
 
8
class Mpdf extends Pdf
9
{
10
    public const SIMULATED_BODY_START = '<!-- simulated body start -->';
11
    private const BODY_TAG = '<body>';
12
 
13
    /**
14
     * Gets the implementation of external PDF library that should be used.
15
     *
16
     * @param array $config Configuration array
17
     *
18
     * @return \Mpdf\Mpdf implementation
19
     */
20
    protected function createExternalWriterInstance(array $config): \Mpdf\Mpdf
21
    {
22
        return new \Mpdf\Mpdf($config);
23
    }
24
 
25
    /**
26
     * Save Spreadsheet to file.
27
     *
28
     * @param string $filename Name of the file to save as
29
     */
30
    public function save($filename, int $flags = 0): void
31
    {
32
        $fileHandle = parent::prepareForSave($filename);
33
 
34
        //  Check for paper size and page orientation
35
        $setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup();
36
        $orientation = $this->getOrientation() ?? $setup->getOrientation();
37
        $orientation = ($orientation === PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
38
        $printPaperSize = $this->getPaperSize() ?? $setup->getPaperSize();
39
        $paperSize = self::$paperSizes[$printPaperSize] ?? PageSetup::getPaperSizeDefault();
40
 
41
        //  Create PDF
42
        $config = ['tempDir' => $this->tempDir . '/mpdf'];
43
        $pdf = $this->createExternalWriterInstance($config);
44
        $ortmp = $orientation;
45
        $pdf->_setPageSize($paperSize, $ortmp);
46
        $pdf->DefOrientation = $orientation;
47
        $pdf->AddPageByArray([
48
            'orientation' => $orientation,
49
            'margin-left' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft()),
50
            'margin-right' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getRight()),
51
            'margin-top' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getTop()),
52
            'margin-bottom' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom()),
53
        ]);
54
 
55
        //  Document info
56
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
57
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
58
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
59
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
60
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
61
 
62
        $html = $this->generateHTMLAll();
63
        $bodyLocation = strpos($html, self::SIMULATED_BODY_START);
64
        if ($bodyLocation === false) {
65
            $bodyLocation = strpos($html, self::BODY_TAG);
66
            if ($bodyLocation !== false) {
67
                $bodyLocation += strlen(self::BODY_TAG);
68
            }
69
        }
70
        // Make sure first data presented to Mpdf includes body tag
71
        //   (and any htmlpageheader/htmlpagefooter tags)
72
        //   so that Mpdf doesn't parse it as content. Issue 2432.
73
        if ($bodyLocation !== false) {
74
            $pdf->WriteHTML(substr($html, 0, $bodyLocation));
75
            $html = substr($html, $bodyLocation);
76
        }
77
        foreach (explode("\n", $html) as $line) {
78
            $pdf->WriteHTML("$line\n");
79
        }
80
 
81
        //  Write to file
82
        fwrite($fileHandle, $pdf->Output('', 'S'));
83
 
84
        parent::restoreStateAfterSave();
85
    }
86
 
87
    /**
88
     * Convert inches to mm.
89
     */
90
    private function inchesToMm(float $inches): float
91
    {
92
        return $inches * 25.4;
93
    }
94
}