Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CliffordJames's avatar

Laravel Excel export multiple sheets to xlsx and pdf (landscape)

Hi,

I've got a Laravel Excel export with multiple sheets which I need to export as a xlsx and pdf, but the pdf needs to be in landscape orientation. Does anyone know how to do this on an export with multiple sheets?

Simplified code:

<?php

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class StatementExport implements WithMultipleSheets
{
    use Exportable;

    /**
     * @return array
     */
    public function sheets(): array
    {
        $sheets = [
            new OverviewSheet($this->statements)
        ];

        $this->statements->each(function ($statement) use (&$sheets) {
            $sheets[] = new StatementSheet($statement);
        });

        return $sheets;
    }
}

<?php

class DownloadController
{
    public function __invoke()
    {
        $formats = ['xlsx', 'pdf'];

        $export = new StatementExport($statements);

        foreach ($this->formats as $format) {
            $files[] = $this->store($export, $format);
        }

        Auth::user()->notify(new Download($files));
    }

    protected function store(ComposerStatementExport $export, $format)
    {
        $name = "statement.{$format}";

        switch ($format) {
            case 'xlsx':
                $type = Excel::XLSX;
                break;

            case 'pdf':
                $type = Excel::DOMPDF;
                break;

            default:
                // TODO: Exception
                break;
        }

        $export->store($name, null, $type);

        return Storage::path($name);
    }
}
0 likes
13 replies
CliffordJames's avatar

Yeah I've tried the following in the export class:

    /**
     * @param \Maatwebsite\Excel\Events\AfterSheet $event
     */
    public static function afterSheet(AfterSheet $event)
    {
        $event->sheet->getDelegate()->getPageSetup()
            ->setOrientation(PageSetup::ORIENTATION_LANDSCAPE)
            ->setPaperSize(PageSetup::PAPERSIZE_A4);
    }
CliffordJames's avatar

Turns out that the afterSheet event is not being triggerd, anyone got ideas why not?

CliffordJames's avatar

Updated simplified code:

<?php

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;

class ComposerStatementExport implements WithEvents, WithMultipleSheets
{
    use Exportable, RegistersEventListeners;

    /**
     * @return array
     */
    public function sheets(): array
    {
        $sheets = [
            new OverviewSheet($this->statements)
        ];

        $this->statements->each(function ($statement) use (&$sheets) {
            $sheets[] = new StatementSheet($statement);
        });

        return $sheets;
    }

    /**
     * @param \Maatwebsite\Excel\Events\AfterSheet $event
     */
    public static function afterSheet(AfterSheet $event)
    {
        $event->sheet->getDelegate()->getPageSetup()
            ->setOrientation(PageSetup::ORIENTATION_LANDSCAPE)
            ->setPaperSize(PageSetup::PAPERSIZE_A4);
    }
}

<?php

class DownloadController
{
    public function __invoke()
    {
        $formats = ['xlsx', 'pdf'];

        $export = new StatementExport($statements);

        foreach ($this->formats as $format) {
            $files[] = $this->store($export, $format);
        }

        Auth::user()->notify(new Download($files));
    }

    protected function store(ComposerStatementExport $export, $format)
    {
        $name = "statement.{$format}";

        switch ($format) {
            case 'xlsx':
                $type = Excel::XLSX;
                break;

            case 'pdf':
                $type = Excel::DOMPDF;
                break;

            default:
                // TODO: Exception
                break;
        }

        $export->store($name, null, $type);

        return Storage::path($name);
    }
}
cookie_good's avatar

So it's saving your pdf but it's not orienting it as landscape? And there's no error message?

cookie_good's avatar

The first thing I would do is this

dd ($event);

Within your after sheet method.

cookie_good's avatar

Also, when I use PHP office it usually requires me to save the workbook, but I don't know about this.

CliffordJames's avatar

Yes, no errors. Also the dd is not showing in the event, so I think the problem could be with the event not being fired...

cookie_good's avatar

I would try putting this somewhere, just to test:

$this->afterSheet($param);

CliffordJames's avatar

Found the issue: there is no afterSheet on multipleSheets. I moved the events to the single sheets and now it works.

Thanks!

Please or to participate in this conversation.