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'); + } +}