Khrisna Gunanasurya
Khrisna Gunanasurya

Reputation: 745

PHPSpreadsheet cannot set worksheet margin

Hi I have a little problem with this plugin, this is the code that makes an error

Sheet::macro('setPageMargins', function (Sheet $sheet, float $top = 1, float $right = 0.75, float $bottom = 1, float $left = 0.75) {
    $sheet->getDelegate()->getPageMargins()->setTop($top);
    $sheet->getDelegate()->getPageMargins()->setRight($right);
    $sheet->getDelegate()->getPageMargins()->setLeft($left);
    $sheet->getDelegate()->getPageMargins()->setBottom($bottom);
});

Here is the error

Argument 1 passed to PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::setPageMargins() must be an instance of PhpOffice\PhpSpreadsheet\Worksheet\PageMargins, float given

and I just following this documentation https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#page-margins with using maatwebsite\Excel, by the way i'm using Laravel 5.7.

The question is how to set the margin? because I think the documentation is wrong, please help

Upvotes: 4

Views: 2574

Answers (1)

Flame
Flame

Reputation: 7618

I believe you should be doing:

// Create a page margins object.
$pageMargins = new \PhpOffice\PhpSpreadsheet\Worksheet\PageMargins();
$pageMargins->setTop(1);
$pageMargins->setRight(0.75);
$pageMargins->setBottom(1);
$pageMargins->setLeft(0.75);

Sheet::macro('setPageMargins', function($sheet, $pageMargins));

According to the code, it says public function setPageMargins(PageMargins $pValue).

(https://github.com/PHPOffice/PhpSpreadsheet/blob/0e8fde9be6d3918ffe030bed0ba0297bfe330c4d/src/PhpSpreadsheet/Worksheet/Worksheet.php#L966)

Your ::macro calls the setPageMargins function, although this pattern confuses me (it appears to be some Maatwebsite\Excel thing).

Upvotes: 2

Related Questions