How to convert excel to pdf using php

How to convert excel to pdf in php

To convert the excel file to pdf we have to use fpdf and php office which is open source library of php. 

 

Import the php office 


use \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
require_once ('../vendor/autoload.php');
$error  = array();
if (isset($_POST["import"])) {

    $allowedFileType = [
        'application/vnd.ms-excel',
        'text/xls',
        'text/xlsx',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    ];

    if (in_array($_FILES["file"]["type"], $allowedFileType)) {

        $targetPath = '../uploads/' . $_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);

        $Reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

        $spreadSheet = $Reader->load($targetPath);
        $excelSheet = $spreadSheet->getActiveSheet();
        $spreadSheetAry = $excelSheet->toArray();
        $sheetCount = count($spreadSheetAry);

Then store the data of excel in variable.

for ($i = 0; $i <= $sheetCount; $i++) {

            if (isset($spreadSheetAry[$i][0])) {
                $code = mysqli_real_escape_string($db, $spreadSheetAry[$i][0]);
            }
            if (isset($spreadSheetAry[$i][1])) {
                $name = mysqli_real_escape_string($db, $spreadSheetAry[$i][1]);
            }

 

 

Here is code of one project in which excel to pdf conversion is given for better understanding 

https://github.com/ronilcajan/pdf-project

 

I will also add another more easy tutorial on excel uploading using php and then export it as pdf. Please check our latest php course on 

 https://courses.collegespike.com/content/course-php 

 

Thank you for reading my course ❤️


Please Share this course

It Looks like you have completed your course!! We have created Online series based on the above content. You can give a test and check your skill.




Be a Part of CollegeSpike by giving your precious comment here and help us in improving our content.