Skip to content

Commit b766d78

Browse files
authored
[HOTFIX][BE]: Use old php native function to count PDF pages (#299)
Before Condtion: In some condition, when count pdf files by using $pdf->pageCount() with spatiePDF modules. It'll run issue in some of PDF files when it can't repopulate with Ghostscript and return error; `PDFDelegateFailed [ghostscript library 10.04] -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r2.0x2.0' -dPrinted=false '-sOutputFile=/tmp/magick-eLtRTJYemQEHIfT1hu3Tuv42uBnodDfK%d' '-f/tmp/magick-Coayb8omrqMkD5YVdcNfHgbCobmeJmeK' '-f/tmp/magick-gjttaXChInmMeYFp5Fq7DgXYBefCiceK': (null)' @ error/pdf.c/ReadPDFImage/712` The backend itself doesn't crash, cause error already handled properly. But it'll be stopper to main process due fail to get total PDF pages. Next Condition: Use old php native function to count PDF pages from previous method, to count properly PDF pages in the mean time
2 parents 6b53a73 + c0703b5 commit b766d78

File tree

6 files changed

+409
-378
lines changed

6 files changed

+409
-378
lines changed

app/Helpers/AppHelper.php

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ function convert($size,$unit)
4242
}
4343
}
4444

45+
function count($path)
46+
{
47+
$pdf = file_get_contents($path);
48+
$number = preg_match_all("/\/Page\W/", $pdf, $dummy);
49+
return $number;
50+
}
51+
4552
function folderSize($dir)
4653
{
4754
$size = 0;

app/Http/Controllers/Api/Core/convertController.php

+2-38
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Ilovepdf\ImagepdfTask;
2323
use Ilovepdf\OfficepdfTask;
2424
use Ilovepdf\PdfjpgTask;
25-
use Spatie\PdfToImage\Pdf;
2625
use Symfony\Component\Process\Process;
2726
use Symfony\Component\Process\Exception\ProcessFailedException;
2827
use Symfony\Component\Process\Exception\RuntimeException;
@@ -507,43 +506,8 @@ public function convert(Request $request) {
507506
$minioUpload = Storage::disk('minio')->get($pdfUpload_Location.'/'.$currentFileName);
508507
file_put_contents(Storage::disk('local')->path('public/'.$pdfUpload_Location.'/'.$currentFileName), $minioUpload);
509508
$newFilePath = Storage::disk('local')->path('public/'.$pdfUpload_Location.'/'.$currentFileName);
510-
try {
511-
$pdf = new Pdf($newFilePath);
512-
$pdfTotalPages = $pdf->pageCount();
513-
Storage::disk('local')->delete('public/'.$pdfUpload_Location.'/'.$trimPhase1);
514-
} catch (\Exception $e) {
515-
$end = Carbon::parse(AppHelper::instance()->getCurrentTimeZone());
516-
$duration = $end->diff($startProc);
517-
appLogModel::where('groupId', '=', $batchId)
518-
->update([
519-
'errReason' => $e->getMessage(),
520-
'errStatus' => 'Failed to count total PDF pages'
521-
]);
522-
cnvModel::where('groupId', '=', $batchId)
523-
->update([
524-
'result' => false,
525-
'procEndAt' => AppHelper::instance()->getCurrentTimeZone(),
526-
'procDuration' => $duration->s.' seconds'
527-
]);
528-
NotificationHelper::Instance()->sendErrNotify(
529-
$currentFileName,
530-
$newFileSize,
531-
$batchId,
532-
'FAIL',
533-
'cnvToImg',
534-
'Failed to count total PDF pages',
535-
$e->getMessage()
536-
);
537-
Storage::disk('local')->delete('public/'.$pdfUpload_Location.'/'.$trimPhase1);
538-
return $this->returnDataMesage(
539-
400,
540-
'PDF Convert failed !',
541-
$e->getMessage(),
542-
$batchId,
543-
null,
544-
'Failed to count total PDF pages'
545-
);
546-
}
509+
$pdfTotalPages = AppHelper::instance()->count($newFilePath);
510+
Storage::disk('local')->delete('public/'.$pdfUpload_Location.'/'.$trimPhase1);
547511
try {
548512
$ilovepdfTask = new PdfjpgTask(env('ILOVEPDF_PUBLIC_KEY'),env('ILOVEPDF_SECRET_KEY'));
549513
$ilovepdfTask->setFileEncryption($pdfEncKey);

0 commit comments

Comments
 (0)