PhpSpreadsheet
PhpSpreadsheet copied to clipboard
Border on merged cells
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?
An outline border around merged cells
What is the current behavior?
NO border all around
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:
<?php
require __DIR__ . '/vendor/autoload.php';
// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet = new Spreadsheet();
$target = 'A2:Z3';
$spreadsheet -> getActiveSheet() -> mergeCells($target);
$spreadsheet -> setActiveSheetIndex(0) -> setCellValue('A2', 'Planning');
$spreadsheet -> getActiveSheet() -> getStyle($target) -> applyFromArray([
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_HAIR,
'color' => [
'rgb' => '000000'
]
]
]
]);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="01simple.pdf"');
header('Cache-Control: max-age=0');
IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
$writer = IOFactory::createWriter($spreadsheet, 'Pdf');
$writer->save('php://output');

I can't set a border to merged cells, thanks for help
Using a slight variation on your code (among other things a much smaller merge range A2:B5), I see bottom and right borders, but not left nor top. PhpSpreadsheet establishes the border attributes for merged cells in Html (and therefore Pdf) using the css !important attribute. Mpdf does not appear to support that attribute, which is the reason for your problem. If you were to generate your output as Html, or as Dompdf, you would see the border.
Although this is an Mpdf problem, code similar to the following within your application will work around that problem and get the desired result almost all the time:
function mpdfborders(string $html): string
{
return preg_replace('/border-(top|bottom|right|left):none #000000;/', '', $html) ?? $html;
}
...
$writer = new Mpdf($spreadsheet);
$callback = 'mpdfborders';
$writer->setEditHtmlCallback($callback);
$writer->save($filename);