美文网首页
在 PHP 中使用 Ghostscript 合并多个 PDF 文

在 PHP 中使用 Ghostscript 合并多个 PDF 文

作者: elileo | 来源:发表于2023-07-22 22:26 被阅读0次

1、在 PHP 中使用 Ghostscript 合并多个 PDF 文件

Ghostscript 是一个没有内置在 Windows 上的命令行库。首先,我们需要从链接 https://ghostscript.com/releases/gsdnld.html 安装 Ghostscript。

根据你的操作系统版本下载 Ghostscript 文件。该文件将是这样的:
gs9550w64.exe
运行下载的文件并安装它。安装完成后,转到安装目录的 bin 文件夹并将文件 gswin64c.exe 重命名为 gs.exe 以使用 gs 作为命令。

Ghostscript 的安装完成。下一步是使用 PHP 将 pdf 文件与 Ghostscript 合并。

<?php
$files= array("one.pdf","two.pdf","three.pdf");
$pdfdir = "C:/Apache24/htdocs/samplepdfs/";
//we gave absolute path because sometimes the libraries can't detect the path. Please use your path here.
$output = $pdfdir."NewMergedPDF.pdf";
$cmd = "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=".$output." -dBATCH ";
//setting enviroment variable path
putenv('PATH=C:\Program Files\gs\gs9.55.0\bin');

foreach($files as $file) {
    //Setting path for each file
    $pdf= $pdfdir.$file;
    //adding each file to the command
    $cmd = $cmd.$pdf." ";
}
// The final command will be the comment below, if you run this command directly in cmd, the output will be similar
//gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=C:/Apache24/htdocs/samplepdfs/NewMergedPDF.pdf -dBATCH C:/Apache24/htdocs/samplepdfs/one.pdf C:/Apache24/htdocs/samplepdfs/two.pdf C:/Apache24/htdocs/samplepdfs/three.pdf
exec($cmd);
?>

上面的代码将三个 PDF 文件作为输入,并将它们合并为一个 PDF。
输出:


Merge Multiple PDFs Using Ghostcript.gif

2、在 PHP 中使用 FPDF 和 FPDI 库合并多个 PDF

首先,我们需要从下面的链接下载 FPDFFPDI 库。

下载 zip 或任何其他文件后,将它们从运行你的 test.php 文件中解压缩到文件夹中。

提取文件后,运行以下示例:

<?php
//import fpdi and fpdf files
use setasign\Fpdi\Fpdi;
require_once('fpdf/fpdf.php');
require_once('fpdi/src/autoload.php');

//create your class to merge pdfs
class MergePdf extends Fpdi
{
    public $pdffiles = array();

    public function setFiles($pdffiles)
    {
        $this->pdffiles = $pdffiles;
    }
    //function to merge pdf files using fpdf and fpdi.
    public function merge()
    {
        foreach($this->pdffiles AS $file) {
            $pdfCount = $this->setSourceFile($file);
            for ($pdfNo = 1; $pdfNo <= $pdfCount; $pdfNo++) {
                $pdfId = $this->ImportPage($pdfNo);
                $temp = $this->getTemplatesize($pdfId);
                $this->AddPage($temp['orientation'], $temp);
                $this->useImportedPage($pdfId);
            }
        }
    }
}

$Outputpdf = new MergePdf();
//we gave absolute path because sometimes the libraries can't detect the path. Please use your path here.
$Outputpdf->setFiles(array('C:\Apache24\htdocs\samplepdfs\one.pdf', 'C:\Apache24\htdocs\samplepdfs\two.pdf', 'C:\Apache24\htdocs\samplepdfs\three.pdf'));
$Outputpdf->merge();

//I: The output pdf will run on the browser
//D: The output will download a merged pdf file
//F: The output will save the file to a particular path.
//We select the default I to run the output on the browser.
$Outputpdf->Output('I', 'Merged PDF.pdf');
?>

上面的代码将导入 fpdi 和 fpdf 库并合并 pdf 文件。输出将是在浏览器上运行的合并 pdf。

输出:


Merge Multiple PDFs Using FPDF and FPDI.gif

其他库也可以对 PDF 执行类似的操作,但 Ghostscript 和 Fpdi 是使用最广泛的两个。

来源:https://www.delftstack.com/zh/howto/php/php-merge-pdfs/

相关文章

网友评论

      本文标题:在 PHP 中使用 Ghostscript 合并多个 PDF 文

      本文链接:https://www.haomeiwen.com/subject/dexfpdtx.html