【问题标题】:Merge PDFs using php code使用 php 代码合并 PDF
【发布时间】:2017-11-23 05:53:46
【问题描述】:

我想使用 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');
?>

【问题讨论】:

    标签: php pdf merge


    【解决方案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

    【讨论】:

    • 您要链接的包使用过时的依赖项。我会建议一个最新的版本。一个使用 FPDI 的简单连接脚本是例如可用here
    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2014-04-19
    • 2016-08-10
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多