From c47b407e3995c27ecc285a2cc09f9a5798fffeac Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Tue, 9 Jun 2020 00:22:22 -0700 Subject: [PATCH] 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'));