Skip to content

Pdf Charts and Drawings #4327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Added

- Nothing yet.
- Pdf Charts and Drawings. [Discussion #4129](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4129) [Discussion #4168](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4168) [PR #4327](https://github.com/PHPOffice/PhpSpreadsheet/pull/4327)

### Changed

Expand Down
3 changes: 3 additions & 0 deletions samples/Chart/32_Chart_read_write_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
$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();
Expand Down
50 changes: 50 additions & 0 deletions samples/Pdf/21f_Drawing_mpdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;

require __DIR__ . '/../Header.php';
require_once __DIR__ . '/Mpdf2.php';

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->getCell('A1')->setValue('A1');
$sheet->getCell('B1')->setValue('B');
$sheet->getCell('C1')->setValue('C');
$sheet->getCell('D1')->setValue('D');
$sheet->getCell('E1')->setValue('E');
$sheet->getCell('F1')->setValue('F');
$sheet->getCell('G1')->setValue('G');
$sheet->getCell('A2')->setValue('A2');
$sheet->getCell('A3')->setValue('A3');
$sheet->getCell('A4')->setValue('A4');
$sheet->getCell('A5')->setValue('A5');
$sheet->getCell('A6')->setValue('A6');
$sheet->getCell('A7')->setValue('A7');
$sheet->getCell('A8')->setValue('A8');

$helper->log('Add drawing to worksheet');
$drawing = new Drawing();
$drawing->setName('Blue Square');
$path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'images/blue_square.png';
$drawing->setPath($path);
$drawing->setResizeProportional(false);
$drawing->setWidth(320);
$drawing->setCoordinates('B2');
$drawing->setCoordinates2('G6');
$drawing->setWorksheet($sheet, true);

$helper->log('Merge drawing cells for Pdf');
$spreadsheet->mergeDrawingCellsForPdf();

$helper->log('Write to Mpdf');
$writer = new Mpdf($spreadsheet);
$filename = $helper->getFileName(__FILE__, 'pdf');
$writer->save($filename);
$helper->log("Saved $filename");
if (PHP_SAPI !== 'cli') {
echo '<a href="/download.php?type=pdf&name=' . basename($filename) . '">Download ' . basename($filename) . '</a><br />';
}
$spreadsheet->disconnectWorksheets();
48 changes: 48 additions & 0 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1636,4 +1636,52 @@ public function setValueBinder(?IValueBinder $valueBinder): self

return $this;
}

/**
* All the PDF writers treat charts as if they occupy a single cell.
* This will be better most of the time.
* It is not needed for any other output type.
* It changes the contents of the spreadsheet, so you might
* be better off cloning the spreadsheet and then using
* this method on, and then writing, the clone.
*/
public function mergeChartCellsForPdf(): void
{
foreach ($this->workSheetCollection as $worksheet) {
foreach ($worksheet->getChartCollection() as $chart) {
$br = $chart->getBottomRightCell();
$tl = $chart->getTopLeftCell();
if ($br !== '' && $br !== $tl) {
if (!$worksheet->cellExists($br)) {
$worksheet->getCell($br)->setValue(' ');
}
$worksheet->mergeCells("$tl:$br");
}
}
}
}

/**
* All the PDF writers do better with drawings than charts.
* This will be better some of the time.
* It is not needed for any other output type.
* It changes the contents of the spreadsheet, so you might
* be better off cloning the spreadsheet and then using
* this method on, and then writing, the clone.
*/
public function mergeDrawingCellsForPdf(): void
{
foreach ($this->workSheetCollection as $worksheet) {
foreach ($worksheet->getDrawingCollection() as $drawing) {
$br = $drawing->getCoordinates2();
$tl = $drawing->getCoordinates();
if ($br !== '' && $br !== $tl) {
if (!$worksheet->cellExists($br)) {
$worksheet->getCell($br)->setValue(' ');
}
$worksheet->mergeCells("$tl:$br");
}
}
}
}
}