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.
2020-05-31 04:27:35 +00:00
|
|
|
<?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();
|
2020-06-09 07:22:22 +00:00
|
|
|
$writer->setEditHtmlCallback(null);
|
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.
2020-05-31 04:27:35 +00:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
}
|