mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-19 21:01:13 +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.
57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
|
|
// Read from Xlsx (.xlsx) template
|
|
$helper->log('Load Xlsx template file');
|
|
$reader = IOFactory::createReader('Xlsx');
|
|
$spreadsheet = $reader->load(__DIR__ . '/../templates/26template.xlsx');
|
|
$spreadsheet->getActiveSheet()->setPrintGridlines(true);
|
|
|
|
// at this point, we could do some manipulations with the template, but we skip this step
|
|
$helper->write($spreadsheet, __FILE__, ['Xlsx', 'Xls', 'Html']);
|
|
|
|
// Export to PDF (mpdf)
|
|
$mpdfCjkWriter = function (Mpdf $writer): void {
|
|
$mpdfCjk = function (string $html): string {
|
|
$html = str_replace("'Calibri'", "'Calibri',Sun-ExtA", $html);
|
|
|
|
return str_replace("'Times New Roman'", "'Times New Roman',Sun-ExtA", $html);
|
|
};
|
|
|
|
$writer->setEditHtmlCallback($mpdfCjk);
|
|
};
|
|
|
|
$helper->log('Write to Mpdf');
|
|
IOFactory::registerWriter('Pdf', Mpdf::class);
|
|
$filename = __FILE__;
|
|
$helper->write($spreadsheet, $filename, ['Pdf'], false, $mpdfCjkWriter);
|
|
|
|
// Remove first two rows with field headers before exporting to CSV
|
|
$helper->log('Removing first two heading rows for CSV export');
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$worksheet->removeRow(1, 2);
|
|
|
|
// Export to CSV (.csv)
|
|
$helper->log('Write to CSV format');
|
|
/** @var Csv $writer */
|
|
$helper->write($spreadsheet, __FILE__, ['Csv']);
|
|
|
|
// Export to CSV with BOM (.csv)
|
|
$filename = str_replace('.php', '-bom.php', __FILE__);
|
|
$helper->log('Write to CSV format (with BOM)');
|
|
$helper->write(
|
|
$spreadsheet,
|
|
$filename,
|
|
['Csv'],
|
|
false,
|
|
function (Csv $writer): void {
|
|
$writer->setUseBOM(true);
|
|
}
|
|
);
|