From 670362422360022a8f87d9348d04572a412458c4 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Mon, 8 Oct 2018 09:21:53 +1100 Subject: [PATCH] Write generated HTML into Mpdf in chunks, rather than as one gigantic string Due to a limitation in Mpdf, the HTML string passed to its WriteHTML method must not exceed a particular length. PhpSpreadsheet produces one HTML string containing all spreadsheet data when writing to HTML, which can easily exceed Mpdf's size limit. Thus, it was impossible to write large spreadsheets to PDF using the Mpdf writer - this change fixes that issue. Fixes #637 Fixes #706 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Writer/Pdf/Mpdf.php | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aee7723..41489efe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Csv reader avoid notice when the file is empty - [#743](https://github.com/PHPOffice/PhpSpreadsheet/pull/743) - 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) ## [1.5.0] - 2018-10-21 diff --git a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php index ad0964db..08aa6273 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php @@ -79,11 +79,12 @@ class Mpdf extends Pdf $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); - $pdf->WriteHTML( - $this->generateHTMLHeader(false) . - $this->generateSheetData() . - $this->generateHTMLFooter() - ); + $pdf->WriteHTML($this->generateHTMLHeader(false)); + $html = $this->generateSheetData(); + foreach (\array_chunk(\explode(PHP_EOL, $html), 1000) as $lines) { + $pdf->WriteHTML(\implode(PHP_EOL, $lines)); + } + $pdf->WriteHTML($this->generateHTMLFooter()); // Write to file fwrite($fileHandle, $pdf->Output('', 'S'));