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();
|
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
|
You can also add a callback function to edit the generated html
|
||||||
before saving. For example, you could add a webfont
|
before saving. For example, you could change the gridlines
|
||||||
(not currently supported for Pdf) as follows:
|
from a thin solid black line:
|
||||||
|
|
||||||
``` php
|
``` php
|
||||||
function webfont(string $html): string
|
function changeGridlines(string $html): string
|
||||||
{
|
{
|
||||||
$linktag = <<<EOF
|
return str_replace('{border: 1px solid black;}',
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Poiret+One&display=swap" rel="stylesheet" />
|
'{border: 2px dashed red;}',
|
||||||
|
|
||||||
EOF;
|
|
||||||
$html = preg_replace('@<style@', "$linktag<style", $html, 1);
|
|
||||||
$html = str_replace("font-family:'Calibri';",
|
|
||||||
"font-family:'Poiret One','Calibri',sans-serif;",
|
|
||||||
$html);
|
$html);
|
||||||
|
|
||||||
return $html;
|
|
||||||
}
|
}
|
||||||
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
||||||
$writer->setEditHtmlCallback('webfont');
|
$writer->setEditHtmlCallback('changeGridlines');
|
||||||
$writer->save($filename);
|
$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
|
#### Decimal and thousands separators
|
||||||
|
|
||||||
See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the
|
See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the
|
||||||
|
@ -866,7 +847,7 @@ $writer->setPreCalculateFormulas(false);
|
||||||
$writer->save("05featuredemo.pdf");
|
$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
|
You can also add a callback function to edit the html used to
|
||||||
generate the Pdf before saving.
|
generate the Pdf before saving.
|
||||||
|
|
|
@ -8,20 +8,13 @@ $spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
|
||||||
$filename = $helper->getFilename(__FILE__, 'html');
|
$filename = $helper->getFilename(__FILE__, 'html');
|
||||||
$writer = new Html($spreadsheet);
|
$writer = new Html($spreadsheet);
|
||||||
|
|
||||||
function webfont(string $html): string
|
function changeGridlines(string $html): string
|
||||||
{
|
{
|
||||||
$linktag = <<<EOF
|
return str_replace('{border: 1px solid black;}', '{border: 2px dashed red;}', $html);
|
||||||
<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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$callStartTime = microtime(true);
|
$callStartTime = microtime(true);
|
||||||
$writer->setEmbedImages(true);
|
$writer->setEmbedImages(true);
|
||||||
$writer->setEditHtmlCallback('webfont');
|
$writer->setEditHtmlCallback('changeGridlines');
|
||||||
$writer->save($filename);
|
$writer->save($filename);
|
||||||
$helper->logWrite($writer, $filename, $callStartTime);
|
$helper->logWrite($writer, $filename, $callStartTime);
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf;
|
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf;
|
|
||||||
|
|
||||||
require __DIR__ . '/../Header.php';
|
require __DIR__ . '/../Header.php';
|
||||||
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
|
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
|
||||||
|
@ -13,35 +11,15 @@ $spreadsheet->getActiveSheet()->setShowGridLines(false);
|
||||||
|
|
||||||
$helper->log('Set orientation to landscape');
|
$helper->log('Set orientation to landscape');
|
||||||
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_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
|
return str_replace('{border: 1px solid black;}', '{border: 2px dashed red;}', $html);
|
||||||
<style type='text/css'>
|
|
||||||
body {
|
|
||||||
background-color: yellow;
|
|
||||||
}
|
}
|
||||||
</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');
|
$helper->log('Write to Mpdf');
|
||||||
$writer = new Mpdf($spreadsheet);
|
$writer = new Mpdf($spreadsheet);
|
||||||
$filename = $helper->getFileName('21a_Pdf_mpdf.xlsx', 'pdf');
|
$filename = $helper->getFileName('21a_Pdf_mpdf.xlsx', 'pdf');
|
||||||
$writer->setEditHtmlCallback('yellowBody');
|
$writer->setEditHtmlCallback('changeGridlines');
|
||||||
$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);
|
$writer->save($filename);
|
||||||
|
|
|
@ -134,9 +134,9 @@ class Html extends BaseWriter
|
||||||
/**
|
/**
|
||||||
* Callback for editing generated html.
|
* Callback for editing generated html.
|
||||||
*
|
*
|
||||||
* @var callable
|
* @var null|callable
|
||||||
*/
|
*/
|
||||||
protected $editHtmlCallback = '';
|
protected $editHtmlCallback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new HTML.
|
* Create a new HTML.
|
||||||
|
@ -197,9 +197,9 @@ class Html extends BaseWriter
|
||||||
|
|
||||||
// Write footer
|
// Write footer
|
||||||
$html .= $this->generateHTMLFooter();
|
$html .= $this->generateHTMLFooter();
|
||||||
$cbk = $this->editHtmlCallback;
|
$callback = $this->editHtmlCallback;
|
||||||
if ($cbk) {
|
if ($callback) {
|
||||||
$html = $cbk($html);
|
$html = $callback($html);
|
||||||
}
|
}
|
||||||
|
|
||||||
Calculation::setArrayReturnType($saveArrayReturnType);
|
Calculation::setArrayReturnType($saveArrayReturnType);
|
||||||
|
@ -208,16 +208,11 @@ class Html extends BaseWriter
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setEditHtmlCallback(callable $cbk): void
|
public function setEditHtmlCallback(?callable $cbk): void
|
||||||
{
|
{
|
||||||
$this->editHtmlCallback = $cbk;
|
$this->editHtmlCallback = $cbk;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resetEditHtmlCallback(): void
|
|
||||||
{
|
|
||||||
$this->editHtmlCallback = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
const VALIGN_ARR = [
|
const VALIGN_ARR = [
|
||||||
Alignment::VERTICAL_BOTTOM => 'bottom',
|
Alignment::VERTICAL_BOTTOM => 'bottom',
|
||||||
Alignment::VERTICAL_TOP => 'top',
|
Alignment::VERTICAL_TOP => 'top',
|
||||||
|
|
|
@ -33,7 +33,7 @@ EOF;
|
||||||
$html1 = $writer->generateHTMLall();
|
$html1 = $writer->generateHTMLall();
|
||||||
$writer->setEditHtmlCallback([$this, 'yellowBody']);
|
$writer->setEditHtmlCallback([$this, 'yellowBody']);
|
||||||
$html2 = $writer->generateHTMLall();
|
$html2 = $writer->generateHTMLall();
|
||||||
$writer->resetEditHtmlCallback();
|
$writer->setEditHtmlCallback(null);
|
||||||
$html3 = $writer->generateHTMLall();
|
$html3 = $writer->generateHTMLall();
|
||||||
|
|
||||||
self::assertFalse(strpos($html1, 'background-color: yellow'));
|
self::assertFalse(strpos($html1, 'background-color: yellow'));
|
||||||
|
|
Loading…
Reference in New Issue