Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrupt XLSX file when drawing image #1022

Closed
ss-ikeda opened this issue Jun 19, 2019 · 15 comments · Fixed by #1462
Closed

Corrupt XLSX file when drawing image #1022

ss-ikeda opened this issue Jun 19, 2019 · 15 comments · Fixed by #1462

Comments

@ss-ikeda
Copy link

This is:

- [* ] 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)

I wanted to download xlsx file in which added image file with phpspreadsheet.
Then I am facing issue of excel file broken due to "$drawing" as below source code.

What is the expected behavior?

No error when open the excel file.

What is the current behavior?

When opened the xlsx file, it always asked if fix to see.

What are the steps to reproduce?

Kindly try below code...

// add code that show the issue here...

use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PhpOffice\PhpSpreadsheet\Writer\CSV as CSVWriter;

class TestsController extends AppController{

public function output(){

    //read template
    $reader = new XlsxReader();
    $spreadsheet = $reader->load(WWW_ROOT.'files/template/empty.xlsx');
    $spreadsheet->setActiveSheetIndex(0);
    $sheet = $spreadsheet->getActiveSheet();

    //add image in excel
    $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
    $drawing->setName('Logo');
    $drawing->setDescription('Logo');
    $drawing->setPath(WWW_ROOT."docs/C20101Docs/20190404202343.png"); 
    $drawing->setCoordinates('A1');
    $drawing->setWorksheet($sheet);
    unset($drawing);

    //create tmp file
    $tmp_path = WWW_ROOT.'files/test_tmp.xlsx';
    $writer = new XlsxWriter($spreadsheet);
    ob_end_clean();
    $writer->save($tmp_path);

    $content = 'attachment;';
    $content .= 'filename*=UTF-8\'\''.rawurlencode("TEST.xlsx"); 
    $this->response->header('Content-Disposition', $content);
    $this->response->type('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    $this->response->file($tmp_path);

    return $this->response;
}

}

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet 1.7
PHP 7.0.3
cakephp3.6

@damijanc
Copy link

just a question, are you missing drawing->setHeight(36);?

@ss-ikeda
Copy link
Author

I tried "drawing->setHeight(36);"
Still facing same issue.

@dangthanhnhat
Copy link

dangthanhnhat commented Jun 21, 2019

Try using xls template then output xlsx file

@ss-ikeda
Copy link
Author

I tried xls template then facing this error "You tried to set a sheet active by the out of bounds index: 0. The actual number of sheets is 0."

@dangthanhnhat
Copy link

dangthanhnhat commented Jun 21, 2019

You must use Class Xls
use PhpSpreadsheet\Reader\Xls;
Then
$reader = new Xls();

@ss-ikeda
Copy link
Author

I could download but the output xlsx looks broken...
When opened the xlsx file, it asked if fix to see.

@dangthanhnhat
Copy link

Did you open template xlsx file and save as xls file or just change the extension ?

@ss-ikeda
Copy link
Author

I opened template xlsx file and saved as xls file...

@dangthanhnhat
Copy link

I opened template xlsx file and saved as xls file...

Try this header
https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#http-headers

@ss-ikeda
Copy link
Author

ss-ikeda commented Jun 21, 2019

"$writer->save($tmp_path);" saves same as the output xlsx as temporary file in the server before downloading. I checked the file and tried to open it.
Then confirmed the issue occured in the temporary file.

Also tried header
https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#http-headers

@beoss
Copy link

beoss commented Jul 3, 2019

I have encountered the same issue but it is only present when loading a template. If I create a sheet using the "new Spreadsheet()" syntax it works as expected.

After additional testing if I created my template entirely in PHP using the library it works correctly. The error only happens if I build a template in Excel (2019) and then use that file.

@githubjeka
Copy link

I have same issue too.
#877 (comment)

@stale
Copy link

stale bot commented Sep 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@whosphp
Copy link

whosphp commented Dec 22, 2020

Try using xls template then output xlsx file

This works for me, thank you

@oleibman
Copy link
Collaborator

FIxed by #1462.

@oleibman oleibman removed the stale label Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

7 participants