Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

insight's avatar

How to avoid Exception TCPDF_PARSER ERROR: decodeFilterFlateDecode: invalid code ?

Dear Friends, I am using TCPDF to merge different category PDF from folders to a single one. But with some PDF's I got error as TCPDF_PARSER ERROR: decodeFilterFlateDecode: invalid code. My controller class code is

<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use App\Models\ApplicationModel;
use DB;
use App\Models\NotificationsModel;
use App\Models\PostModel;
use PDF;
use LynX39\LaraPdfMerger\Facades\PdfMerger;

class ApplicationsController extends Controller
{
    public function ViewApplnPDF(Request $request)
    {     

        if(Session::has('role'))
        {
                $pdfMerger = PDFMerger::init();
                $applnId = base64_decode($request->appln_id);
                $applicantData = DB::table("ksrtc_applications")->where('ksrtc_applications.id',$applnId)
                                ->select('ksrtc_applications.*','ksrtc_applications.created_at as submitted_date','ksrtc_post.*')
                                 ->join('ksrtc_post', 'ksrtc_applications.post_id', '=', 'ksrtc_post.id')
                            ->first();
                                                                 
                $education = DB::table("ksrtc_app_education")->where('app_id',$applnId)->get()->toArray();
                $workExp = DB::table("ksrtc_work_experience")->where('app_id',$applnId)->get()->toArray();
                $addlQuali = DB::table("ksrtc_addl_qualification")->where('app_id',$applnId)->get()->toArray();
                 $applicantPhoto = "applicant_photo/".$applicantData->photo;
                 $arrayP = [
                    'applicant'=> $applicantData,  
                    'education' => $education, 
                    'addlQuali' => $addlQuali, 
                    'workExp' => $workExp,
                    'applicant_photo' => $applicantPhoto                  
                                    
                ];
                $pdf = PDF::setOptions(['isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true,'chroot' => public_path()])->loadView('admin/ViewApplicationPdf', $arrayP);
                            
                $pdf->save(public_path('applicant_personal_details/appln_'.$applnId.'.pdf'));
                $pdfMerger->addPDF(public_path('applicant_personal_details/appln_'.$applnId.'.pdf'));
                $pdfMerger->addPDF(public_path('applicant_photo_id/'.$applicantData->identification_document));
                               
                foreach($education as $e)
                {            
                    $pdfMerger->addPDF(public_path('educational_qualification/'.$e->edn_certificate));
                }
               
                foreach($workExp as $w)
                {            
                    $pdfMerger->addPDF(public_path('work_experience/'.$w->exp_certificate));
                }
                foreach($addlQuali as $q)
                {            
                    $pdfMerger->addPDF(public_path('additional_qualification/'.$q->addl_certificate));
                }
                  
                $pdfMerger->merge();
                
                $pdfMerger->save(public_path('applicant_view_pdf/appln_'.$applnId.'.pdf'));
                return  response()->file('applicant_view_pdf/appln_'.$applnId.'.pdf');

                return PDF::setOptions(['isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true,'chroot' => public_path()])
                ->loadView('admin/ViewApplicationPdf',$arrayP)->stream('Application_PDF.pdf');
        }
        else
        {
            return view('admin/AdminLoginForm');
        }

    }
}

I need to check the files are good for merging before get that ERROR. example : in the code

 foreach($workExp as $w)
                {            
                    $pdfMerger->addPDF(public_path('work_experience/'.$w->exp_certificate));
                }

I need to check each file in code to find it's good or bad. If BAD omit that file and continue with GOOD files for merging .

Please help

Thanks Anes

0 likes
3 replies
tisuchi's avatar

@insight Can you try this approach?

foreach ($workExp as $w) {
    try {
        $pdfPath = public_path('work_experience/' . $w->exp_certificate);
        if (is_file($pdfPath) && is_readable($pdfPath)) {
            $pdfMerger->addPDF($pdfPath);
        } else {
            \Log::error('PDF file not found or not readable: ' . $pdfPath);
        }
    } catch (\Exception $e) {
        \Log::error('Error merging PDF: ' . $w->exp_certificate . ' - ' . $e->getMessage());
    }
}
insight's avatar
insight
OP
Best Answer
Level 2

Finally I wind up that issue by using

use Exception;

and modify the controller function code in try/catch as

try {
                    $pdfMerger->merge();
                } catch (Exception $e) {
                    // Handle the exception and display a graceful message to the user.
                    return view('PDFMergeException')->with('message', 'An error occurred while merging the PDF files. Please try again later or contact support.');
                }

Blade file is a static one with a graceful message as

<!DOCTYPE html>
<html>
<head>
  <title>Exception Message</title>
  
  <style>
  
    body {
      background-color: #333;
      color: #eee;
      font-family: monospace;
      padding: 20px;
    }
    
    svg {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      width: 100%;
    }
    
    .content {
      position: relative;
      z-index: 1;
    }
  
  </style>
</head>

<body>

  <svg viewBox="0 0 500 500">
  
    <defs>
      <pattern id="grid" width="60" height="60" patternUnits="userSpaceOnUse">
        <path d="M 60 0 L 0 0 0 60" fill="none" stroke="rgb(255,255,255,0.1)" stroke-width="1"/>
      </pattern>
      
      <filter id="wavy">
        <feTurbulence x="0" y="0" baseFrequency="0.02" numOctaves="5" seed="2">
          <animate attributeName="baseFrequency" dur="60s" values="0.02;0.05;0.02" repeatCount="indefinite"></animate>
        </feTurbulence>
        <feDisplacementMap in="SourceGraphic" scale="20"/>
      </filter>
      
    </defs>
    
    <rect width="100%" height="100%" fill="url(#grid)"></rect>
    
    <rect x="400" y="350" width="100" height="100" filter="url(#wavy)" fill="rgb(255,255,255,0.3)"></rect>
    
  </svg>

  <div class="content">
  
    <h1>Sorry for Inconvenience</h1>
    
    <p> We apologize for any inconvenience, but there is an issue with parsing the uploaded document due to corrupted PDF.</p>
    
   
  </div>

</body>
</html>

Thanks

Anes P A

Please or to participate in this conversation.