【问题标题】:Merge two PDF files into single one using MPDF使用 MPDF 将两个 PDF 文件合并为一个
【发布时间】:2018-10-12 09:44:25
【问题描述】:

我正在使用 MPDF 库生成 pdf 文件。我在我的根目录中创建了两个 PDF 文件,如下所示:

$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');
}

现在我想将这两个文件合并成一个具有不同页面的文件。我怎样才能做到这一点?我已经搜索了很多例子,但没有任何效果。

【问题讨论】:

  • 为什么你在里面输出 pdf 每个循环放在外面并看到它的工作原理

标签: php pdf mpdf


【解决方案1】:

mPDF 不是合并 PDF 文件的最佳工具。 GhostScript 会更好:

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

或者,将两张发票直接生成到一个文件中:

$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');

【讨论】:

  • 这是某种图书馆吗?
  • 我想用 php 来做这个。有什么帮助吗?
【解决方案2】:

您好,所以我实际上使用此代码来拼合具有可编辑表单的 PDF,但我相信我们可以更改它以将 pdf 合并在一起。

此解决方案使用 php 的 Imagick(),它应该是您托管环境的一部分。

所以这里是代码,我尽量评论它。您将调用 mergePdf() 并放置目标文件夹(您的文件所在的位置以及您将保存新文件的位置)和要合并的文件数组(只是那里的名称),然后是一个新的文件名。完成后,它会将新文件保存在目标文件夹中。

/**
 * 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");
}

【讨论】:

  • Imagick 不是 PHP 安装的标准部分,它只能作为 PECL 扩展使用,而且 imagemagick 必须作为外部应用程序安装。这似乎是一种复杂的解决方案。
  • 嗨,我们和这里都有很多关于stackover流程的安装说明。这是一个可能有帮助的。 stackoverflow.com/questions/7870878/…
猜你喜欢
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 2014-11-16
  • 2013-06-10
  • 1970-01-01
  • 2017-12-17
  • 2011-04-04
相关资源
最近更新 更多