在 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);
?>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现