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

Davidgo's avatar

Date mising from exported PDF

Hello, I am trying to export some pdfs with having date in top right corner but they are not there at all I followed the documentation but I cant seem to get hang of it, any ideas ? I am using the laravel-excel package from Maatwebsite


namespace App\Exports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;

class UsersExport implements FromCollection, WithEvents
{
    use Exportable;
    
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        //map to return only user names
        return User::query()->get()->map(function ($user) {
            return [
                $user->name,
            ];
        });
    }
    
    /**
     * @return array
     */
    public function registerEvents(): array
    {
        $header = 'Please treat this document as confidential! and do not share with anyone';
        
        return [
            BeforeExport::class  => function(BeforeExport $event) {
                $event->writer->getDelegate()->getProperties()->setCreator('Product');
            },
            AfterSheet::class => function(AfterSheet $event) use ($header) {
                $event->sheet->getPageSetup()->setHorizontalCentered(true);
                $event->sheet->getPageSetup()->setVerticalCentered(true);
                $event->sheet->getDefaultRowDimension()->setRowHeight(-1);
                $event->sheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
                $event->sheet->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4);
                
                // Set header
                $event->sheet->getHeaderFooter()->setOddHeader('&C&B'.$header);
            },
        ];
    }
}

That is my code

0 likes
0 replies

Please or to participate in this conversation.