PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.28k stars 3.43k forks source link

Border on merged cells #3557

Closed lokomass closed 3 months ago

lokomass commented 1 year ago

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');

image

I can't set a border to merged cells, thanks for help

oleibman commented 10 months ago

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.

oleibman commented 3 months ago

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);