-1

I want to Merge 2 pdf stored in my storage folder of Laravel app. i only want to merge using MPDF.

i've a function create pdf. From that Function I created a pdf

Here is the Function to create a PDF.

public function generateProjectPDF(){

$projects = Projects::all();
$file_name = 'Project_Task'.rand(1,1000).'.pdf';
$mpdf = new \Mpdf\Mpdf();

$mpdf->WriteHTML(view('pdf.projectpdf',['projects'=>$projects]));

$mpdf->Output('storage/app/files/'.$file_name,'F');
DB::table('merge_pdf')->insert(
    [
        'name' => $file_name,
    ]
);

} Now I have a function to merge 2 pdf.

Now i don't know how to merge the two pdfs.

public function mergePDF(){

$content1 = Storage::get('files/filename.pdf');

 $content1 = Storage::get('files/filename.pdf');

}

2 Answers2

0

Change your mergePDF function accordingly.

public function mergePDF(){
    $mpdf = new \Mpdf\Mpdf();
    $mpdf->SetImportUse(); //only with mPDF <8.0
    $pagecount1 = $mpdf->SetSourceFile('file1.pdf');
    $tplId1 = $mpdf->importPage($pagecount1);
    $mpdf->useTemplate($tplId1);
    $pagecount2 = $mpdf->SetSourceFile('file2.pdf');
    $tplId2 = $mpdf->importPage($pagecount2);
    $mpdf->useTemplate($tplId2);
    $mpdf->Output();
    }

For more details , please refer https://mpdf.github.io/reference/mpdf-functions/importpage-v8.html

Coder
  • 303
  • 2
  • 7
0

merge multiple pdf using Mpdf library

merge pdf function

function mergePdf($pdfArr){
    $pdf = new Mpdf();
    foreach($pdfArr as $currentPdf){
      $pagecount = $pdf->SetSourceFile($currentPdf);
      for($i = 1; $i <= $pagecount; $i++){
        $pdf->AddPage();
        $tplId = $pdf->importPage($i);
        $pdf->useTemplate($tplId);
      }
    }
    //direct show on browser
    $pdf->Output();
    
    //spacefic location save file
    $outputpath = 'location/output.pdf';
    $pdf->Output($outputpath, 'F');
}

Call this function

mergePdf(['path/pdf1.pdf','path/pdf2.pdf', .....])
Vijay Kanaujia
  • 439
  • 4
  • 9