使用PHP代码合并PDF

时间:2017-11-23 05:53:47

标签: php pdf merge

我想使用php代码合并PDF。 所以我从一些网站使用了这段代码,但是这段代码对我来说不起作用,因为找不到类'PDFMerger'而给我错误。 即使执行此代码,输出文件夹仍为空。 我正在使用xampp在localhost上运行代码。

<?php
    include 'PDFMerger.php';

    $pdf = new PDFMerger;

    $pdf->addPDF('D:/sample/pdf-1.pdf', 'all')
        ->addPDF('D:/sample/pdf-2.pdf', 'all')
        ->addPDF('D:/sample/pdf-3.pdf', 'all')
        ->merge('file', 'D:/output/merged.pdf');
?>

1 个答案:

答案 0 :(得分:1)

您可以使用此功能进行合并,

/**
 * Used to merge list of pdf files to make it one
 * @param  array   $files    array of files which are to be merged
 * @param  string  $action   download is currently we implemented, rest you can modify the code
 * @param  integer $page     number of pages need to fetched from every pdf
 * @param  string  $filename name of file for pdf which is merged one
 * @return void
 */
function merge_pdf($files = [], $action = '', $page = 1, $filename = '')
{
    require base_path('vendor/autoload.php');
    $pdf = new \Jurosh\PDFMerge\PDFMerger;
    foreach ($files as $key => $file) {
        $pdf = $pdf->addPDF($file, $page, $key);
    }
    // call merge, output format `file`
    if ($action == 'download') {
        $pdf->merge('download', $filename);
    }
}

以下是文档link