Files
PhpSpreadsheet/samples/Pdf/21c_Pdf.php
T
oleibman 8a6142947e Minor Changes to Writer/Mpdf and Writer/Html (#3645)
* Minor Changes to Writer/Mpdf and Writer/Html

Changed a sample to illustrate how to add header/footer in Mpdf using setHtmlEditCallback. This uses custom Html tags, and, like body, it appears that these must be defined in the first writeHtml. Adjust writeMpdf to permit this by using a new constant `SIMULATED_BODY_START`, defined as an Html comment, as a delimiter.

Sample 21c_Pdf, to which the header/footer code is added, had been introduced with PR #2434 to ensure that the body tag was always in the first chunk. However, PR #3016 accidentally invalidated that test by reducing the number of style lines so that the sample now included the body tag in its first 1000 records rather than afterwards. This change puts it past record 1000 again.

Inspecting the results of all the Html/Pdf samples after this change, it turns out that sample 25_In_memory_image was accidentally broken by PR #3535 - the combination of `max-width:100%` (already present before that change) with `position:absolute` (introduced with that change) made the memory drawing disappear from the rendered html when the image occurs in a column after the last column with data in it. It appears that there is no need for max-width (drawings which are not memory drawings do not use it), so it is dropped. The sample is changed to add a second page with a memory drawing, one page with the memory drawing after the last data column, and one with it before. The Html results now reflect the Xlsx result, as they should.

* Minor Performance Improvements

Anonymous function.
2023-07-23 21:22:43 -07:00

71 lines
2.3 KiB
PHP

<?php
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);
$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);
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$counter = 0;
$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', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
$helper->write($spreadsheet, __FILE__, ['Pdf']);
$helper->write(
$spreadsheet,
__FILE__,
['Pdf'],
false,
function (Mpdf $writer): void {
$writer->setEditHtmlCallback('addHeadersFootersMpdf2000');
}
);
$spreadsheet->disconnectWorksheets();