I was trying to merge some PDF on Laravel 10. If there are any other type of file i am converting them into PDF and mergeing . Sometime for some PDF i got this kind of error .
This PDF document probably uses a compression technique which is not supported by the free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)
is there any way to solve this ?
It was my function to merge PDF.
function mergePDFs(array $pdfPaths, $fileName, $fullPublicPath = false)
{
if ( count($pdfPaths) <= 0 ) return null;
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 900);
$pdfTempFiles = [];
$oMerger = PDFMerger::init();
try {
foreach ($pdfPaths as $pdfPath) {
$tempPath = 'uploads/temp/' . IlluminateSupportStr::uuid().'.pdf';
$extension = getExtension($pdfPath);
$pdfPath = public_path(substring($pdfPath, "uploads"));
if ( $extension == 'pdf' ) {
$oMerger->addPDF($pdfPath, 'all');
continue;
}
if ( isDocType($pdfPath) ){
if ( $extension == 'xlsx' || $extension == 'xls' ){
$path = xlsToPDF($pdfPath, $tempPath);
$oMerger->addPDF($path, 'all');
$pdfTempFiles[] = $path;
} else {
if ( $extension == 'doc' ){
$newPath = public_path('uploads/temp/' . IlluminateSupportStr::uuid().'.docx');
$pdfPath = docToDocx($pdfPath, $newPath);
$pdfTempFiles[] = $newPath;
}
$path = docxToPDF($pdfPath, $tempPath);
$oMerger->addPDF($path, 'all');
$pdfTempFiles[] = $path;
}
} else {
if ( $extension == 'jpeg' || $extension == 'jpg' || $extension == 'png' ){
$path = imageToPDF($pdfPath, $tempPath);
$oMerger->addPDF($path, 'all');
$pdfTempFiles[] = $path;
} elseif ( $extension == 'webp' ){
$path = convertWebpToPdf($pdfPath, $fileName);
$oMerger->addPDF($path, 'all');
$pdfTempFiles[] = $path;
} elseif ( $extension == 'bmp' ){
$path = convertBmpToPdf($pdfPath, $fileName);
$oMerger->addPDF($path, 'all');
$pdfTempFiles[] = $path;
}
}
}
$oMerger->merge();
$oMerger->save($fileName);
} catch (Exception $e) {
throw new Exception();
}
foreach ($pdfTempFiles as $pdfTempFile) {
unlink($pdfTempFile);
}
return ($fullPublicPath) ? $fileName : url(substring($fileName, 'uploads'));
}