Skip to content

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

Closed
@stevenbuehner

Description

@stevenbuehner

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions