| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf; |
| 4 |
|
| 5 |
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
| 6 |
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
| 7 |
use PhpOffice\PhpSpreadsheet\Writer\Pdf; |
| 8 |
|
| 9 |
class Mpdf extends Pdf |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Gets the implementation of external PDF library that should be used. |
| 13 |
* |
| 14 |
* @param array $config Configuration array |
| 15 |
* |
| 16 |
* @return \Mpdf\Mpdf implementation |
| 17 |
*/ |
| 18 |
protected function createExternalWriterInstance($config) |
| 19 |
{ |
| 20 |
return new \Mpdf\Mpdf($config); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Save Spreadsheet to file. |
| 25 |
* |
| 26 |
* @param string $pFilename Name of the file to save as |
| 27 |
* |
| 28 |
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
| 29 |
* @throws PhpSpreadsheetException |
| 30 |
*/ |
| 31 |
public function save($pFilename) |
| 32 |
{ |
| 33 |
$fileHandle = parent::prepareForSave($pFilename); |
| 34 |
|
| 35 |
// Default PDF paper size |
| 36 |
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.) |
| 37 |
|
| 38 |
// Check for paper size and page orientation |
| 39 |
if (null === $this->getSheetIndex()) { |
| 40 |
$orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation() |
| 41 |
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
| 42 |
$printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize(); |
| 43 |
} else { |
| 44 |
$orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() |
| 45 |
== PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
| 46 |
$printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize(); |
| 47 |
} |
| 48 |
$this->setOrientation($orientation); |
| 49 |
|
| 50 |
// Override Page Orientation |
| 51 |
if (null !== $this->getOrientation()) { |
| 52 |
$orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT) |
| 53 |
? PageSetup::ORIENTATION_PORTRAIT |
| 54 |
: $this->getOrientation(); |
| 55 |
} |
| 56 |
$orientation = strtoupper($orientation); |
| 57 |
|
| 58 |
// Override Paper Size |
| 59 |
if (null !== $this->getPaperSize()) { |
| 60 |
$printPaperSize = $this->getPaperSize(); |
| 61 |
} |
| 62 |
|
| 63 |
if (isset(self::$paperSizes[$printPaperSize])) { |
| 64 |
$paperSize = self::$paperSizes[$printPaperSize]; |
| 65 |
} |
| 66 |
|
| 67 |
// Create PDF |
| 68 |
$config = ['tempDir' => $this->tempDir]; |
| 69 |
$pdf = $this->createExternalWriterInstance($config); |
| 70 |
$ortmp = $orientation; |
| 71 |
$pdf->_setPageSize(strtoupper($paperSize), $ortmp); |
| 72 |
$pdf->DefOrientation = $orientation; |
| 73 |
$pdf->AddPageByArray([ |
| 74 |
'orientation' => $orientation, |
| 75 |
'margin-left' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft()), |
| 76 |
'margin-right' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getRight()), |
| 77 |
'margin-top' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getTop()), |
| 78 |
'margin-bottom' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom()), |
| 79 |
]); |
| 80 |
|
| 81 |
// Document info |
| 82 |
$pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); |
| 83 |
$pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator()); |
| 84 |
$pdf->SetSubject($this->spreadsheet->getProperties()->getSubject()); |
| 85 |
$pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); |
| 86 |
$pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); |
| 87 |
|
| 88 |
$pdf->WriteHTML($this->generateHTMLHeader(false)); |
| 89 |
$html = $this->generateSheetData(); |
| 90 |
foreach (\array_chunk(\explode(PHP_EOL, $html), 1000) as $lines) { |
| 91 |
$pdf->WriteHTML(\implode(PHP_EOL, $lines)); |
| 92 |
} |
| 93 |
$pdf->WriteHTML($this->generateHTMLFooter()); |
| 94 |
|
| 95 |
// Write to file |
| 96 |
fwrite($fileHandle, $pdf->Output('', 'S')); |
| 97 |
|
| 98 |
parent::restoreStateAfterSave($fileHandle); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Convert inches to mm. |
| 103 |
* |
| 104 |
* @param float $inches |
| 105 |
* |
| 106 |
* @return float |
| 107 |
*/ |
| 108 |
private function inchesToMm($inches) |
| 109 |
{ |
| 110 |
return $inches * 25.4; |
| 111 |
} |
| 112 |
} |
| 113 |
|