I am merging pdfs . These files could be more than 100 . Now problem is my server only has 1 GB RAM
. Server hangs when these merging starts actually this process is working in Job. It works fine on my localhost which has 4 GB of RAM . First thing I want to ask is how I can optmize this process so memory didn't use at once?
May be I am doing something wrong in logic . Please Identify that If any
Here is code which is written inside Job handle()
function
$pdfMerger = new PdfMerger();
$bulk_pdf = public_path('bulk-pdf-consignment/manifest' . $this->manifest_id . '_.pdf');
$allFilesPath = public_path('bulk-pdf-consignment/manifest_' . $this->manifest_id);
// if (File::exists($bulk_pdf)) {
// $pdfMerger->addPDF($bulk_pdf, 'all');
// }
$filesInFolder = collect(File::files($allFilesPath))->sortBy(function ($file) {
return $file->getCTime();
});
$arr = [];
$filesCount = 0;
foreach ($filesInFolder as $path) {
$file = pathinfo($path);
$finalPath = $file['dirname'] . '/' . $file['basename'];
$added = $pdfMerger->addPDF($finalPath, 'all');
if ($added) {
array_push($arr, $finalPath);
}
$filesCount++;
}
$merged = $pdfMerger->merge("file", $bulk_pdf);
if ($merged) {
$url = Storage::disk('s3')->url('bulk_pdf_consignments/manifest' . $this->manifest_id . '_.pdf', fopen($bulk_pdf, 'r+'));
Notification_Helper::sendNotification($this->user_id, 'Download Bulk Print Label PDF'.$filesCount, $url,$this->user_type);
if ($this->user_type == 'staff') {
$user = Staff::find($this->user_id);
}
if ($this->user_type == 'customer') {
$user = Contact::find($this->user_id);
}
sendEmail($user->email, 'Manifest Consignment Labels PDF ', $url, $attachment = null);
File::delete($bulk_pdf);
File::deleteDirectory($allFilesPath);
foreach ($arr as $file) {
File::delete($file);
}
}
My second Question is can I test on localhost by reducing my RAM to 1GB ?