Skip to content

Instantly share code, notes, and snippets.

@tuckbloor
Created November 28, 2016 14:32
Show Gist options
  • Select an option

  • Save tuckbloor/effd74b794175522f8881ca68a609f17 to your computer and use it in GitHub Desktop.

Select an option

Save tuckbloor/effd74b794175522f8881ca68a609f17 to your computer and use it in GitHub Desktop.
muhimbi
<?php
namespace App\Http\Repositories;
ini_set('max_execution_time', 0);
use App\Http\Interfaces\GenerateInterface;
use App\Muhimbi\PdfConverterService;
use PHPExcel_IOFactory;
class GenerateRepository implements GenerateInterface
{
private $muhimbi;
public function __construct(PdfConverterService $muhimbi){
$this->muhimbi = $muhimbi;
}
/**
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Reader_Exception
*/
public function generate()
{
$target_path = public_path() . '/excel_templates/test_1_sheet_only.xlsx';
$inputFile = $target_path;
try {
$inputFileType = \PHPExcel_IOFactory::identify($inputFile);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFile);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFile, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);//change sheet number
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$row_keys = ["0" => "DXF Item No.",
"1" => "Piece Number",
"2" => "Total Qty",
"3" => "Plate PO Item No.",
"4" => "Plate Sequence",
"5" => "Plate Boat Side",
"6" => "Assigned Boat Side",
"7" => "From Plate Master UID",
"8" => "T",
"9" => "W",
"10" => "L",
"11" => "Build Sequence",
"12" => "COMPONENT CODE",
"13" => "Dwg No",
"14" => "Spec",
"15" => "Description",
"16" => "FDIM1",
"17" => "FDIM2",
"18" => "FDIM3",
"19" => "Remarks",
"20" => "Routing (Cut, Bend, M/C, Procure)",
"21" => "Reccie?"];
//load the excel template here so its loaded only once for all the mitps;
$target_path = public_path() . '/excel_templates/MITP-PLATE-GI-GE-PORT-TEST.xlsx';
$inputFile = $target_path;
try {
$inputFileType = \PHPExcel_IOFactory::identify($inputFile);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFile);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFile, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
//the data for each row
$data = [];
// Loop through each row of the worksheet in turn
for ($row = 2; $row <= $highestRow; $row++) {
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
$i = 0;
foreach ($row_keys as $key) {
foreach ($rowData as $keys => $value) {
$data[$key] = $value[$i];
}
$i++;
}
$this->createExcel($data, $objPHPExcel, $row, $i, $highestRow);
$data = [];
}
}
/**
* @param $data
* @param $objPHPExcel
* @param $row
* @param $i
* @throws \PHPExcel_Reader_Exception
*/
public function createExcel($data, $objPHPExcel, $row, $i, $highestRow) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('AA11', $data['Dwg No']);
$objPHPExcel->setActiveSheetIndex(1)
->setCellValue('Y11', $data['FDIM1']);
echo 'Creating Excel MITP_' . $row .'.xlsx<br>';
ob_flush();
flush();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$path = public_path() . '/excel_templates/completed_excel_files/MITP_' . $row .'.xlsx';
$objWriter->save($path);
if($row == $highestRow) {
$this->createPdf();
}
}
/**
* @throws \App\Muhimbi\Exception
*/
public function createPdf() {
//get all excel files in a directory
$files = glob(public_path() . '/excel_templates/completed_excel_files/*.xlsx');
//loop through all the files
foreach($files as $file) {
$parts = pathinfo($file);;
$filename = 'excel_templates/completed_excel_files/' . $parts['filename'];
$extension = $parts['extension'];
echo 'Generating ' . $parts['filename'] . 'pdf<br>';
ob_flush();
flush();
$this->muhimbi->convertToFile($filename, $extension);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment