From 914a580bb14cc564b87862b01ab4127ef64094d3 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Mon, 13 Feb 2023 12:47:21 +0530 Subject: [PATCH] pdfMerger Implementation : ps --- api/application/config/routes.php | 1 + api/application/controllers/PD_Controller.php | 170 ++++++++++++++---- 2 files changed, 134 insertions(+), 37 deletions(-) diff --git a/api/application/config/routes.php b/api/application/config/routes.php index 9f9d58e8..0d8b16fd 100755 --- a/api/application/config/routes.php +++ b/api/application/config/routes.php @@ -427,4 +427,5 @@ $route['transferCreditPDToCETApp'] = 'SMC_Credit_PD_Controller/transferCreditPDT $route['newSalesPd'] = 'Sales_PD_Controller/cetNewSalesPd'; $route['login'] = 'Common_Masters_Controller/login'; +$route['pdfMerger'] = 'PD_Controller/pdfMerger'; diff --git a/api/application/controllers/PD_Controller.php b/api/application/controllers/PD_Controller.php index 2fd4cbdb..f28dea78 100644 --- a/api/application/controllers/PD_Controller.php +++ b/api/application/controllers/PD_Controller.php @@ -4513,37 +4513,12 @@ class PD_Controller extends REST_Controller // die(); //PDF Gen using DOM PDF $pdf_doc = $this->pdfgenerator->generate($temp, $filename = 'version', $stream = false, $paper = 'A4', $orientation = "portrait"); - $details = $this->PD_Model->getDocsDetails($pdid); - if(!empty($details)){ - for($i=0;$iaws_s3->getSingleObjectInaBucketAsSignedURI($bucketname, $dockey, '+30 minutes'); - $src_local_file = XLPATH.'/tmp/'.$details[$i]['pd_document_name']; - $ifp = fopen( $src_local_file, 'wb' ); - fwrite( $ifp, $doc_singed_uri ); - fclose( $ifp ); - chmod($src_local_file, 0777); - $docsdetails[] = $details[$i]['pd_document_name']; - } - } - // ALTER TABLE `sineedgedemo`.`t_pd_documents` - // ADD COLUMN `docs_merge` TINYINT(4) NULL DEFAULT '0' AFTER `content_type`; //End of PDF Gen using DOM PDF $output_file2 = $this->external_path . "/pd_reports/" . $pdid . "/sample.pdf"; $bytes = file_put_contents($output_file2, $pdf_doc); // echo $output_file2; // die(); if (file_put_contents($output_file2, $pdf_doc)) { - if(!empty($docsdetails)){ - $pdf = new \Jurosh\PDFMerge\PDFMerger; - // $pdf = new PDFMerger; - $pdf->addPDF($output_file2, 'all'); - foreach($docsdetails as $doc) - { - $pdf->addPDF(XLPATH.'/tmp/'.$doc, 'all'); - } - $pdf->merge('file', $output_file2); - } $sourcefile = $output_file2; // print_r($sourcefile);die(); $bucket_data = array('bucket_name' => $bucketname, 'key' => $newkey, 'sourcefile' => $sourcefile); @@ -4571,11 +4546,6 @@ class PD_Controller extends REST_Controller $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; $data['records'] = $singed_uri; - foreach($docsdetails as $doc) - { - $src_local_file = XLPATH.'/tmp/'.$doc; - unlink($src_local_file); - } //$data['records'] = array('pdf_url' => $singed_uri,'zip_url' => $zip_file_uri); $this->response($data, REST_Controller::HTTP_OK); } else { @@ -4626,7 +4596,7 @@ class PD_Controller extends REST_Controller $pdid = $records['pd_id']; $fk_createdby = $records['fk_createdby']; $random_string = $records['random_string']; - $doc_id = $records['digilocker_merge']; + // $doc_id = $records['digilocker_merge']; if (!file_exists($this->external_path . '/pd_reports/' . $pdid)) { mkdir($this->external_path . '/pd_reports/' . $pdid, 0777); } @@ -4803,12 +4773,6 @@ class PD_Controller extends REST_Controller if ($is_write_done) { //$update = $this->PD_Model->updateRecords(array('pd_report_html_filename' => $pd_report_time),PDTRIGGER,array('pd_id' => $pdid)); - if(!empty($records['digilocker_merge'])){ - foreach ($doc_id as $id) { - $filtered_arr[] = $id['pd_document_id']; - } - if(!empty($filtered_arr))$this->PD_Model->updpddocs($filtered_arr); - } $where_condition_array = array("pd_id" => $pdid); $temp_array = array('is_original_report_created' => 1, 'pd_report_html_filename' => $pd_report_time, 'pd_report_generated_date' => date('Y-m-d H:i:s'), 'is_qc_edited' => 2); @@ -10930,5 +10894,137 @@ class PD_Controller extends REST_Controller } } + public function pdfMerger_post(){ + $records = $this->post('records'); + if(!empty($records)){ + $pdid = $records[0]['fk_pd_id'];// $pdid = 3444; + $random_string = $this->PD_Model->selectCustomRecords(array('random_string'),array('pd_id' => $pdid),PDTRIGGER); + $random_string = $random_string[0]['random_string']; + + log_message('ERROR', "### PDF merger pdId : ".$pdid." randomString : ".$random_string); + $pd_master_details = $this->PD_Model->getPDMasterDetails($pdid, $random_string); + if($pd_master_details[0]['pd_status'] == "COMPLETED"){ + $bucketname = LENDER_BUCKET_NAME_PREFIX . $pd_master_details[0]['fk_lender_id']; + $main_applicant_name = $pd_master_details[0]['main_applicant']; + $pdf_name = 'PD_Report_' . $main_applicant_name; + + $key = 'pd' . $pdid . '/pd_reports/' . $main_applicant_name . '.pdf'; + $newkey = 'pd' . $pdid . '/pd_reports/' . $pdf_name . '.pdf'; + + // $records = $this->PD_Model->getDocsDetails($pdid); + + $_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucketname, $key, '30 minutes'); + + $headers = @get_headers($_uri); + if ($headers && strpos($headers[0], '200')) { + $is_writed = file_put_contents(XLPATH.'/tmp/existingReport.pdf',file_get_contents($_uri)); + } else { + $singed_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucketname, $newkey, '30 minutes'); + $is_writed = file_put_contents(XLPATH.'/tmp/existingReport.pdf',file_get_contents($singed_uri)); + } + + + for($i=0;$iPD_Model->updpddocs($filtered_arr); + if(!empty($docsdetails)){ + $pdf = new \Jurosh\PDFMerge\PDFMerger; + $pdf->addPDF(XLPATH.'/tmp/existingReport.pdf', 'all'); + foreach($docsdetails as $doc) + { + $pdf->addPDF(XLPATH.'/tmp/'.$doc, 'all'); + } + $pdf->merge('file', XLPATH.'/tmp/newReport.pdf'); + } + $sourcefile = XLPATH.'/tmp/newReport.pdf'; + $bucket_data = array('bucket_name' => $bucketname, 'key' => $newkey, 'sourcefile' => $sourcefile); + $s3result = $this->aws_s3->uploadFileToS3Bucket($bucket_data); + //print_r($s3result); + if (is_object($s3result) && $s3result['ObjectURL'] != '' && $s3result['@metadata']['statusCode'] == 200) { + $data['dataStatus'] = true; + $data['status'] = REST_Controller::HTTP_OK; + $data['msg'] = 'Mergered Successfully'; + } else { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NOT_FOUND; + $data['msg'] = 'Something Went Wrong! Try Later'; + } + foreach($docsdetails as $doc){ + $src_local_file = XLPATH.'/tmp/'.$doc; + unlink($src_local_file); + } + unlink($sourcefile); + $this->response($data, REST_Controller::HTTP_OK); + }else{ + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NO_CONTENT; + $data['msg'] = 'This PD is Not in `COMPLETED` Status'; + $this->response($data, REST_Controller::HTTP_OK); + } + }else { + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NO_CONTENT; + $data['msg'] = 'Document Details Not available!'; + $this->response($data, REST_Controller::HTTP_OK); + } + } + // public function testPDFMerger_post(){ + // // $files = array(APPPATH.'view/a1.pdf', APPPATH.'view/a2.pdf'); + // // $mpdf->Output(APPPATH.'view/a3.pdf', 'D'); + // // $pdf = new \Jurosh\PDFMerge\PDFMerger; + // // $pdf->addPDF(APPPATH.'views/a1.pdf', 'all'); + // // $pdf->addPDF(APPPATH.'views/a2.pdf', 'all'); + // // $pdf->merge('file', APPPATH.'views/a6.pdf');3407;3414;//, + // $pdid = 3444; + // // echo "pdid".$pdid;die(); + // log_message('ERROR', "### PDF merger pdid".$pdid); + // $random_string = $this->PD_Model->selectCustomRecords(array('random_string'),array('pd_id' => $pdid),PDTRIGGER); + // $random_string = $random_string[0]['random_string']; + // $pd_master_details = $this->PD_Model->getPDMasterDetails($pdid, $random_string); + // $bucketname = LENDER_BUCKET_NAME_PREFIX . $pd_master_details[0]['fk_lender_id']; + // $main_applicant_name = $pd_master_details[0]['main_applicant']; + // $pdf_name = 'PD_Report_' . $main_applicant_name; + + // $key = 'pd' . $pdid . '/pd_reports/' . $main_applicant_name . '.pdf'; + // $newkey = 'pd' . $pdid . '/pd_reports/' . $pdf_name . '.pdf'; + + // $details = $this->PD_Model->getDocsDetails($pdid); + + // $_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucketname, $key, '30 minutes'); + + // $headers = @get_headers($_uri); + // if ($headers && strpos($headers[0], '200')) { + // $is_writed = file_put_contents(XLPATH.'/dump/existingReport.pdf',file_get_contents($_uri)); + // } else { + // $singed_uri = $this->aws_s3->getSingleObjectInaBucketAsSignedURI($bucketname, $newkey, '30 minutes'); + // $is_writed = file_put_contents(XLPATH.'/dump/existingReport.pdf',file_get_contents($singed_uri)); + // } + + // if(!empty($details)){ + // for($i=0;$iaws_s3->getSingleObjectInaBucketAsSignedURI($bucketname, $dockey, '+30 minutes'); + // $src_local_file = XLPATH.'/dump/'.$details[$i]['pd_document_name']; + // $is_writed = file_put_contents($src_local_file,file_get_contents($doc_singed_uri)); + // $docsdetails[] = $details[$i]['pd_document_name']; + // } + // } + + // if(!empty($docsdetails)){ + // $pdf = new \Jurosh\PDFMerge\PDFMerger; + // $pdf->addPDF(XLPATH.'/dump/existingReport.pdf', 'all'); + // foreach($docsdetails as $doc) + // { + // // $pdf->addPDF(XLPATH.'/dump/'.$doc, 'all'); + // $pdf->addPDF(XLPATH.'/dump/'.$doc, 'all'); + // } + // $pdf->merge('file', XLPATH.'/dump/newReport.pdf'); + // } + // } + // ALTER TABLE `sineedgedemo`.`t_pd_documents` + // ADD COLUMN `docs_merge` TINYINT(4) NULL DEFAULT '0' AFTER `content_type`; }