ghostscript windows mac 下安装和 C++ 程序调用

ghostscript 下载地址 https://www.ghostscript.com/releases/gsdnld.html

ghostscript 可以把 pdf 文件拆分成图片。

  1. 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
    
  2. windows 安装方法,略,就是和普通的软件安装一样。

  3. ghostscript 把 pdf 拆分成图片的 C++ 代码

    1. 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;
    }
    
    
    1. 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;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Ghostscript的API接口可以在Windows下实现静默打印PDF文件。Ghostscript是一个开源的PDF处理引擎,它提供了C/C++接口,可以在程序调用相应的函数实现PDF打印功能。 下面是一个简单的示例代码,演示如何使用Ghostscript的API接口实现静默打印PDF文件: ```c++ #include <stdio.h> #include <windows.h> #include "gdiplus.h" #include "gs/gsapi.h" #pragma comment(lib, "gdiplus.lib") #pragma comment(lib, "gsdll32.lib") void print_pdf(const wchar_t* filename, const wchar_t* printer_name) { // 初始化Ghostscript API int argc = 3; const char* argv[3] = { "gsdll32.dll", "-dNOPAUSE", "-dBATCH" }; GSAPI_HANDLE gs_handle; gsapi_new_instance(&gs_handle, NULL); // 设置输出打印机 char printer[256] = { 0 }; char port[256] = { 0 }; WideCharToMultiByte(CP_ACP, 0, printer_name, -1, printer, sizeof(printer), NULL, NULL); PRINTER_INFO_2* pinfo = NULL; DWORD num_printers = 0; EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &num_printers, NULL); pinfo = (PRINTER_INFO_2*)malloc(num_printers * sizeof(PRINTER_INFO_2)); EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pinfo, num_printers * sizeof(PRINTER_INFO_2), &num_printers, NULL); for (DWORD i = 0; i < num_printers; ++i) { if (strcmp(pinfo[i].pPrinterName, printer) == 0) { sprintf_s(port, sizeof(port), "%s", pinfo[i].pPortName); break; } } free(pinfo); gsapi_set_arg_encoding(gs_handle, GS_ARG_ENCODING_UTF8); gsapi_set_stdio(gs_handle, NULL, NULL, NULL); gsapi_init_with_args(gs_handle, argc, (char**)argv); gsapi_set_arg_encoding(gs_handle, GS_ARG_ENCODING_UTF8); gsapi_set_stdio(gs_handle, NULL, NULL, NULL); gsapi_set_poll(gs_handle, NULL); gsapi_set_display_callback(gs_handle, NULL); gsapi_set_stdin(gs_handle, NULL); gsapi_set_stdout(gs_handle, NULL); // 打印PDF文件 wchar_t command[1024] = { 0 }; swprintf_s(command, sizeof(command), L"-dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=\"%%printer%s\" \"%s\"", port, filename); char* argv2[64] = { 0 }; int argc2 = 0; const wchar_t* p = command; while (*p) { argv2[argc2++] = (char*)p; while (*p && !iswspace(*p)) p++; if (*p) *p++ = 0; } gsapi_exit(gs_handle); gsapi_delete_instance(gs_handle); } int main() { Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); wchar_t filename[] = L"C:\\test.pdf"; wchar_t printer_name[] = L"Microsoft Print to PDF"; print_pdf(filename, printer_name); Gdiplus::GdiplusShutdown(gdiplusToken); return 0; } ``` 需要注意的是,使用Ghostscript打印PDF文件需要确保Ghostscript已经正确安装并设置好环境变量。另外,在打印PDF文件前,需要先通过Ghostscript API初始化Ghostscript引擎,并设置输出打印机等参数。最后,调用Ghostscript API打印PDF文件即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值