mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-29 03:28:20 +00:00
ecbd702628
Phpstan hasn't identified any errors in Samples in a long time. But, as it turns out, one particular error is suppressed: ``` Variable $helper might not be defined. ``` This error would be generated by almost all samples, and the errors that it suppresses will become problematic if we move to Level 10. We are not yet committed to doing that, but it is pretty easy to write a script to change the samples so that error no longer happens, and there isn't really any reason to delay doing so. The results of that script constitute this PR.
73 lines
2.5 KiB
PHP
73 lines
2.5 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
|
|
// Issue 2432 - styles were too large to fit in first Mpdf chunk, causing problems.
|
|
|
|
function addHeadersFootersMpdf2000(string $html): string
|
|
{
|
|
$pagerepl = <<<EOF
|
|
@page page0 {
|
|
odd-header-name: html_myHeader1;
|
|
odd-footer-name: html_myFooter2;
|
|
|
|
EOF;
|
|
$html = preg_replace('/@page page0 {/', $pagerepl, $html) ?? throw new SpreadsheetException('preg 1 failed');
|
|
$bodystring = '/<body>/';
|
|
$simulatedBodyStart = Mpdf::SIMULATED_BODY_START;
|
|
$bodyrepl = <<<EOF
|
|
<body>
|
|
<htmlpageheader name="myHeader1" style="display:none">
|
|
<div style="text-align: right; border-bottom: 1px solid #000000; font-weight: bold; font-size: 10pt;">
|
|
My document header
|
|
</div>
|
|
</htmlpageheader>
|
|
|
|
<htmlpagefooter name="myFooter2" style="display:none">
|
|
<table width="100%">
|
|
<tr>
|
|
<td width="33%">My document</td>
|
|
<td width="33%" align="center">Page {PAGENO} of {nbpg}</td>
|
|
<td width="33%" style="text-align: right;">{DATE Y-m-j}</td>
|
|
</tr>
|
|
</table>
|
|
</htmlpagefooter>
|
|
$simulatedBodyStart
|
|
|
|
EOF;
|
|
|
|
return preg_replace($bodystring, $bodyrepl, $html) ?? throw new SpreadsheetException('preg 2 failed');
|
|
}
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$counter = 0;
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
$helper->log('Populate spreadsheet');
|
|
for ($row = 1; $row < 1001; ++$row) {
|
|
$sheet->getCell("A$row")->setValue(++$counter);
|
|
// Add many styles by using slight variations of font color for each.
|
|
$sheet->getCell("A$row")->getStyle()->getFont()->getColor()->setRgb(sprintf('%06x', $counter));
|
|
$sheet->getCell("B$row")->setValue(++$counter);
|
|
$sheet->getCell("C$row")->setValue(++$counter);
|
|
}
|
|
|
|
$helper->log('Write to Mpdf');
|
|
IOFactory::registerWriter('Pdf', Mpdf::class);
|
|
$helper->write($spreadsheet, __FILE__, ['Pdf']);
|
|
$helper->write(
|
|
$spreadsheet,
|
|
__FILE__,
|
|
['Pdf'],
|
|
false,
|
|
function (Mpdf $writer): void {
|
|
$writer->setEditHtmlCallback('addHeadersFootersMpdf2000');
|
|
}
|
|
);
|
|
$spreadsheet->disconnectWorksheets();
|