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

Border on merged cells #3557

Closed
lokomass opened this issue May 6, 2023 · 2 comments · Fixed by #4047
Closed

Border on merged cells #3557

lokomass opened this issue May 6, 2023 · 2 comments · Fixed by #4047

Comments

@lokomass
Copy link

lokomass commented May 6, 2023

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
Copy link
Collaborator

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
Copy link
Collaborator

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

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue May 27, 2024
Fix PHPOffice#3557. Borders around merged cells are not handled correctly for Mpdf. Although a perfectly acceptable workaround is suggested in the issue, it would be better if things just worked without the workaround. Html and Dompdf work with the existing code. As it turns out, Tcpdf does not work, but for a different reason than Mpdf.

Mpdf was not working because Mpdf does not honor the `!important` attribute in Css. We can get it working almost perfectly by suppressing `border*:none`; the exception is fairly Byzantine, and I'll be glad to discuss the matter should anyone report a problem with it. At any rate, it's not working now in the exception case, so we won't be any worse off.

Tcpdf was not working because the merging of attributes happened only when `useInlineCss` was not being used, but Tcpdf does use it. Merging of border attributes is now added for useInlineCss.
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