PHPOffice / PhpSpreadsheet

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

Export HTML to XLSX cause broken file #4231

Open darkvovich opened 2 days ago

darkvovich commented 2 days 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 expected behavior?

Export HTML to XLSX cause broken file in latestPhpSpreadsheet v3.4.0

What is the current behavior?

When I use this code

What are the steps to reproduce?

I catch html via ob_start() + ob_get_clean() and when i push it to writer - it return broken file! But when I POST this html to external script (similar) - it save correct file.

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

$html = ob_get_clean();
$html = preg_replace("/\s+|\n+|\r/", ' ', $html);

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$tableName = 'Act for '.$aCompanyName;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->loadFromString(trim($html));

// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$tableName.'.xlsx"');
header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

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

Which versions of PhpSpreadsheet and PHP are affected?

3.4.0

oleibman commented 2 days ago

I would have to see what's in $html after ob_get_clean to have any idea what's going wrong. Can you upload it?