In PHP we have many number of open source plugins available to convert EXCEL to PDF. Here im using PHPEXCEL and HTML2PDF plugins to complete my task. To convert EXCEL to PDF, First of all, we need to read the excel data and then that values write on to PDF.
Firstly, Arrange the data in required format in excel, then read the the excel data by using PHPEXCEL plugin.
click here to download PHPEXCEL plugin.
Next,
click here to download HTML2PDF plugin.
For example, the excel data is like below.
Find the Step by process for read the excel data:
Step 1: Add the plugins to project folder, then find the IOFactory.php file in plugin and include the path to required file and alse find the html2pdf.class.php file from HTML2PDF Plugin, like below.
<?php
include "PHPExcelOrg/Classes/PHPExcel/IOFactory.php";
require_once "html2pdf/html2pdf.class.php";
?>
Step 2: Save the Uploaded excel file into tempory folder and create instance for the excel file to read the data
<?php
$target_dir = "uploads/";
//folder to save the uploaded excel
$target_file = $target_dir.basename($_FILES["exampleInputFile"]["name"]);
if(isset($_POST["process"]))
{
//check the size, increase the limit accordingly
if ($_FILES["exampleInputFile"]["size"] > 2000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
else{
if(move_uploaded_file($_FILES["exampleInputFile"]["tmp_name"],$target_file))
{
$filepath=$target_file;
$fpath = basename($_FILES["exampleInputFile"]["name"]);
$uploaded_dir = $_SERVER['DOCUMENT_ROOT'].'/payslip/payslip';
$inputFileName = $uploaded_dir.'/'.$target_file;
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
. '": ' . $e->getMessage());
}
}
}
?>
Step 3: Find the highest columns and highest rows.
<?php
$sheet = $objPHPExcel->getSheet(0);
// first sheet
$highestRow = $sheet->getHighestRow();
//print 2
$highestColumn = $sheet->getHighestColumn();
//print C
?>
Step 4: read the Excel cells data.
<?php
// Loop through each row of the worksheet in turn
// leave the header of the excel so start from second row then $row=1
$cellValue = array();
for ($row =1; $row <= $highestRow; $row++)
{
for($column = 0; $column < count($columnIndex); $column++)
{
$cellValue[$i][$column] = $objPHPExcel->getActiveSheet()
->getCellByColumnAndRow($columnIndex[$column],$row)->getValue();
if($cellValue[$i][$column]==''||$cellValue[$i][$column]==NULL)
{
$cellValue[$i][$column]=0;
}
}
}
?>
Step 5 : Include the HTML2PDF plugin then Create the html data using excel values.
//if we have multiple rows then use loop
for($i =1; $row <= $highestRow; $i++){
$content='<div><table><tr><td>FirstName:</td><td>'.$cellValue[$i][1].'</td></tr>
<tr><td>LastName:</td><td>'.$cellValue[$i][2].'</td></tr>
<tr><td>Address:</td><td>'.$cellValue[$i][3].'</td></tr>
</table>
</div>';
try
{
$html2pdf = new HTML2PDF('P','A4','en',true, 'UTF-8');
$html2pdf->WriteHTML($content);
$html2pdf->Output('pdf/filename.pdf', 'F');
//give the path
}
catch(HTML2PDF_exception $e)
{
echo $e;
exit;
}
}
OUTPUT of PDF: