Skip to content

ODS Writer: Add image/drawing export support #4809

@xxltomcat-ux

Description

@xxltomcat-ux

Summary

Currently, the ODS writer in PhpSpreadsheet does not support exporting images. Images (Drawing or MemoryDrawing objects) are completely ignored in ODS exports, though they work correctly for XLSX. This issue proposes and provides a full implementation to add support for image/drawing export in ODS, bringing feature parity with the XLSX writer.

Problem

  • ODS exports silently drop images and worksheet graphics
  • This is a required feature for interoperability with LibreOffice/OpenOffice users

Solution

A full working patch with 4 files is provided in this post, following existing code quality and cross-version compatibility:

Key Implementation:

  • New: src/PhpSpreadsheet/Writer/Ods/Drawing.php to manage extraction & XML for worksheet images (Drawing & MemoryDrawing)
  • Update: src/PhpSpreadsheet/Writer/Ods/Content.php to integrate images into content.xml and table cells
  • Update: src/PhpSpreadsheet/Writer/Ods/MetaInf.php to list all Pictures/ images in the manifest
  • Update: src/PhpSpreadsheet/Writer/Ods.php to collect/package images into the export ZIP

Features

  • Exports all worksheet images (Drawing, MemoryDrawing)
  • Embeds images in Pictures/ directory inside ODS
  • Writes <draw:frame> and <draw:image> elements linked to cell positions
  • Updates META-INF/manifest.xml with all images
  • Handles cell/row mapping & coordinates
  • Supports PNG, JPEG, GIF
  • No breaking changes

Code Quality

  • Compatible with PHP 7.4+, 8+
  • No named parameter syntax (uses setIterateOnlyExistingCells)
  • No deprecated calls (getValue() and getCalculatedValue())
  • Handles missing file cases

Testing

  • Works in LibreOffice and OpenOffice
  • Backward compatible (spreadsheets with no drawings still export fine)

Usage

No changes in user code:

$drawing = new Drawing();
$drawing->setName('Logo');
$drawing->setPath('./images/logo.png');
$drawing->setCoordinates('B2');
$drawing->setWorksheet($worksheet);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Ods($spreadsheet);
$writer->save('output.ods');

Complete Files

All 4 files are attached in this issue below (see comments/attachment section for copy-paste). They have been thoroughly tested.


This issue provides full code for the maintainers. Community members are invited to review, discuss, and merge. This brings ODS export on par with XLSX and resolves #4808.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions