PHP MPDF SetSourceFile split pdfs-ThrowExceptions

Exception or error:

I am trying to split a pdf into many to reduce the file size. PDFs come with fewer pages, but the file size remains the same as the full PDF.
Below I show the function with problem. I believe the problem is in SetSourceFile, who maintain the filesize the pdf full. What can I do?

public function downloadPdfFull($id_processo)
{
set_time_limit(300);

    $caminho_pdf_full = public_path('pdf_full_'.$id_processo.'.pdf');

    if(!file_exists($caminho_pdf_full)){
        $this->SavePdfFull($id_processo, $caminho_pdf_full);
    }   

    $tamanho_total = filesize($caminho_pdf_full)/pow(1024, 2);

    if($tamanho_total >= 3){

        $zipname = 'processo-'.$id_processo.'.zip';

        $zip = new \ZipArchive();
        $zip->open($zipname, \ZipArchive::CREATE);

        $mpdf = new \Mpdf\Mpdf([

        ]);

        $mpdf_partes = new \Mpdf\Mpdf([

        ]);
        $mpdf_partes->simpleTables = true;

        $partes = ceil($tamanho_total/3);       

        $paginas = $mpdf_partes->SetSourceFile($caminho_pdf_full);

        $mod = ceil($paginas/($partes*10));

        $contador_partes = 0;
        for($i = 1; $i <= $paginas; $i++) {
            if($i%$mod==0) {
                $contador_partes+=1;
                $filename = 'processo-'.$id_processo.'-PARTE'.$contador_partes.'.pdf';
                $caminho = public_path($filename);

                $mpdf_partes->Output($caminho, 'F'); 

                $zip->addFile($filename);  

                $mpdf_partes = new \Mpdf\Mpdf([

                ]);
                $mpdf_partes->simpleTables = true;

                $mpdf_partes->SetSourceFile($caminho_pdf_full);

                //$mpdf_partes->RestartDocTemplate();
            }
            $pagina_importada = $mpdf_partes->ImportPage($i);

            $mpdf_partes->SetPageTemplate($pagina_importada);

            if($i != $paginas) {
                $mpdf_partes->WriteHTML('<pagebreak/>');
            }
        }

        if($paginas%$mod!=0) {
            $contador_partes+=1;

            $filename = 'processo-'.$id_processo.'-PARTE'.$contador_partes.'.pdf';

            $caminho = public_path($filename);

            $mpdf_partes->Output($caminho, 'F'); 

            $zip->addFile($filename);  
        }

        $zip->close();


        return response()->download($zipname);


    } else {
        $mpdf->Output('processo-'.$id_processo.'.pdf', 'D');
    }

}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *