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

PDFWriter produces incorrectly merged cells as soon as some columns are setVisible(false) #4319

Closed
1 of 10 tasks
stevenbuehner opened this issue Jan 14, 2025 · 2 comments · Fixed by #4320
Closed
1 of 10 tasks

Comments

@stevenbuehner
Copy link

This problem was introduced after Version 2.0. I realized it, when updating to "phpoffice/phpspreadsheet": "3.8.0"

This is a bug report

What is the expected behavior?

When a column is setVisible(false) I expect the PDF-Writer to still display merged cells correctly.

What is the current behavior?

When some colums in the spreadsheet are hidden, the phpWriter is not able to correctly format merged cells anymore.

  • Boxing does not work anymore
  • Color-Styles (like bold) are not assigned corretly anymore

Example

This is a screenshot of the generated PDF with column (A) set to visible:

$worksheet->getColumnDimension("A")->setVisible(TRUE); 

image


This is how the same spreadsheet is generated by the PDF-writer, when column A is set to hidden:

$worksheet->getColumnDimension("A")->setVisible(FALSE); 

image


What are the steps to reproduce?

  • Install with Composer "phpoffice/phpspreadsheet": "3.8.0" and "mpdf/mpdf": "^8.2"
  • store the following file as "test.php" and execute it with php test.php
<?php
// filename: test.php

require_once __DIR__ . "vendor/autoload.php";

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();

$worksheet = $spreadsheet->getActiveSheet();

// just some labels for better visualisation of the problem
$worksheet->setCellValue("A1", "A");
$worksheet->setCellValue("B1", "B");
$worksheet->setCellValue("C1", "C");

// setting the row height to better visualize the problem
for ($i = 1; $i <= 10; $i++) {
	$worksheet->getRowDimension($i)->setRowHeight(17);
}

// Headline - merged over two cells AND two rows
$worksheet->mergeCells("B2:C3");
$worksheet->setCellValue("B2", "Hello World Headline");
$worksheet->getStyle("B2:C3")->getFont()->setBold(TRUE);
$worksheet->getStyle("B2:C3")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$worksheet->getStyle("B2:C3")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN)->setColor(new Color(Color::COLOR_BLACK));

// Content 1 - merge over two rows
$worksheet->mergeCells("B4:B5");
$worksheet->mergeCells("C4:C5");
$worksheet->setCellValue("B4", "Label 1");
$worksheet->setCellValue("C4", "Text 1");
$worksheet->getStyle("B4:B5")->getFont()->setBold(TRUE);
$worksheet->getStyle("B4:C5")->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
$worksheet->getStyle("B4:B5")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN)->setColor(new Color(Color::COLOR_BLACK));
$worksheet->getStyle("C4:C5")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN)->setColor(new Color(Color::COLOR_BLACK));

// Content 2 - merge over two rows
$worksheet->mergeCells("B6:B7");
$worksheet->mergeCells("C6:C7");
$worksheet->setCellValue("B6", "Label 2");
$worksheet->setCellValue("C6", "Text 2");
$worksheet->getStyle("B6:B7")->getFont()->setBold(TRUE);
$worksheet->getStyle("B6:C7")->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
$worksheet->getStyle("B6:B7")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN)->setColor(new Color(Color::COLOR_BLACK));
$worksheet->getStyle("C6:C7")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN)->setColor(new Color(Color::COLOR_BLACK));


// This is where the error is introduced (!!!)
// Uncomment next line to produce correct results
$worksheet->getColumnDimension("A")->setVisible(FALSE);

// Generate PDF
$pdfWriter = new Mpdf($spreadsheet);
$filename  = 'filename_test';
$pdfWriter->writeAllSheets();
$pdfWriter->save($filename . '.pdf');

// Optional: Generate XLSY to compare
$xlsxWriter = new Xlsx($spreadsheet);
$xlsxWriter->save($filename . '.xlsx');
  • I narrowed it down to this setup to reproduce the error

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

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

I don't know.

Which versions of PhpSpreadsheet and PHP are affected?

  • This error does not exist in "phpoffice/phpspreadsheet": "2.0" ...
  • I tested multiple versions when upgrading and found the error in all the later versions
@stevenbuehner stevenbuehner changed the title PDFWriter produces nonsense as soon as some Columns are setVisible(false) PDFWriter produces incorrectly merged cells as soon as some columns are setVisible(false) Jan 14, 2025
@oleibman
Copy link
Collaborator

Probably a result of PR #3945. Your output isn't exactly correct for PhpSpreadsheet V1 either - the hidden column is part of the output there, which is the problem the PR addressed. I will try to figure out why unhidden merged cells are affected.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jan 15, 2025
Fix PHPOffice#4319. PR PHPOffice#3495 allowed Mpdf and Tcpdf to handle hidden rows and columns. The handling is a little off when merged cells are part of the output. The fix for Mpdf is pretty straightforward. Tcpdf is a little more convoluted. Dompdf and Html do not require a change. Unit tests are added for the same scenario for all of Mpdf, Tcpdf, Dompdf, and Html.
@stevenbuehner
Copy link
Author

Awesome work. Thank you @oleibman. #4320 solved all issues! 💪

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.

2 participants