Merge two PDF files into single one using MPDF

2019-08-22 06:41发布

问题:

I am using MPDF library to generate pdf files .I have created two PDF files in my root directory as follows :

$invoice_nos = ['0'=>'ISE-00000014Y18','1'=>'ISE-00000005Y18'];
foreach ($invoice_nos as $key => $invoice_no) {
    $html = 'Invoice No - '.$invoice_no;
    $pdf_file_name = $invoice_no.'.pdf';
    $pdf_file_path = ROOT . '/app/webroot/Service_Invoices/'. DS .$pdf_file_name ;
    ob_start();
    $mpdf = new \mPDF('utf-8', 'A4' ,'','',5,5,36,10,5,4);
    $mpdf->WriteHTML($html,2);
    ob_clean();
    $mpdf->Output($pdf_file_name,'f');
}

Now I want to merge these two files into a single file with different pages. How can I do this? I have searched many examples of it but nothing is working.

回答1:

mPDF is not the best tool to merge PDF files. You'll be better off with GhostScript:

gs -dBATCH -dSAFER -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=combined.pdf invoice1.pdf invoice2.pdf

Alternatively, generate both invoices directly to one file:

$invoice_nos = ['0' => 'ISE-00000014Y18', '1' => 'ISE-00000005Y18'];
$mpdf = new \mPDF('utf-8', 'A4', '', '', 5, 5, 36, 10, 5, 4);

foreach ($invoice_nos as $key => $invoice_no) {
    $html = 'Invoice No - ' . $invoice_no;
    $mpdf->WriteHTML($html, 2);
    $mpdf->WriteHTML('<pagebreak>');
}

$pdf_file_name = $invoice_no . 'invoices.pdf';
$mpdf->Output($pdf_file_name, 'f');


回答2:

Hi There so i actually used this code to Flatten a PDF that had editable forms but i believe we can change it to merge the pdf's together.

This solution uses php's Imagick() which should be part of your hosting environment.

So here is the code, i have tried to comment it as best possible. you will call the mergePdf() and put the destination folder (where your files are and where you will save the new file) and an array of the files (Just there names) to be merged, and then a new file name. once done it will save the new file in the destination folder.

/**
 * mergePdf()
 * 
 * @param mixed $destinationPath
 * @param array $files
 * @param mixed $newFileName
 * @return
 */
public function mergePdf($destinationPath, $files, $newFileName){
    //Create array to hold images
    $array_images = array();
    //Loop through to be merged
    foreach($files as $file){
        //Firstly we check to see if the file is a PDF
        if(mime_content_type($destinationPath.$file)=='application/pdf'){
            // Strip document extension
            $file_name = pathinfo($file, PATHINFO_FILENAME);
            // Convert this document
            // Each page to single image
            $im = new imagick();
            //Keep good resolution
            $im->setResolution(175, 175);
            $im->readImage($destinationPath.$file);
            $im->setImageFormat('png');
            $im->writeImages($destinationPath.$file_name.'.png',false);

            //loop through pages and add them to array
            for($i = 0; $i < $im->getNumberImages(); $i++){
                //insert images into array
                array_push($array_images, $destinationPath.$file_name.'-'.$i.'.png');
            }
            //Clear im object
            $im->clear();
            $im->destroy();


        }else{
            return false;
        }
    }
    //Now that the array of images is created we will create the PDF        
    if(!empty($array_images)){
        //Create new PDF document
        $pdf = new Imagick($array_images);
        $pdf->setImageFormat('pdf');
        if($pdf->writeImages($destinationPath.$newFileName, true)){
            $pdf->clear();
            $pdf->destroy();
            //delete images
            foreach($array_images as $image){
                unlink($image);
            }

            return true;

        }else{
            return false;
        }
    }else{
        return false;
    }
}

public function getMergePdf(){
    $destinationPath = "/your/destination/to/the/file/goes/here/";
    //put the files in the order you want them to be merged
    $files = array('file1.pdf','file2.pdf','file3.pdf');
    $this->mergePdf($destinationPath, $files, "NewPdf.pdf");
}


标签: php pdf mpdf