mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-28 02:58:26 +00:00
4a2b731ec2
Fix #4129. Fix #4168. Html Writer, which all the Pdf writers use, defines its charts and drawings (henceforth I will just use charts for this discussion) using position:absolute and z-index. Browsers handle this correctly, but none of the Pdf writers do, and I can't think of an alternative method of styling them. The result is that the charts take up too much or too little room on the Pdf. I suggested in the two discussions that treating the areas covered by the charts as merged cells might mitigate the problem. I think there are too many unknowns to do so automatically (and see next paragraph). However, adding to Spreadsheet new methods `mergeChartCellsForPdf` and `mergeDrawingCellsForPdf` allows the end user to do this if desired. The new methods are exercised for charts in samples/Chart/32_Chart_read_write_PDF, the results of which are much improved as a result. New samples/Pdf/21f_Drawing_mpdf does likewise for drawings. The new methods alter the spreadsheet they are working on, which could be a problem if you still wish to work with the spreadsheet after writing it to Pdf. In that case, making a copy of the spreadsheet, then calling the new methods on the copy, and writing the copy to Pdf is probably best.
92 lines
3.5 KiB
PHP
92 lines
3.5 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Settings;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
|
|
IOFactory::registerWriter('Pdf', PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
|
|
|
|
// Change these values to select the Rendering library that you wish to use
|
|
//Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
|
|
Settings::setChartRenderer(PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraphRenderer::class);
|
|
|
|
$inputFileType = 'Xlsx';
|
|
$inputFileNames = __DIR__ . '/../templates/36write*.xlsx';
|
|
|
|
if ((isset($argc)) && ($argc > 1)) {
|
|
$inputFileNames = [];
|
|
for ($i = 1; $i < $argc; ++$i) {
|
|
$inputFileNames[] = __DIR__ . '/../templates/' . $argv[$i];
|
|
}
|
|
} else {
|
|
$inputFileNames = glob($inputFileNames) ?: [];
|
|
}
|
|
foreach ($inputFileNames as $inputFileName) {
|
|
$inputFileNameShort = basename($inputFileName);
|
|
|
|
if (!file_exists($inputFileName)) {
|
|
$helper->log('File ' . $inputFileNameShort . ' does not exist');
|
|
|
|
continue;
|
|
}
|
|
|
|
$helper->log("Load Test from $inputFileType file " . $inputFileNameShort);
|
|
|
|
$reader = IOFactory::createReader($inputFileType);
|
|
$reader->setIncludeCharts(true);
|
|
$spreadsheet = $reader->load($inputFileName);
|
|
|
|
$helper->log('Merge chart cells (needed only for Pdf)');
|
|
$spreadsheet->mergeChartCellsForPdf();
|
|
|
|
$helper->log('Iterate worksheets looking at the charts');
|
|
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
|
|
$sheetName = $worksheet->getTitle();
|
|
$helper->log('Worksheet: ' . $sheetName);
|
|
|
|
$chartNames = $worksheet->getChartNames();
|
|
if (empty($chartNames)) {
|
|
$helper->log(' There are no charts in this worksheet');
|
|
} else {
|
|
natsort($chartNames);
|
|
foreach ($chartNames as $i => $chartName) {
|
|
$chart = $worksheet->getChartByNameOrThrow($chartName);
|
|
if ($chart->getTitle() !== null) {
|
|
$caption = '"' . $chart->getTitle()->getCaptionText($spreadsheet) . '"';
|
|
} else {
|
|
$caption = 'Untitled';
|
|
}
|
|
$helper->log(' ' . $chartName . ' - ' . $caption);
|
|
$helper->log(str_repeat(' ', strlen($chartName) + 3));
|
|
$groupCount = $chart->getPlotAreaOrThrow()->getPlotGroupCount();
|
|
if ($groupCount == 1) {
|
|
$chartType = $chart->getPlotAreaOrThrow()->getPlotGroupByIndex(0)->getPlotType();
|
|
$helper->log(' ' . $chartType);
|
|
} else {
|
|
$chartTypes = [];
|
|
for ($i = 0; $i < $groupCount; ++$i) {
|
|
$chartTypes[] = $chart->getPlotAreaOrThrow()->getPlotGroupByIndex($i)->getPlotType();
|
|
}
|
|
$chartTypes = array_unique($chartTypes);
|
|
if (count($chartTypes) == 1) {
|
|
$chartType = 'Multiple Plot ' . array_pop($chartTypes);
|
|
$helper->log(' ' . $chartType);
|
|
} elseif (count($chartTypes) == 0) {
|
|
$helper->log(' *** Type not yet implemented');
|
|
} else {
|
|
$helper->log(' Combination Chart');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Save
|
|
$filename = $helper->getFilename($inputFileName, 'pdf');
|
|
$helper->write($spreadsheet, $filename, ['Pdf'], true);
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
unset($spreadsheet);
|
|
}
|