Skip to content

Commit 30c1a96

Browse files
committed
feat: add drawing pass-through support for unsupported elements
1 parent fd26e45 commit 30c1a96

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/PhpSpreadsheet/Reader/BaseReader.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ abstract class BaseReader implements IReader
6060
*/
6161
protected bool $createBlankSheetIfNoneRead = false;
6262

63+
/**
64+
* Enable drawing pass-through?
65+
* Identifies whether the Reader should preserve unsupported drawing elements (shapes, grouped images, etc.)
66+
* by storing the original XML for pass-through during write operations.
67+
* When enabled, drawings cannot be modified programmatically but are preserved exactly.
68+
*/
69+
protected bool $enableDrawingPassThrough = false;
70+
6371
/**
6472
* IReadFilter instance.
6573
*/
@@ -125,6 +133,18 @@ public function setIncludeCharts(bool $includeCharts): self
125133
return $this;
126134
}
127135

136+
public function getEnableDrawingPassThrough(): bool
137+
{
138+
return $this->enableDrawingPassThrough;
139+
}
140+
141+
public function setEnableDrawingPassThrough(bool $enableDrawingPassThrough): self
142+
{
143+
$this->enableDrawingPassThrough = $enableDrawingPassThrough;
144+
145+
return $this;
146+
}
147+
128148
/** @return null|string[] */
129149
public function getLoadSheetsOnly(): ?array
130150
{

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,11 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
14811481
$xmlDrawing = $this->loadZipNoNamespace($fileDrawing, '');
14821482
$xmlDrawingChildren = $xmlDrawing->children(Namespaces::SPREADSHEET_DRAWING);
14831483

1484+
// Store drawing XML for pass-through if enabled
1485+
if ($this->enableDrawingPassThrough) {
1486+
$unparsedDrawings[$drawingRelId] = $xmlDrawing->asXML();
1487+
}
1488+
14841489
if ($xmlDrawingChildren->oneCellAnchor) {
14851490
foreach ($xmlDrawingChildren->oneCellAnchor as $oneCellAnchor) {
14861491
$oneCellAnchor = self::testSimpleXml($oneCellAnchor);

src/PhpSpreadsheet/Writer/Xlsx/Drawing.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class Drawing extends WriterPart
2323
*/
2424
public function writeDrawings(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $worksheet, bool $includeCharts = false): string
2525
{
26+
// Check if we have stored drawing XML (pass-through for unsupported elements)
27+
$unparsedLoadedData = $worksheet->getParentOrThrow()->getUnparsedLoadedData();
28+
if (isset($unparsedLoadedData['sheets'][$worksheet->getCodeName()]['Drawings'])) {
29+
return reset($unparsedLoadedData['sheets'][$worksheet->getCodeName()]['Drawings']);
30+
}
31+
2632
// Create XML writer
2733
$objWriter = null;
2834
if ($this->getParentWriter()->getUseDiskCaching()) {

0 commit comments

Comments
 (0)