From 31e25ad14b844f8a5c724725470cc5ed95cc28f5 Mon Sep 17 00:00:00 2001 From: Albert Scherman Date: Tue, 30 Oct 2018 13:56:04 +0200 Subject: [PATCH] Support page margin in mPDF Fixes #750 Fixes #751 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Writer/Pdf/Mpdf.php | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41489efe..038863f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Fix print area parser for XLSX reader - [#734](https://github.com/PHPOffice/PhpSpreadsheet/pull/734) - Support overriding `DefaultValueBinder::dataTypeForValue()` without overriding `DefaultValueBinder::bindValue()` - [#735](https://github.com/PHPOffice/PhpSpreadsheet/pull/735) - Mpdf export can exceed pcre.backtrack_limit - [#637](https://github.com/PHPOffice/PhpSpreadsheet/issues/637) +- Support page margin in mPDF - [#750](https://github.com/PHPOffice/PhpSpreadsheet/issues/750) ## [1.5.0] - 2018-10-21 diff --git a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php index 08aa6273..fd2664a8 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php @@ -70,7 +70,13 @@ class Mpdf extends Pdf $ortmp = $orientation; $pdf->_setPageSize(strtoupper($paperSize), $ortmp); $pdf->DefOrientation = $orientation; - $pdf->AddPage($orientation); + $pdf->AddPageByArray([ + 'orientation' => $orientation, + 'margin-left' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft()), + 'margin-right' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getRight()), + 'margin-top' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getTop()), + 'margin-bottom' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom()), + ]); // Document info $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); @@ -91,4 +97,16 @@ class Mpdf extends Pdf parent::restoreStateAfterSave($fileHandle); } + + /** + * Convert inches to mm. + * + * @param float $inches + * + * @return float + */ + private function inchesToMm($inches) + { + return $inches * 25.4; + } }