Skip to content

Commit 4afa751

Browse files
authored
Merge pull request #4327 from oleibman/pdfchart
Pdf Charts and Drawings
2 parents 414f8a2 + db8c768 commit 4afa751

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

12-
- Nothing yet.
12+
- 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)
1313

1414
### Changed
1515

samples/Chart/32_Chart_read_write_PDF.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
$reader->setIncludeCharts(true);
3838
$spreadsheet = $reader->load($inputFileName);
3939

40+
$helper->log('Merge chart cells (needed only for Pdf)');
41+
$spreadsheet->mergeChartCellsForPdf();
42+
4043
$helper->log('Iterate worksheets looking at the charts');
4144
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
4245
$sheetName = $worksheet->getTitle();

samples/Pdf/21f_Drawing_mpdf.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
5+
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
6+
7+
require __DIR__ . '/../Header.php';
8+
require_once __DIR__ . '/Mpdf2.php';
9+
10+
$spreadsheet = new Spreadsheet();
11+
$sheet = $spreadsheet->getActiveSheet();
12+
13+
$sheet->getCell('A1')->setValue('A1');
14+
$sheet->getCell('B1')->setValue('B');
15+
$sheet->getCell('C1')->setValue('C');
16+
$sheet->getCell('D1')->setValue('D');
17+
$sheet->getCell('E1')->setValue('E');
18+
$sheet->getCell('F1')->setValue('F');
19+
$sheet->getCell('G1')->setValue('G');
20+
$sheet->getCell('A2')->setValue('A2');
21+
$sheet->getCell('A3')->setValue('A3');
22+
$sheet->getCell('A4')->setValue('A4');
23+
$sheet->getCell('A5')->setValue('A5');
24+
$sheet->getCell('A6')->setValue('A6');
25+
$sheet->getCell('A7')->setValue('A7');
26+
$sheet->getCell('A8')->setValue('A8');
27+
28+
$helper->log('Add drawing to worksheet');
29+
$drawing = new Drawing();
30+
$drawing->setName('Blue Square');
31+
$path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'images/blue_square.png';
32+
$drawing->setPath($path);
33+
$drawing->setResizeProportional(false);
34+
$drawing->setWidth(320);
35+
$drawing->setCoordinates('B2');
36+
$drawing->setCoordinates2('G6');
37+
$drawing->setWorksheet($sheet, true);
38+
39+
$helper->log('Merge drawing cells for Pdf');
40+
$spreadsheet->mergeDrawingCellsForPdf();
41+
42+
$helper->log('Write to Mpdf');
43+
$writer = new Mpdf($spreadsheet);
44+
$filename = $helper->getFileName(__FILE__, 'pdf');
45+
$writer->save($filename);
46+
$helper->log("Saved $filename");
47+
if (PHP_SAPI !== 'cli') {
48+
echo '<a href="/download.php?type=pdf&name=' . basename($filename) . '">Download ' . basename($filename) . '</a><br />';
49+
}
50+
$spreadsheet->disconnectWorksheets();

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,4 +1636,52 @@ public function setValueBinder(?IValueBinder $valueBinder): self
16361636

16371637
return $this;
16381638
}
1639+
1640+
/**
1641+
* All the PDF writers treat charts as if they occupy a single cell.
1642+
* This will be better most of the time.
1643+
* It is not needed for any other output type.
1644+
* It changes the contents of the spreadsheet, so you might
1645+
* be better off cloning the spreadsheet and then using
1646+
* this method on, and then writing, the clone.
1647+
*/
1648+
public function mergeChartCellsForPdf(): void
1649+
{
1650+
foreach ($this->workSheetCollection as $worksheet) {
1651+
foreach ($worksheet->getChartCollection() as $chart) {
1652+
$br = $chart->getBottomRightCell();
1653+
$tl = $chart->getTopLeftCell();
1654+
if ($br !== '' && $br !== $tl) {
1655+
if (!$worksheet->cellExists($br)) {
1656+
$worksheet->getCell($br)->setValue(' ');
1657+
}
1658+
$worksheet->mergeCells("$tl:$br");
1659+
}
1660+
}
1661+
}
1662+
}
1663+
1664+
/**
1665+
* All the PDF writers do better with drawings than charts.
1666+
* This will be better some of the time.
1667+
* It is not needed for any other output type.
1668+
* It changes the contents of the spreadsheet, so you might
1669+
* be better off cloning the spreadsheet and then using
1670+
* this method on, and then writing, the clone.
1671+
*/
1672+
public function mergeDrawingCellsForPdf(): void
1673+
{
1674+
foreach ($this->workSheetCollection as $worksheet) {
1675+
foreach ($worksheet->getDrawingCollection() as $drawing) {
1676+
$br = $drawing->getCoordinates2();
1677+
$tl = $drawing->getCoordinates();
1678+
if ($br !== '' && $br !== $tl) {
1679+
if (!$worksheet->cellExists($br)) {
1680+
$worksheet->getCell($br)->setValue(' ');
1681+
}
1682+
$worksheet->mergeCells("$tl:$br");
1683+
}
1684+
}
1685+
}
1686+
}
16391687
}

0 commit comments

Comments
 (0)