Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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