PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.36k stars 3.47k forks source link

File corrupted after writer creation and save #4179

Closed kboumedal closed 1 month ago

kboumedal commented 1 month ago

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the current behavior?

File is corrupted after IOFactory::createWriter and save


<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
        $spreadsheet = IOFactory::load(
[SimuV1.xlsx](https://github.com/user-attachments/files/17210539/SimuV1.xlsx)
);

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');

$temporaryPathfile = rtrim(sys_get_temp_dir(), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.md5(uniqid().mt_rand());

$writer->save($temporaryPathfile);

### What features do you think are causing the issue

- [ ] Reader
- [x] Writer
- [ ] Styles
- [ ] Data Validations
- [ ] Formula Calculations
- [ ] Charts
- [ ] AutoFilter
- [ ] Form Elements

### Does an issue affect all spreadsheet file formats? If not, which formats are affected?

xlsx

### Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet 3.3, PHP8.3
oleibman commented 1 month ago

Thank you for the sample file. Problem confirmed. It also happens with PhpSpreadsheet 2.2.0, so probably is not caused by recent changes.

kboumedal commented 1 month ago

No indeed, I tried to update to the last version to see if the problem was solved but no.

oleibman commented 1 month ago

It might take some time to figure this out. One thing that I see in your spreadsheet that I don't see in most others is the use of SVG images, for which we have no formal support. I'm not saying this is causing the problem, but it would be useful to see what happens without them. Is it possible for you to create a copy of your spreadsheet that uses PNG images in place of SVG, and see if the same problem occurs?

oleibman commented 1 month ago

Having nothing to do with SVG, there appears to be a duplicate entry for PNG in ContentTypes. I think that is the reason Xlsx thinks the file is corrupt. Researching ...

oleibman commented 1 month ago

Yes, that's it. Your sheet has a background image, and the code is not properly accounting for the image's extension already being defined in ContentTypes. Background images are fairly unusual, which is probably why this problem hasn't arisen before. The SVG images will be dropped from the copy, but are otherwise not the source of any problems. Expect a fix in a few days.

oleibman commented 1 month ago

If you can test against PR 4180, please do so.

kboumedal commented 1 month ago

Perfect, it works !

Thank you for your quick fix !