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

llevvi's avatar

How to properly format PDF using Laravel Excel?

Hey you all,

I am using Laravel Excel on my platform for a lot of tasks. Now I am working on exporting some data in XLS and in PDF. THE XLS is exported fine, the PDF though looks very bad: it does show all columns and one of the columns takes a lot of space (width). It spans out of the page edge. How can I make the whole content fit in the page (A4 landscape)?

(I am using dompdf)

Here's the code:

public function export($id,$format)
    {
       $comparison = Comparison::where('label_id',$id)->get();
        try {
                $document = Excel::create('Comparison',function($excel) use ($comparison){
                $excel->setTitle('Test - Comparison');
                $excel->setCreator(Auth::User()->email)
                ->setCompany('teste');
               $excel->setDescription('Comparison created using Test plataform');

               $excel->sheet('Compared', function ($sheet) use ($comparison){
                $sheet->fromModel($comparison, null,"A1",true);
                $sheet->setOrientation('landscape');
                $sheet->setScale(5);
                $sheet->setAllBorders('thin');

               });

          })->download($format);

      }catch(\Excel $e){
        return redirect()->back()->with('error','Failed to generate document');
      }
    }

Here an snapshot of what the PDF looks like: http://imgur.com/a/BrcK1

0 likes
2 replies

Please or to participate in this conversation.