Laravel EXCEL and PDF export

Abrar Jahin picture Abrar Jahin · Sep 18, 2015 · Viewed 11.3k times · Source

I am new in Laravel and I am using Laravel 4.2

I like to export some data in PDF and excel.

Is there any way in Laravel?

Answer

Peyman.H picture Peyman.H · Sep 18, 2015

Use FPDF to do what u need. You must create a pdf file from scratch and fill it in the way u want.

<?php
require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();    // add page to PDF
$pdf->SetFont('Arial','B',16);    // Choose a font and size
$pdf->Cell(40,10,'Hello World!');  // write anything to any line you want
$pdf->Output("your_name.pdf");   // Export the file and send in to browser       
?>

And for Excel a simple way is to add PHPExcel to laravel. Add this line to your composer.json:

"require": {
    "phpexcel/phpexcel": "dev-master"
}

then run a composer update. So use it like this:

$ea = new PHPExcel();

$ea->getProperties()
   ->setCreator('somebody')
   ->setTitle('PHPExcel Demo')
   ->setLastModifiedBy('soembody')
   ->setDescription('A demo to show how to use PHPExcel to manipulate an Excel file')
   ->setSubject('PHP Excel manipulation')
   ->setKeywords('excel php office phpexcel')
   ->setCategory('programming')
   ;

$ews = $ea->getSheet(0);
$ews->setTitle('Data');

$ews->setCellValue('a1', 'ID'); // Sets cell 'a1' to value 'ID 
$ews->setCellValue('b1', 'Season');