-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Inserting a drawing / chart causes print options to break in version 1.3.1 #610
Comments
@rf1234 what template are you using? |
I guess I found the problem now: If I insert a drawing into one of the sheets (it can be a jpeg or an Excel chart) the problem occurs: This rather simple code which does almost nothing causes the print option problem: $templateFile = "Darlehensuebersicht_template.xlsx";
//we have xlsx and xls input files the input file types are hence Xlsx or Xls
$readerType = ucfirst( substr($templateFile, strrpos($templateFile, '.') + 1) );
if ( $readerType === 'Xlsm' ) {
$readerType = 'Xlsx';
}
//open spreadsheet template
$templateFilePath = __DIR__ . '/reporting/templates/' . $templateFile;
$reader = IOFactory::createReader($readerType);
$reader->setIncludeCharts(false); //we don't want to load charts
$spreadsheet = $reader->load($templateFilePath);
$outputFile = "Darlehensuebersicht.xlsx";
$outputFileType = substr($outputFile, strrpos($outputFile, '.') + 1);
$writerType = ucfirst( $outputFileType );
if ( $writerType === 'Xlsm' ) {
$writerType = 'Xlsx';
}
//change $outputFile by removing xls or xlsx
$outputFile = substr($outputFile, 0, strrpos( $outputFile, '.') );
$reportId = "1.3.1";
$outputFileName = $outputFile . '_' . $reportId . '.' . $outputFileType;
//set fields to update the file table and to write the file
$webPath = '/lgfuploads/reporting/' . $outputFileName;
$systemPath = $_SERVER['DOCUMENT_ROOT'] . $webPath;
$spreadsheet->setActiveSheetIndex(0);
$col = 'K';
$ws = $spreadsheet->getActiveSheet();
$logoPath = __DIR__ . '/reporting/templates/SmallLogo.jpg';
$drawing = new Drawing();
$drawing->setPath($logoPath);
$drawing->setCoordinates($col.'2');
$drawing->setHeight(45); //pixels
$colWidth = $ws->getColumnDimension($col)->getWidth();
if ($colWidth == -1) { //not defined which means we have the standard width
$colWidthPixels = 64; //pixels, this is the standard width of an Excel cell in pixels = 9.140625 char units outer size
} else { //innner width is 8.43 char units
$colWidthPixels = $colWidth * 7.0017094; //colwidht in Char Units * Pixels per CharUnit
}
$offsetX = $colWidthPixels - $drawing->getWidth(); //pixels
$drawing->setOffsetX($offsetX); //pixels
$drawing->setWorksheet($ws);
//write Excel file based on template
$writer = IOFactory::createWriter($spreadsheet, $writerType);
$writer->setIncludeCharts(true);
$writer->setPreCalculateFormulas(true);
$writer->save($systemPath); "Issue checked" means I open "file/check for issues" and have Excel fix any issues in the template. If I remove this code I have no problem: $col = 'K';
$ws = $spreadsheet->getActiveSheet();
$logoPath = __DIR__ . '/reporting/templates/SmallLogo.jpg';
$drawing = new Drawing();
$drawing->setPath($logoPath);
$drawing->setCoordinates($col.'2');
$drawing->setHeight(45); //pixels
$colWidth = $ws->getColumnDimension($col)->getWidth();
if ($colWidth == -1) { //not defined which means we have the standard width
$colWidthPixels = 64; //pixels, this is the standard width of an Excel cell in pixels = 9.140625 char units outer size
} else { //innner width is 8.43 char units
$colWidthPixels = $colWidth * 7.0017094; //colwidht in Char Units * Pixels per CharUnit
}
$offsetX = $colWidthPixels - $drawing->getWidth(); //pixels
$drawing->setOffsetX($offsetX); //pixels
$drawing->setWorksheet($ws);
All of this used to work perfectly with version 1.2.1! Hence I moved back to 1.2.1. I attached four files: the template file, the resulting file without drawing which is ok, the resulting file with drawing which is broken and the picture file that I insert as a drawing. I am actually surprised that I seem to be the first user facing the issue. I thought many people insert charts or pictures programmatically. All of them should be facing the issue once migrating to 1.3.1. Please help! Darlehensuebersicht_1.3.1_without_drawing.xlsx |
Found a work around in the meantime: I use versions 1.3.1 and 1.2.1 in parallel depending on the report type. I am lucky that the xlsm files I write do not contain drawings (no logos, no charts). Hence I can use 1.3.1 for xlsm and have the benefit of not losing a button that is contained in the template I read. For all other files (almost all of them have drawings) I use version 1.2.1 and hence avoid the print option issue mentioned above. Hope this gets fixed soon; I think it is a pretty serious bug because version 1.3.1 is unusable for files containing drawings / pictures / charts if you don't want to bother the user with files that need repair. |
@rf1234 are you able to create a Minimal, Complete, and Verifiable example of code that exhibits this issue without relying on an external Excel file ? |
Sorry, no I can't. I never use PHPSpreadsheet without a template. Wouldn't know any reason for using it without a template to be honest. You can find my template attached above and also the full code I used. |
@PowerKiKi |
To maximise the chance of getting help, you should submit the minimal code to reproduce the issue. What you submitted so far is clearly not the minimum. It should be a only a few line of code. You should be able to create a brand new spreadsheet, set printing options, add a drawing and save the file to demonstrate the bug. |
That's not working. The error does not occur if you do not use a template. I have submitted everything I have. If this still isn't good enough then I can't help it. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Stale or not the problem still persists. |
@JParkerCFI , you are right. All of the issues I have brought up so far have become "stale" after a while. Virtually nothing got resolved! I am deeply frustrated that I obviously just wasted my time. |
This project heavily relies on community contributions, because we don't have the resources to do it ourselves. This unfortunately means that if a bug is not resolved in a timely fashion, then your best course of action is to debug it yourself and ideally share back the fix you found. I understand this can be frustrating, especially for people who are less experienced. But there really is nothing else we can do. For the record we are only 2 "active" people on this project and only in our spare time. I'll do my best to merge incoming PRs, but I personally don't have enough time to spend a whole day debugging a single issue. I suggest you guys, @rf1234 and @JParkerCFI work together to resolve this particular issue. |
Hello, in which branch can I commit fixes? |
Pull requests! "First time contributing to PHPOffice/PhpSpreadsheet? You can read this repository’s contributing guidelines or GitHub Help to learn how to open a good pull request." |
Pull requests should be made against |
This is:
What is the expected behavior?
In version 1.2.1 there was no issue with print options: I don't set any and the template I use is issue checked so that there shouldn't be any problems.
What is the current behavior?
In version 1.3.1 I get an issue with Print Options. Excel repairs the file and removes print options.
What are the steps to reproduce?
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:
Which versions of PhpSpreadsheet and PHP are affected?
The text was updated successfully, but these errors were encountered: