diff --git a/samples/Chart/32_Chart_read_write_PDF.php b/samples/Chart/32_Chart_read_write_PDF.php
index 55b8ef2520..0d77d11292 100644
--- a/samples/Chart/32_Chart_read_write_PDF.php
+++ b/samples/Chart/32_Chart_read_write_PDF.php
@@ -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();
diff --git a/samples/Pdf/21f_Drawing_mpdf.php b/samples/Pdf/21f_Drawing_mpdf.php
new file mode 100644
index 0000000000..8787cbfbdc
--- /dev/null
+++ b/samples/Pdf/21f_Drawing_mpdf.php
@@ -0,0 +1,50 @@
+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 'Download ' . basename($filename) . '
';
+}
+$spreadsheet->disconnectWorksheets();
diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php
index 943db95cc3..757235feec 100644
--- a/src/PhpSpreadsheet/Spreadsheet.php
+++ b/src/PhpSpreadsheet/Spreadsheet.php
@@ -1610,4 +1610,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");
+ }
+ }
+ }
+ }
}