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.
This commit is contained in:
parent
edc411e6dd
commit
c47b407e39
|
@ -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
|
||||
<link href="https://fonts.googleapis.com/css2?family=Poiret+One&display=swap" rel="stylesheet" />
|
||||
|
||||
EOF;
|
||||
$html = preg_replace('@<style@', "$linktag<style", $html, 1);
|
||||
$html = str_replace("font-family:'Calibri';",
|
||||
"font-family:'Poiret One','Calibri',sans-serif;",
|
||||
return str_replace('{border: 1px solid black;}',
|
||||
'{border: 2px dashed red;}',
|
||||
$html);
|
||||
|
||||
return $html;
|
||||
}
|
||||
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
||||
$writer->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.
|
||||
|
|
|
@ -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
|
||||
<link href="https://fonts.googleapis.com/css2?family=Poiret+One&display=swap" rel="stylesheet" />
|
||||
|
||||
EOF;
|
||||
$html = preg_replace('@<style@', "$linktag<style", $html, 1);
|
||||
$html = str_replace("font-family:'Calibri';", "font-family:'Poiret One','Calibri',sans-serif;", $html);
|
||||
|
||||
return $html;
|
||||
return str_replace('{border: 1px solid black;}', '{border: 2px dashed red;}', $html);
|
||||
}
|
||||
|
||||
$callStartTime = microtime(true);
|
||||
$writer->setEmbedImages(true);
|
||||
$writer->setEditHtmlCallback('webfont');
|
||||
$writer->setEditHtmlCallback('changeGridlines');
|
||||
$writer->save($filename);
|
||||
$helper->logWrite($writer, $filename, $callStartTime);
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf;
|
||||
|
||||
require __DIR__ . '/../Header.php';
|
||||
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
|
||||
|
@ -13,35 +11,15 @@ $spreadsheet->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 = <<<EOF
|
||||
<style type='text/css'>
|
||||
body {
|
||||
background-color: yellow;
|
||||
return str_replace('{border: 1px solid black;}', '{border: 2px dashed red;}', $html);
|
||||
}
|
||||
</style>
|
||||
|
||||
EOF;
|
||||
|
||||
return preg_replace('@</head>@', "$newstyle</head>", $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);
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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'));
|
||||
|
|
Loading…
Reference in New Issue