2017-05-28 89 views
0

我知道我们已经在SO中有多个帖子,这可能是重复的。使用php合并两个PDF

但是,我从过去24小时一直在努力,没有取得任何成功。我有 通过所有帖子,没有为我工作。

我试过的很简单,我看到很多人说他们为他们工作的例子,但从来没有为我工作过。

下面是我尝试没有任何运气的代码。评论部分有我一直在尝试的方式。在这方面请帮助我。

<?php 
    error_reporting(E_ALL); 

/* 
    require __DIR__ . '/vendor/autoload.php'; 
    use iio\libmergepdf\Merger; 
    use iio\libmergepdf\Pages; 


    $m = new Merger(); 
    $m->addFromFile($file1); 
    $m->addFromFile($file2); 
    file_put_contents($outputName, $m->merge()); 

    $fileArray= array($file1, $file2); 

    // Using Ghostscript 
    $cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName "; 
    //Add each pdf file to the end of the command 
    foreach($fileArray as $file) { 
     $cmd .= $file." "; 
    } 
    echo $cmd; 
    $result = shell_exec($cmd); 

    //using fpdf 

    require('fpdf181/fpdf.php'); 
    require('fpdi162/fpdi.php'); 

    $pdf = new FPDI(); 

    foreach ($fileArray as $file) { 
     print_r($file); 
     $pageCount = $pdf->setSourceFile($file); 
     for ($i = 0; $i < $pageCount; $i++) { 
      echo $i; 
      $tpl = $pdf->importPage($i + 1, '/MediaBox'); 
      $pdf->addPage(); 
      $pdf->useTemplate($tpl); 
     } 
    } 

    // output the pdf as a file (http://www.fpdf.org/en/doc/output.htm) 
    $pdf->Output('F', $outputName); 

*/ 
    $file1 = "pdf1/file1.pdf"; 
    $file2 = "pdf2/file2.pdf"; 
    $datadir = "merged_pdf/"; 
    $outputName = $datadir."merged.pdf"; 

    include 'PDFMerger/PDFMerger.php'; 

    $pdf = new PDFMerger; // or use $pdf = new \PDFMerger; for Laravel 

    $pdf->addPDF($file1); 
    $pdf->addPDF($file2); 

    $pdf->merge('file', "merged.pdf"); // generate the file 

    // $pdf->merge('download', 'samplepdfs/test.pdf'); // force download 

    // REPLACE 'file' WITH 'browser', 'download', 'string', or 'file' for output options 


?> 

回答

0

我用PDFTK和解决我的问题,更有帮助。

0

只要按照这个环节,我希望这是

http://pdfmerger.codeplex.com/

<?php 
include 'PDFMerger.php'; 

$pdf = new PDFMerger; 

$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4') 
    ->addPDF('samplepdfs/two.pdf', '1-2') 
    ->addPDF('samplepdfs/three.pdf', 'all') 
    ->merge('file', 'samplepdfs/TEST2.pdf'); 

    //REPLACE 'file' WITH 'browser', 'download', 'string', or 'file' for output options 
    //You do not need to give a file path for browser, string, or download - just the name. 
?> 
+0

pdfmerger无法下载。链接被破坏 –

+0

下载网址:http://pdfmerger.codeplex.com/downloads/get/99109 – Arun