Files
PhpSpreadsheet/samples/Pdf/21b_Pdf.php
T
oleibman ecbd702628 Phpstan and Samples
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.
2025-04-05 13:54:20 -07:00

76 lines
2.6 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf as TcpdfClass;
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) ?? throw new SpreadsheetException('preg failed');
}
require __DIR__ . '/../Header.php';
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
$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');
IOFactory::registerWriter('Pdf', Dompdf::class);
$filename = str_replace('.php', '_dompdf.php', __FILE__);
$helper->write(
$spreadsheet,
$filename,
['Pdf'],
false,
function (Dompdf $writer): void {
$writer->setEditHtmlCallback('replaceBody');
}
);
$helper->log('Write to Mpdf');
IOFactory::registerWriter('Pdf', Mpdf::class);
$filename = str_replace('.php', '_mpdf.php', __FILE__);
$helper->write(
$spreadsheet,
$filename,
['Pdf'],
false,
function (Mpdf $writer): void {
$writer->setEditHtmlCallback('replaceBody');
}
);
$helper->log('Write to Tcpdf');
IOFactory::registerWriter('Pdf', TcpdfClass::class);
$filename = str_replace('.php', '_tcpdf.php', __FILE__);
$helper->write(
$spreadsheet,
$filename,
['Pdf'],
false,
function (TcpdfClass $writer): void {
$writer->setEditHtmlCallback('replaceBody');
}
);