From edc411e6dd0ffbd7faf02aea9458f350d5a41574 Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Sat, 30 May 2020 21:27:35 -0700 Subject: [PATCH 01/13] Add ability to save edited Html/Pdf We give users the ability to edit Html/Pdf, but it's a little cumbersome to use the edited Html for an Html file, and difficult to use it for a Pdf. I believe we could make it fairly painless in both cases by allowing the user to set a callback to edit the generated Html. This can be accomplished with fewer than a dozen lines of very simple code. I think this would be easier than grabbing the Html in pieces, editing it, and reassembling it. I think it would also be simpler than an alternative I considered, namely the addition of a new method (e.g. saveEditedHtml) to each of the Html and Pdf writers. One edit that users might like to make when editing html is to add fallback fonts, something that is not currently available in PhpSpreadsheet, and might be difficult to add. A natural extension to that idea would be the use of webfonts, something which is guaranteed difficult to add. See samples/Basic/17b_Html for an example of this. None of the PDF writers support webfonts yet. That doesn't mean they won't do so in future, but, for now, samples/Pdf/21a_Pdf is a prosaic example of something you could do with this callback. In fact, this opens the door to letting the user replace the entire body with data of their choosing, effectively allowing PhpSpreadsheet (where you can set things like paper size and orientation) to be used as a front-end to the Pdf processor without the user having to be be overly familiar with the vagaries of the PDF processor. I think this is actually a pretty nice idea. YMMV. See samples/Basic/21b_Pdf for an example. --- docs/topics/reading-and-writing-to-file.md | 33 +++++++++++- samples/Basic/17b_Html.php | 27 ++++++++++ samples/Pdf/21a_Pdf.php | 47 ++++++++++++++++ samples/Pdf/21b_Pdf.php | 51 ++++++++++++++++++ src/PhpSpreadsheet/Writer/Html.php | 21 ++++++++ .../Writer/Html/CallbackTest.php | 53 +++++++++++++++++++ 6 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 samples/Basic/17b_Html.php create mode 100644 samples/Pdf/21a_Pdf.php create mode 100644 samples/Pdf/21b_Pdf.php create mode 100644 tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php diff --git a/docs/topics/reading-and-writing-to-file.md b/docs/topics/reading-and-writing-to-file.md index 8f92e1f2..abd7c5f3 100644 --- a/docs/topics/reading-and-writing-to-file.md +++ b/docs/topics/reading-and-writing-to-file.md @@ -693,7 +693,7 @@ $sty = $writer->generateStyles(false); // do not write $newstyle = << $sty -html { +body { background-color: yellow; } @@ -703,6 +703,31 @@ echo $writer->generateSheetData(); echo $writer->generateHTMLFooter(); ``` +#### Editing HTML During Save Via a Callback + +You can also add a callback function to edit the generated html +before saving. For example, you could add a webfont +(not currently supported for Pdf) as follows: + +``` php +function webfont(string $html): string +{ + $linktag = << + +EOF; + $html = preg_replace('@setEditHtmlCallback('webfont'); +$writer->save($filename); +``` + #### Writing UTF-8 HTML files A HTML file can be marked as UTF-8 by writing a BOM file header. This @@ -841,6 +866,12 @@ $writer->setPreCalculateFormulas(false); $writer->save("05featuredemo.pdf"); ``` +#### Editing Pdf During Save Via a Callback + +You can also add a callback function to edit the html used to +generate the Pdf before saving. +[See under Html](#editing-html-during-save-via-a-callback). + #### Decimal and thousands separators See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the diff --git a/samples/Basic/17b_Html.php b/samples/Basic/17b_Html.php new file mode 100644 index 00000000..05649314 --- /dev/null +++ b/samples/Basic/17b_Html.php @@ -0,0 +1,27 @@ +getFilename(__FILE__, 'html'); +$writer = new Html($spreadsheet); + +function webfont(string $html): string +{ + $linktag = << + +EOF; + $html = preg_replace('@setEmbedImages(true); +$writer->setEditHtmlCallback('webfont'); +$writer->save($filename); +$helper->logWrite($writer, $filename, $callStartTime); diff --git a/samples/Pdf/21a_Pdf.php b/samples/Pdf/21a_Pdf.php new file mode 100644 index 00000000..c4dc2c48 --- /dev/null +++ b/samples/Pdf/21a_Pdf.php @@ -0,0 +1,47 @@ +log('Hide grid lines'); +$spreadsheet->getActiveSheet()->setShowGridLines(false); + +$helper->log('Set orientation to landscape'); +$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE); + +function yellowBody(string $html): string +{ + $newstyle = << +body { +background-color: yellow; +} + + +EOF; + + return preg_replace('@@', "$newstyle", $html); +} + +$helper->log('Write to Dompdf'); +$writer = new Dompdf($spreadsheet); +$filename = $helper->getFileName('21a_Pdf_dompdf.xlsx', 'pdf'); +$writer->setEditHtmlCallback('yellowBody'); +$writer->save($filename); + +$helper->log('Write to Mpdf'); +$writer = new Mpdf($spreadsheet); +$filename = $helper->getFileName('21a_Pdf_mpdf.xlsx', 'pdf'); +$writer->setEditHtmlCallback('yellowBody'); +$writer->save($filename); + +$helper->log('Write to Tcpdf'); +$writer = new Tcpdf($spreadsheet); +$filename = $helper->getFileName('21a_Pdf_tcpdf.xlsx', 'pdf'); +$writer->setEditHtmlCallback('yellowBody'); +$writer->save($filename); diff --git a/samples/Pdf/21b_Pdf.php b/samples/Pdf/21b_Pdf.php new file mode 100644 index 00000000..142fc344 --- /dev/null +++ b/samples/Pdf/21b_Pdf.php @@ -0,0 +1,51 @@ +.*@ms'; + $bodyrepl = << +

Serif

+

$lorem

+

Sans-Serif

+

$lorem

+

Monospace

+

$lorem

+ +EOF; + + return preg_replace($bodystring, $bodyrepl, $html); +} + +require __DIR__ . '/../Header.php'; +$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php'; + +$helper->log('Hide grid lines'); +$spreadsheet->getActiveSheet()->setShowGridLines(false); + +$helper->log('Set orientation to landscape'); +$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE); + +$helper->log('Write to Dompdf'); +$writer = new Dompdf($spreadsheet); +$filename = $helper->getFileName('21b_Pdf_dompdf.xlsx', 'pdf'); +$writer->setEditHtmlCallback('replaceBody'); +$writer->save($filename); + +$helper->log('Write to Mpdf'); +$writer = new Mpdf($spreadsheet); +$filename = $helper->getFileName('21b_Pdf_mpdf.xlsx', 'pdf'); +$writer->setEditHtmlCallback('replaceBody'); +$writer->save($filename); + +$helper->log('Write to Tcpdf'); +$writer = new Tcpdf($spreadsheet); +$filename = $helper->getFileName('21b_Pdf_tcpdf.xlsx', 'pdf'); +$writer->setEditHtmlCallback('replaceBody'); +$writer->save($filename); diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index c9f2d7cb..963b02d7 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -131,6 +131,13 @@ class Html extends BaseWriter */ private $generateSheetNavigationBlock = true; + /** + * Callback for editing generated html. + * + * @var callable + */ + protected $editHtmlCallback = ''; + /** * Create a new HTML. */ @@ -190,6 +197,10 @@ class Html extends BaseWriter // Write footer $html .= $this->generateHTMLFooter(); + $cbk = $this->editHtmlCallback; + if ($cbk) { + $html = $cbk($html); + } Calculation::setArrayReturnType($saveArrayReturnType); Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); @@ -197,6 +208,16 @@ class Html extends BaseWriter return $html; } + public function setEditHtmlCallback(callable $cbk): void + { + $this->editHtmlCallback = $cbk; + } + + public function resetEditHtmlCallback(): void + { + $this->editHtmlCallback = ''; + } + const VALIGN_ARR = [ Alignment::VERTICAL_BOTTOM => 'bottom', Alignment::VERTICAL_TOP => 'top', diff --git a/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php new file mode 100644 index 00000000..f712419c --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php @@ -0,0 +1,53 @@ + +body { + background-color: yellow; +} + + +EOF; + + return preg_replace('@@', "$newstyle", $html); + } + + public function testSetAndReset(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->setCellValue('A1', '1'); + + $writer = new Html($spreadsheet); + $html1 = $writer->generateHTMLall(); + $writer->setEditHtmlCallback([$this, 'yellowBody']); + $html2 = $writer->generateHTMLall(); + $writer->resetEditHtmlCallback(); + $html3 = $writer->generateHTMLall(); + + self::assertFalse(strpos($html1, 'background-color: yellow')); + self::assertNotFalse(strpos($html2, 'background-color: yellow')); + self::assertFalse(strpos($html3, 'background-color: yellow')); + self::assertEquals($html3, $html1); + + $writer->setEditHtmlCallback([$this, 'yellowBody']); + $oufil = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); + $writer->save($oufil); + $html4 = file_get_contents($oufil); + unlink($oufil); + self::assertNotFalse(strpos($html4, 'background-color: yellow')); + + $this->writeAndReload($spreadsheet, 'Html'); + } +} From 9ba8db761b71b1d0dea43cac98ec18a60ae1911b Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 10:51:53 -0700 Subject: [PATCH 02/13] Update docs/topics/reading-and-writing-to-file.md Co-authored-by: Adrien Crivelli --- docs/topics/reading-and-writing-to-file.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/topics/reading-and-writing-to-file.md b/docs/topics/reading-and-writing-to-file.md index abd7c5f3..aa885f61 100644 --- a/docs/topics/reading-and-writing-to-file.md +++ b/docs/topics/reading-and-writing-to-file.md @@ -703,7 +703,7 @@ echo $writer->generateSheetData(); echo $writer->generateHTMLFooter(); ``` -#### Editing HTML During Save Via a Callback +#### Editing HTML during save via a callback You can also add a callback function to edit the generated html before saving. For example, you could add a webfont From 7e87a9f8d8cdd25dfec07ee2a9a12b791183bf09 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 10:54:30 -0700 Subject: [PATCH 03/13] Update samples/Pdf/21a_Pdf.php Co-authored-by: Adrien Crivelli --- samples/Pdf/21a_Pdf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Pdf/21a_Pdf.php b/samples/Pdf/21a_Pdf.php index c4dc2c48..6ab1938f 100644 --- a/samples/Pdf/21a_Pdf.php +++ b/samples/Pdf/21a_Pdf.php @@ -25,7 +25,7 @@ background-color: yellow; EOF; - return preg_replace('@@', "$newstyle", $html); + return preg_replace('~~', "$newstyle", $html); } $helper->log('Write to Dompdf'); From 32b01148f438d534d09739c62c5a8162cfce1ebf Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 10:56:27 -0700 Subject: [PATCH 04/13] Update src/PhpSpreadsheet/Writer/Html.php Co-authored-by: Adrien Crivelli --- src/PhpSpreadsheet/Writer/Html.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 963b02d7..2796de50 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -197,9 +197,9 @@ class Html extends BaseWriter // Write footer $html .= $this->generateHTMLFooter(); - $cbk = $this->editHtmlCallback; - if ($cbk) { - $html = $cbk($html); + $callback = $this->editHtmlCallback; + if ($callback) { + $html = $callback($html); } Calculation::setArrayReturnType($saveArrayReturnType); From 68002815ec436157e851d863d4968e9b0417f59a Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 10:59:10 -0700 Subject: [PATCH 05/13] Update src/PhpSpreadsheet/Writer/Html.php Co-authored-by: Adrien Crivelli --- src/PhpSpreadsheet/Writer/Html.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 2796de50..1403d9be 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -134,9 +134,9 @@ class Html extends BaseWriter /** * Callback for editing generated html. * - * @var callable + * @var null|callable */ - protected $editHtmlCallback = ''; + protected $editHtmlCallback = null; /** * Create a new HTML. From f42c3ef5a093571f22922c9e708896313e8109a2 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 10:59:52 -0700 Subject: [PATCH 06/13] Update src/PhpSpreadsheet/Writer/Html.php Co-authored-by: Adrien Crivelli --- src/PhpSpreadsheet/Writer/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 1403d9be..fceec678 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -208,7 +208,7 @@ class Html extends BaseWriter return $html; } - public function setEditHtmlCallback(callable $cbk): void + public function setEditHtmlCallback(?callable $cbk): void { $this->editHtmlCallback = $cbk; } From 39eeef5ec658b796a689694a3ea66d95de596910 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 11:01:14 -0700 Subject: [PATCH 07/13] Update src/PhpSpreadsheet/Writer/Html.php Co-authored-by: Adrien Crivelli --- src/PhpSpreadsheet/Writer/Html.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index fceec678..942685f6 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -213,11 +213,6 @@ class Html extends BaseWriter $this->editHtmlCallback = $cbk; } - public function resetEditHtmlCallback(): void - { - $this->editHtmlCallback = ''; - } - const VALIGN_ARR = [ Alignment::VERTICAL_BOTTOM => 'bottom', Alignment::VERTICAL_TOP => 'top', From 48c65cff7fe76dc587757ce3e29d482af9055be0 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 11:11:18 -0700 Subject: [PATCH 08/13] Update CallbackTest.php resetEditHtmlCallback was removed per suggestions from PowerKiki --- tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php index f712419c..388dbd0e 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php @@ -33,7 +33,7 @@ EOF; $html1 = $writer->generateHTMLall(); $writer->setEditHtmlCallback([$this, 'yellowBody']); $html2 = $writer->generateHTMLall(); - $writer->resetEditHtmlCallback(); + $writer->setEditHtmlCallback(null); $html3 = $writer->generateHTMLall(); self::assertFalse(strpos($html1, 'background-color: yellow')); From bbaf03c2efec48720c67dbaa8f3b2aab6a112786 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 31 May 2020 11:18:47 -0700 Subject: [PATCH 09/13] Update Html.php Travis says no need to initialize private class variable to null. --- src/PhpSpreadsheet/Writer/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 942685f6..752f286f 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -136,7 +136,7 @@ class Html extends BaseWriter * * @var null|callable */ - protected $editHtmlCallback = null; + protected $editHtmlCallback; /** * Create a new HTML. From c47b407e3995c27ecc285a2cc09f9a5798fffeac Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Tue, 9 Jun 2020 00:22:22 -0700 Subject: [PATCH 10/13] Different Example for Callback Replace default gridlines with different style. Usable in PDF as well as HTML. Documentation mentioned use of setUseBOM with Html, but that method does not exist, and there is no real reason to support it. Removed it from documentation. --- docs/topics/reading-and-writing-to-file.md | 35 +++++-------------- samples/Basic/17b_Html.php | 13 ++----- samples/Pdf/21a_Pdf.php | 30 +++------------- src/PhpSpreadsheet/Writer/Html.php | 17 ++++----- .../Writer/Html/CallbackTest.php | 2 +- 5 files changed, 22 insertions(+), 75 deletions(-) diff --git a/docs/topics/reading-and-writing-to-file.md b/docs/topics/reading-and-writing-to-file.md index abd7c5f3..13b62e01 100644 --- a/docs/topics/reading-and-writing-to-file.md +++ b/docs/topics/reading-and-writing-to-file.md @@ -703,43 +703,24 @@ echo $writer->generateSheetData(); echo $writer->generateHTMLFooter(); ``` -#### Editing HTML During Save Via a Callback +#### Editing HTML during save via a callback You can also add a callback function to edit the generated html -before saving. For example, you could add a webfont -(not currently supported for Pdf) as follows: +before saving. For example, you could change the gridlines +from a thin solid black line: ``` php -function webfont(string $html): string +function changeGridlines(string $html): string { - $linktag = << - -EOF; - $html = preg_replace('@setEditHtmlCallback('webfont'); +$writer->setEditHtmlCallback('changeGridlines'); $writer->save($filename); ``` -#### Writing UTF-8 HTML files - -A HTML file can be marked as UTF-8 by writing a BOM file header. This -can be enabled by using the following code: - -``` php -$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet); -$writer->setUseBOM(true); - -$writer->save("05featuredemo.htm"); -``` - #### Decimal and thousands separators See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the @@ -866,7 +847,7 @@ $writer->setPreCalculateFormulas(false); $writer->save("05featuredemo.pdf"); ``` -#### Editing Pdf During Save Via a Callback +#### Editing Pdf during save via a callback You can also add a callback function to edit the html used to generate the Pdf before saving. diff --git a/samples/Basic/17b_Html.php b/samples/Basic/17b_Html.php index 05649314..97bb29a3 100644 --- a/samples/Basic/17b_Html.php +++ b/samples/Basic/17b_Html.php @@ -8,20 +8,13 @@ $spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php'; $filename = $helper->getFilename(__FILE__, 'html'); $writer = new Html($spreadsheet); -function webfont(string $html): string +function changeGridlines(string $html): string { - $linktag = << - -EOF; - $html = preg_replace('@setEmbedImages(true); -$writer->setEditHtmlCallback('webfont'); +$writer->setEditHtmlCallback('changeGridlines'); $writer->save($filename); $helper->logWrite($writer, $filename, $callStartTime); diff --git a/samples/Pdf/21a_Pdf.php b/samples/Pdf/21a_Pdf.php index c4dc2c48..b5572afe 100644 --- a/samples/Pdf/21a_Pdf.php +++ b/samples/Pdf/21a_Pdf.php @@ -1,9 +1,7 @@ getActiveSheet()->setShowGridLines(false); $helper->log('Set orientation to landscape'); $spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE); +$spreadsheet->setActiveSheetIndex(0)->setPrintGridlines(true); -function yellowBody(string $html): string +function changeGridlines(string $html): string { - $newstyle = << -body { -background-color: yellow; + return str_replace('{border: 1px solid black;}', '{border: 2px dashed red;}', $html); } - - -EOF; - - return preg_replace('@@', "$newstyle", $html); -} - -$helper->log('Write to Dompdf'); -$writer = new Dompdf($spreadsheet); -$filename = $helper->getFileName('21a_Pdf_dompdf.xlsx', 'pdf'); -$writer->setEditHtmlCallback('yellowBody'); -$writer->save($filename); $helper->log('Write to Mpdf'); $writer = new Mpdf($spreadsheet); $filename = $helper->getFileName('21a_Pdf_mpdf.xlsx', 'pdf'); -$writer->setEditHtmlCallback('yellowBody'); -$writer->save($filename); - -$helper->log('Write to Tcpdf'); -$writer = new Tcpdf($spreadsheet); -$filename = $helper->getFileName('21a_Pdf_tcpdf.xlsx', 'pdf'); -$writer->setEditHtmlCallback('yellowBody'); +$writer->setEditHtmlCallback('changeGridlines'); $writer->save($filename); diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 963b02d7..752f286f 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -134,9 +134,9 @@ class Html extends BaseWriter /** * Callback for editing generated html. * - * @var callable + * @var null|callable */ - protected $editHtmlCallback = ''; + protected $editHtmlCallback; /** * Create a new HTML. @@ -197,9 +197,9 @@ class Html extends BaseWriter // Write footer $html .= $this->generateHTMLFooter(); - $cbk = $this->editHtmlCallback; - if ($cbk) { - $html = $cbk($html); + $callback = $this->editHtmlCallback; + if ($callback) { + $html = $callback($html); } Calculation::setArrayReturnType($saveArrayReturnType); @@ -208,16 +208,11 @@ class Html extends BaseWriter return $html; } - public function setEditHtmlCallback(callable $cbk): void + public function setEditHtmlCallback(?callable $cbk): void { $this->editHtmlCallback = $cbk; } - public function resetEditHtmlCallback(): void - { - $this->editHtmlCallback = ''; - } - const VALIGN_ARR = [ Alignment::VERTICAL_BOTTOM => 'bottom', Alignment::VERTICAL_TOP => 'top', diff --git a/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php index f712419c..388dbd0e 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php @@ -33,7 +33,7 @@ EOF; $html1 = $writer->generateHTMLall(); $writer->setEditHtmlCallback([$this, 'yellowBody']); $html2 = $writer->generateHTMLall(); - $writer->resetEditHtmlCallback(); + $writer->setEditHtmlCallback(null); $html3 = $writer->generateHTMLall(); self::assertFalse(strpos($html1, 'background-color: yellow')); From 2896e6ceb969297f8ff3669bb8d07a91a910a08d Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 28 Jun 2020 17:34:32 +0900 Subject: [PATCH 11/13] Consistent regexp escaping --- samples/Pdf/21b_Pdf.php | 2 +- tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/Pdf/21b_Pdf.php b/samples/Pdf/21b_Pdf.php index 142fc344..c67ff3d2 100644 --- a/samples/Pdf/21b_Pdf.php +++ b/samples/Pdf/21b_Pdf.php @@ -8,7 +8,7 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf; function replaceBody(string $html): string { $lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; - $bodystring = '@.*@ms'; + $bodystring = '~.*~ms'; $bodyrepl = <<

Serif

diff --git a/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php index 388dbd0e..94c201a7 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/CallbackTest.php @@ -20,7 +20,7 @@ body { EOF; - return preg_replace('@@', "$newstyle", $html); + return preg_replace('~~', "$newstyle", $html); } public function testSetAndReset(): void From 5e64479c06b5880182e2b6276f94ba9fd3c75106 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 28 Jun 2020 17:34:50 +0900 Subject: [PATCH 12/13] Document the callback --- src/PhpSpreadsheet/Writer/Html.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 752f286f..f3b40fa1 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -208,9 +208,15 @@ class Html extends BaseWriter return $html; } - public function setEditHtmlCallback(?callable $cbk): void + /** + * Set a callback to edit the entire HTML. + * + * The callback must accept the HTML as string as first parameter, + * and it must return the edited HTML as string. + */ + public function setEditHtmlCallback(?callable $callback): void { - $this->editHtmlCallback = $cbk; + $this->editHtmlCallback = $callback; } const VALIGN_ARR = [ From 14a0fa4cd0601da933616e06dfc95537f822ec80 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 28 Jun 2020 17:35:10 +0900 Subject: [PATCH 13/13] New members should always be private --- src/PhpSpreadsheet/Writer/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index f3b40fa1..eb50c456 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -136,7 +136,7 @@ class Html extends BaseWriter * * @var null|callable */ - protected $editHtmlCallback; + private $editHtmlCallback; /** * Create a new HTML.