Debug School

rakesh kumar
rakesh kumar

Posted on

how to convert Excelto pdf in laravel

why we use IOFactory::createWriter?
To convert an Excel file to a PDF in Laravel, you can use a package called "phpoffice/phpspreadsheet". This package provides a simple way to read and write spreadsheet files in various formats, including Excel and PDF.

Here are the steps to convert an Excel file to PDF in Laravel using the "phpoffice/phpspreadsheet" package:

Step 1: Install the package using Composer

composer require phpoffice/phpspreadsheet
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a controller and add the following code to it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;

class ExcelToPdfController extends Controller
{
    public function convert(Request $request)
    {
        // Get the file path from the request
        $filePath = $request->file('file')->getPathName();

        // Load the Excel file
        $spreadsheet = IOFactory::load($filePath);

        // Create a writer to output the PDF file
        $writer = IOFactory::createWriter($spreadsheet, 'Mpdf');

        // Set the output file name and path
        $outputFileName = 'output.pdf';
        $outputFilePath = storage_path('app/public/' . $outputFileName);

        // Write the PDF file to the output path
        $writer->save($outputFilePath);

        // Return the file download response
        return response()->download($outputFilePath);
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Step 3: Create a route for the controller method

Route::post('/convert', 'ExcelToPdfController@convert');
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a form in your view to submit the Excel file

<form method="POST" action="/convert" enctype="multipart/form-data">
    @csrf
    <input type="file" name="file">
    <button type="submit">Convert to PDF</button>
</form>
Enter fullscreen mode Exit fullscreen mode

why we use IOFactory::createReader with example while convert an Excel file to a PDF

In Laravel, when converting an Excel file to a PDF, we can use the PHPExcel library to read the data from the Excel file and then use a PDF library, such as Dompdf, to generate the PDF. The IOFactory::createReader method is used to create a reader object for reading the data from the Excel file.

Here is an example of how to convert an Excel file to a PDF in Laravel using the PHPExcel library and Dompdf:

use PHPExcel_IOFactory;
use Dompdf\Dompdf;

// Load the Excel file
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$excel = $reader->load('/path/to/excel/file.xlsx');

// Get the active sheet
$sheet = $excel->getActiveSheet();

// Get the data from the sheet
$data = $sheet->toArray();

// Create a HTML table from the data
$table = '<table>';
foreach ($data as $row) {
    $table .= '<tr>';
    foreach ($row as $cell) {
        $table .= '<td>' . $cell . '</td>';
    }
    $table .= '</tr>';
}
$table .= '</table>';

// Create a PDF
$dompdf = new Dompdf();
$dompdf->loadHtml($table);
$dompdf->render();

// Output the PDF
$dompdf->stream('output.pdf');
Enter fullscreen mode Exit fullscreen mode

Image description

Image description
In Laravel, we use the IOFactory class from the PHPExcel library to create reader and writer objects for reading and writing Excel files. The IOFactory::createReader method is used to create a reader object, which is used to read data from an Excel file. The IOFactory::createWriter method is used to create a writer object, which is used to write data to an Excel file.

In the example above, we first create a reader object using the IOFactory::createReader method and load the Excel file using the reader's load method. We then get the active sheet from the Excel file and read the data from the sheet into an array.

Next, we create an HTML table from the data and use the Dompdf library to generate a PDF from the HTML. Finally, we output the PDF to the browser using the stream method.

Top comments (0)