-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add ability to save edited Html/Pdf #1499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1861412
Merge pull request #1 from PHPOffice/master
oleibman 7d58ba8
Merge pull request #2 from PHPOffice/master
oleibman 79d024f
Merge pull request #3 from PHPOffice/master
oleibman edc411e
Add ability to save edited Html/Pdf
oleibman 9ba8db7
Update docs/topics/reading-and-writing-to-file.md
oleibman 7e87a9f
Update samples/Pdf/21a_Pdf.php
oleibman 32b0114
Update src/PhpSpreadsheet/Writer/Html.php
oleibman 6800281
Update src/PhpSpreadsheet/Writer/Html.php
oleibman f42c3ef
Update src/PhpSpreadsheet/Writer/Html.php
oleibman 39eeef5
Update src/PhpSpreadsheet/Writer/Html.php
oleibman 48c65cf
Update CallbackTest.php
oleibman bbaf03c
Update Html.php
oleibman c47b407
Different Example for Callback
oleibman d563254
Resoved merged conflict
oleibman 360c8d8
Merge branch 'master' into htmledit
oleibman 2896e6c
Consistent regexp escaping
PowerKiKi 5e64479
Document the callback
PowerKiKi 14a0fa4
New members should always be private
PowerKiKi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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 changeGridlines(string $html): string | ||
{ | ||
return str_replace('{border: 1px solid black;}', '{border: 2px dashed red;}', $html); | ||
} | ||
|
||
$callStartTime = microtime(true); | ||
$writer->setEmbedImages(true); | ||
$writer->setEditHtmlCallback('changeGridlines'); | ||
$writer->save($filename); | ||
$helper->logWrite($writer, $filename, $callStartTime); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; | ||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; | ||
|
||
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); | ||
$spreadsheet->setActiveSheetIndex(0)->setPrintGridlines(true); | ||
|
||
function changeGridlines(string $html): string | ||
{ | ||
return str_replace('{border: 1px solid black;}', '{border: 2px dashed red;}', $html); | ||
} | ||
|
||
$helper->log('Write to Mpdf'); | ||
$writer = new Mpdf($spreadsheet); | ||
$filename = $helper->getFileName('21a_Pdf_mpdf.xlsx', 'pdf'); | ||
$writer->setEditHtmlCallback('changeGridlines'); | ||
$writer->save($filename); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
PowerKiKi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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->setEditHtmlCallback(null); | ||
$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'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.