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

Drawing() setPath does not work with special characters #1470

Closed
duncanclaridgebg opened this issue May 12, 2020 · 6 comments · Fixed by #2416
Closed

Drawing() setPath does not work with special characters #1470

duncanclaridgebg opened this issue May 12, 2020 · 6 comments · Fixed by #2416

Comments

@duncanclaridgebg
Copy link

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?

The .xlsx file should open normally and display any added images.

What is the current behavior?

When attempting to open the file, you see an 'Alert' popup saying 'We found a problem with some content in MyFileName.xlsx'.

What are the steps to reproduce?

Use an image file that has a '#' in the filename.

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

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

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...
$spreadsheet->createSheet();
$spreadsheet->setActiveSheetIndex(0);

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('test');
$drawing->setDescription('test');
$drawing->setPath('/var/www/various_folder_names/my_filename_#000000_700x400.png');
$drawing->setCoordinates('A1');
$drawing->setOffsetX(0);
$drawing->setRotation(0);
$drawing->setWorksheet( $spreadsheet->getActiveSheet() );

$objWriter = new Xlsx( $spreadsheet );
$objWriter->save('php://output');

Which versions of PhpSpreadsheet and PHP are affected?

I'm using PHP 7.3 and the version of PhpSpreadsheet is 1.9.0 2019-08-17

@Vagir-dev
Copy link
Contributor

Vagir-dev commented May 13, 2020

Hi!

I checked the situation you've described out and I'm shure it's not a PhpSpreadsheet's bug.
MS Excel doesn't allow drawing's (and not only drawing's, I suppose) filenames to include symbol '#'.
So, when an opened XLSX-file contains an incorrect filename, Excel raises an error.

PhpSpreadsheet doesn't change such filenames in writing methods because it's your (as a programmer) responsibility to provide a correct filenames.

I think you could add a little code to bring the file names to an acceptable format.

Close #1470

@duncanclaridgebg
Copy link
Author

duncanclaridgebg commented May 14, 2020 via email

@Vagir-dev
Copy link
Contributor

Thanks for clearing that up. It’s a great class by-the-way.

Thanks! I'm glad to help.

I have a question to do with very large datasets. We have one report that can be very big – and we constantly run into memory problems with it. Is there a way to save partial sheets and then stitch them together at the end?

I think the solution of the memory problem depends very much on exactly where in the code it happens. Unfortunately, it's hard to say anything useful without analyzing your code.

But for now I don't see any reason why your large report cannnot be created step by step and then merged into a single document.

@stale
Copy link

stale bot commented Jul 13, 2020

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.

@stale stale bot added the stale label Jul 13, 2020
@stale stale bot closed this as completed Jul 20, 2020
@norqf
Copy link

norqf commented Oct 16, 2021

Dentro de la carpeta "vendor\PhpOffice\PhpSpreadsheet\Worksheet" modifica el archivo Drawing.php
public function getIndexedFilename()
{
// $fileName = $this->getFilename();
// $fileName = str_replace(' ', '_', $fileName);
//return str_replace('.' . $this->getExtension(), '', $fileName) . $this->getImageIndex() . '.' . $this->getExtension();
return "imagen".$this->getImageIndex() . '.' . $this->getExtension();
}
solucion temporal

@windmeup62
Copy link

windmeup62 commented Nov 26, 2021

If the problem with "#" in image filename ist still interesting here is a quick solution:
#norqf point me to the right direction.

  1. Go to the file ../Worksheet/Drawing.php
  2. Go to the function "getIndexedFilename()"
  3. Add before or after $filename = str_replace(' ','_', $filename); a new line with:
  4. $filename = str_replace('#','%23', $filename);

That's it. Now Excel find the image and open correctly without faults.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Nov 27, 2021
Fix PHPOffice#2415. Fix PHPOffice#1470. If path name of image contains anything other than ASCII, or if it contains # or space or probably other exceptions, PhpSpreadsheet creates a file that Excel cannot, for whatever reason, read (it is valid xml). When adding an image to a spreadsheet, Excel does not retain the original path name; PhpSpreadsheet does, but probably shouldn't. It is changed to save the image file in the zip as the MD5 hash of the original path name. This produces a file that Excel can read. In addition, it ensures that, if the image is used in multiple places, it is saved in the Excel file only once.

Because this error becomes evident only when opening the file in Excel, it is difficult to write a test case. I have instead duplicated sample Basic/05... using image files whose names match the reported error conditions.
oleibman added a commit that referenced this issue Dec 6, 2021
* Special Characters in Image File Name

Fix #2415. Fix #1470. If path name of image contains anything other than ASCII, or if it contains # or space or probably other exceptions, PhpSpreadsheet creates a file that Excel cannot, for whatever reason, read (it is valid xml). When adding an image to a spreadsheet, Excel does not retain the original path name; PhpSpreadsheet does, but probably shouldn't. It is changed to save the image file in the zip as the MD5 hash of the original path name. This produces a file that Excel can read. In addition, it ensures that, if the image is used in multiple places, it is saved in the Excel file only once.

Because this error becomes evident only when opening the file in Excel, it is difficult to write a test case. I have instead duplicated sample Basic/05... using image files whose names match the reported error conditions.

* Scrutinizer Minor Error

Remove some newly dead code.
@oleibman oleibman removed the stale label Jul 3, 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.

5 participants