ghostscript 下载地址 https://www.ghostscript.com/releases/gsdnld.html
ghostscript 可以把 pdf 文件拆分成图片。
-
mac 安装方法
参考链接:mac 下 ghostscript 的安装由于下载地址里没有 mac 的下载链接,bing 了下找到了 mac 下 ghostscript 的安装方法:
brew install ghostscript
直接用 brew install,其实更方便。
在安装的最后能看到这么一段话:==> Installing ghostscript ==> Pouring ghostscript-10.0.0.monterey.bottle.tar.gz 🍺 /usr/local/Cellar/ghostscript/10.0.0: 644 files, 147.4MB ==> Running `brew cleanup ghostscript`...
在上文中可以找到 ghostscript 的安装路径:
···
/usr/local/Cellar/ghostscript/10.0.0
···找到 /usr/local/Cellar/ghostscript/10.0.0 的 bin 目录,在 bin 目录下有个 gs 文件,gs 就是 ghostscript 的可执行文件。即 :
/usr/local/Cellar/ghostscript/10.0.0/bin/gs
-
windows 安装方法,略,就是和普通的软件安装一样。
-
ghostscript 把 pdf 拆分成图片的 C++ 代码
- mac 版
#include <iostream> using namespace std; // 注意📢ghostscript 的安装路径,需要根据实际情况改写。 std::string gs = "/usr/local/Cellar/ghostscript/10.0.0/bin/gs"; bool pdfToImg(string pdfPath, string outputImgPath, int& imgPageCount); int main() { int imgPageCount; string pdfPath = "/Users/shfq/Downloads/xxx.pdf"; string outputImgPath = "/Users/shfq/Desktop/imgs"; pdfToImg(pdfPath, outputImgPath, imgPageCount); std::cout << "转换图片页数:" + to_string(imgPageCount) << std::endl; return 0; } /** * * @param pdfPath * @param outputImgPath * @param imgPageCount * @return */ bool pdfToImg(string pdfPath, string outputImgPath, int& imgPageCount) { try { char line[300]; char message[4096]; string outDirFormat = outputImgPath + "/%05d.jpg"; memset(message, 0, sizeof(message)); sprintf(message, "%s -dNOPAUSE -dBATCH -sDEVICE=jpeggray -r200 -sOutputFile=%s %s", gs.c_str(), outDirFormat.c_str(), pdfPath.c_str()); FILE *fp; int count = 0; const char *sysCommand = message; if ((fp = popen(sysCommand, "r")) == NULL) { memset(message, 0, sizeof(message)); return false; } while (fgets(line, sizeof(line) - 1, fp) != NULL) { string temp = line; if (temp.find("Page") != -1) count++; } pclose(fp); imgPageCount = count; } catch (...) { return false; } return true; }
- windows 版
#include <string>
#include <iostream>
#include<fstream>
#include <time.h>
using namespace std;
// 在 bin 目录下有 gswin32c.exe 和 gswin32.exe 两个 exe ,有什么恶区别呢?
// gswin32c.exe 多了个 c 应该代表的是 command 命令行,gswin32.exe 代表的是 windows 图形界面的可执行文件。大家可以点击一下看一下效果,前者是 cmd 打开的黑色的命令行程序,后者是 windows 图形界面的程序。
// 在这里使用的是 command 命令行程序,命令行程序调用之后会有返回结果,图形界面不会。
// 在这里是 32 位的程序,也有 64 位的。
string gs = "D:\\software\\gs9.56.1\\bin\\gswin32c.exe";
bool pdfToImg(string pdfPath, string outputImgPath, int& imgPageCount);
int main() {
int imgPageCount;
string pdfPath = "xxxx.pdf";
string outputImgPath = "xxxx";
pdfToImg(pdfPath, outputImgPath, imgPageCount);
std::cout << "转换图片页数:" + to_string(imgPageCount) << std::endl;
return 0;
}
/**``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
*
* @param pdfPath
* @param outputImgPath
* @param imgPageCount
* @return
*/
bool pdfToImg(string pdfPath, string outputImgPath, int& imgPageCount)
{
try
{
char line[300];
char message[4096];
string outDirFormat = outputImgPath + "/%05d.jpg";
memset(message, 0, sizeof(message));
sprintf_s(message, "%s -dNOPAUSE -dBATCH -sDEVICE=jpeggray -r200 -sOutputFile=%s %s", gs.c_str(), outDirFormat.c_str(), pdfPath.c_str());
FILE* fp;
int count = 0;
const char* sysCommand = message;
if ((fp = _popen(sysCommand, "r")) == NULL) {
memset(message, 0, sizeof(message));
return false;
}
while (fgets(line, sizeof(line) - 1, fp) != NULL) {
string temp = line;
if (temp.find("Page") != -1) count++;
}
_pclose(fp);
imgPageCount = count;
}
catch (...)
{
return false;
}
return true;
}