使用php

时间:2017-05-28 01:33:09

标签: php

我知道我们已经在SO中发布了一些帖子,这可能是重复的。

但是,我在过去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


?>

2 个答案:

答案 0 :(得分:1)

只需点击此链接,我希望这更有帮助

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.
?>

答案 1 :(得分:0)

我使用了pdftk并解决了我的问题。