目录
2.3 在 Linux 上安装(Ubuntu/Debian)
Ghostscript 简介与使用指南
1. 什么是 Ghostscript?
Ghostscript 是一款强大的开源工具,主要用于处理 PostScript(PS)和 PDF 文件。它既可以作为独立的命令行工具运行,也可以作为库嵌入到其他程序中。Ghostscript 主要用于:
- 查看和转换 PDF/PS 文件
- PDF 压缩与优化
- PDF 合并与拆分
- 打印与渲染
- 图像格式转换(如 PDF 转 PNG、JPEG)
Ghostscript 适用于 Windows、Linux 和 macOS,并且被广泛用于自动化文档处理任务。
2. Ghostscript 的安装
2.1 在 Windows 上安装
- 访问 Ghostscript 官网 https://www.ghostscript.com
- 下载适用于 Windows 的
Ghostscript
安装包(一般为gswin64.exe
)。 - 运行安装程序并按照提示完成安装。
- 安装完成后,可以使用命令行测试:
gswin64 --version
2.2 在 macOS 上安装(使用 Homebrew)
brew install ghostscript
2.3 在 Linux 上安装(Ubuntu/Debian)
sudo apt install ghostscript
3. Ghostscript 的基本用法
Ghostscript 通过 gs
命令行工具提供各种功能,以下是一些常见的使用方法。
3.1 查看 PDF 或 PostScript 文件
gs -dNOPAUSE -dBATCH -sDEVICE=display input.pdf
3.2 将 PDF 转换为 PNG/JPEG
gs -sDEVICE=png16m -r300 -o output.png input.pdf
其中:
-sDEVICE=png16m
:输出为 PNG 格式-r300
:指定分辨率(DPI),提高图像清晰度
3.3 压缩 PDF 文件
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -o output.pdf input.pdf
压缩级别:
/screen
(低质量,适用于屏幕阅读)/ebook
(中等质量)/printer
(高质量)/prepress
(最高质量)
3.4 合并多个 PDF 文件
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged.pdf -dBATCH file1.pdf file2.pdf
3.5 拆分 PDF(提取页面)
提取 PDF 的第 1-3 页:
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dFirstPage=1 -dLastPage=3 -sOUTPUTFILE=output.pdf input.pdf
3.6 转换 PS 文件为 PDF
gs -sDEVICE=pdfwrite -o output.pdf input.ps
4. 适用场景
Ghostscript 在以下场景中非常有用:
- 自动化 PDF 处理:用于 Web 服务器或批量任务自动转换和优化 PDF。
- 打印行业:在印刷工作流程中转换和优化 PostScript 文件。
- Linux 服务器:在无 GUI 环境中批量处理 PDF/PS 文件。
- 文档管理系统:集成到应用程序中处理和优化文档。
5. 总结
Ghostscript 是一个功能强大的命令行工具,可用于 PDF/PS 处理、压缩、转换、合并等任务。它适用于各种操作系统,并且在许多企业级解决方案中得到了广泛应用。如果你需要高效处理 PDF 文件,Ghostscript 是一个非常值得尝试的工具。
希望这篇文章能帮助你快速上手 Ghostscript!