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.
This commit is contained in:
parent
79d024fec0
commit
edc411e6dd
|
@ -693,7 +693,7 @@ $sty = $writer->generateStyles(false); // do not write <style> and </style>
|
||||||
$newstyle = <<<EOF
|
$newstyle = <<<EOF
|
||||||
<style type='text/css'>
|
<style type='text/css'>
|
||||||
$sty
|
$sty
|
||||||
html {
|
body {
|
||||||
background-color: yellow;
|
background-color: yellow;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
@ -703,6 +703,31 @@ echo $writer->generateSheetData();
|
||||||
echo $writer->generateHTMLFooter();
|
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
|
||||||
|
<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;
|
||||||
|
}
|
||||||
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
|
||||||
|
$writer->setEditHtmlCallback('webfont');
|
||||||
|
$writer->save($filename);
|
||||||
|
```
|
||||||
|
|
||||||
#### Writing UTF-8 HTML files
|
#### Writing UTF-8 HTML files
|
||||||
|
|
||||||
A HTML file can be marked as UTF-8 by writing a BOM file header. This
|
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");
|
$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
|
#### Decimal and thousands separators
|
||||||
|
|
||||||
See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the
|
See section `\PhpOffice\PhpSpreadsheet\Writer\Csv` how to control the
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Writer\Html;
|
||||||
|
|
||||||
|
require __DIR__ . '/../Header.php';
|
||||||
|
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
|
||||||
|
|
||||||
|
$filename = $helper->getFilename(__FILE__, 'html');
|
||||||
|
$writer = new Html($spreadsheet);
|
||||||
|
|
||||||
|
function webfont(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
$callStartTime = microtime(true);
|
||||||
|
$writer->setEmbedImages(true);
|
||||||
|
$writer->setEditHtmlCallback('webfont');
|
||||||
|
$writer->save($filename);
|
||||||
|
$helper->logWrite($writer, $filename, $callStartTime);
|
|
@ -0,0 +1,47 @@
|
||||||
|
<?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';
|
||||||
|
|
||||||
|
$helper->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 = <<<EOF
|
||||||
|
<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');
|
||||||
|
$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);
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
||||||
|
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 = '@<body>.*</body>@ms';
|
||||||
|
$bodyrepl = <<<EOF
|
||||||
|
<body>
|
||||||
|
<h1>Serif</h1>
|
||||||
|
<p style='font-family: serif; font-size: 12pt;'>$lorem</p>
|
||||||
|
<h1>Sans-Serif</h1>
|
||||||
|
<p style='font-family: sans-serif; font-size: 12pt;'>$lorem</p>
|
||||||
|
<h1>Monospace</h1>
|
||||||
|
<p style='font-family: monospace; font-size: 12pt;'>$lorem</p>
|
||||||
|
</body>
|
||||||
|
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);
|
|
@ -131,6 +131,13 @@ class Html extends BaseWriter
|
||||||
*/
|
*/
|
||||||
private $generateSheetNavigationBlock = true;
|
private $generateSheetNavigationBlock = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for editing generated html.
|
||||||
|
*
|
||||||
|
* @var callable
|
||||||
|
*/
|
||||||
|
protected $editHtmlCallback = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new HTML.
|
* Create a new HTML.
|
||||||
*/
|
*/
|
||||||
|
@ -190,6 +197,10 @@ class Html extends BaseWriter
|
||||||
|
|
||||||
// Write footer
|
// Write footer
|
||||||
$html .= $this->generateHTMLFooter();
|
$html .= $this->generateHTMLFooter();
|
||||||
|
$cbk = $this->editHtmlCallback;
|
||||||
|
if ($cbk) {
|
||||||
|
$html = $cbk($html);
|
||||||
|
}
|
||||||
|
|
||||||
Calculation::setArrayReturnType($saveArrayReturnType);
|
Calculation::setArrayReturnType($saveArrayReturnType);
|
||||||
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
|
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
|
||||||
|
@ -197,6 +208,16 @@ class Html extends BaseWriter
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setEditHtmlCallback(callable $cbk): void
|
||||||
|
{
|
||||||
|
$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',
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Writer\Html;
|
||||||
|
use PhpOffice\PhpSpreadsheetTests\Functional;
|
||||||
|
|
||||||
|
class CallbackTest extends Functional\AbstractFunctional
|
||||||
|
{
|
||||||
|
public function yellowBody(string $html): string
|
||||||
|
{
|
||||||
|
$newstyle = <<<EOF
|
||||||
|
<style type='text/css'>
|
||||||
|
body {
|
||||||
|
background-color: yellow;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
EOF;
|
||||||
|
|
||||||
|
return preg_replace('@</head>@', "$newstyle</head>", $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');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue