PDF压缩

winnzip项目pdf压缩部分

复制代码
/**
     * 压缩PDF文件
     * @param inputFile 输入PDF文件路径
     * @param outputFile 输出PDF文件路径
     * @param compressionLevel 压缩等级: 0=小尺寸, 1=中等尺寸, 2=大尺寸
     * @param lossless 是否无损压缩
     * @return 压缩是否成功
     */

使用Ghostscript命令行方式进行pdf压缩,这个东西自己找,开源的。

检查文件是否存在

复制代码
 static bool fileExists(const std::string& filePath) {
        DWORD const attr = GetFileAttributes(CommonTool::charToWchar(filePath).c_str());
        return (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY));
    }

如果文件属性有效并且不是一个目录即存在,返回验证结果。

构造gs命令

复制代码
static std::string constructGSCommand(const std::string& inputFile, const std::string& outputFile,
                                          int compressionLevel, bool lossless) {
        std::string command = "gswin64c.exe -sDEVICE=pdfwrite \"-dCompatibilityLevel=1.4\"  -dNOPAUSE -dBATCH -dQUIET";

        // 根据压缩等级设置压缩参数
        switch (compressionLevel) {
            case 0:  // Small size
                command += " -dPDFSETTINGS=/screen -dEmbedAllFonts=true";
                break;
            case 1:  // Medium size
                command += " -dPDFSETTINGS=/ebook -dEmbedAllFonts=true";
                break;
            case 2:
                command += " -dPDFSETTINGS=/printer";
                break;
            default:
                command += " -dPDFSETTINGS=/default";
                break;
        }

        // 如果是无损压缩,使用默认设置
        if (lossless) {
            command += " -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode";
        }

        command += " -sOutputFile=\"" + outputFile + "\" \"" + inputFile + "\"";

        spdlog::debug("Constructed gs command: {}", command);
        return command;
    }

参数输入文件,输出文件,压缩等级,是否无损压缩,根据参数进行字符串拼接。

执行命令行

cpp 复制代码
static int executeCommand(const std::string& command) {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;

        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);
        ZeroMemory(&pi, sizeof(pi));

        std::array<WCHAR, 1024> buffer{};
        wcscpy_s(buffer.data(), buffer.size(), CommonTool::charToWchar(command).c_str());
        // 创建不带窗口的进程
        if (!CreateProcess(nullptr, buffer.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, nullptr, &si,
                           &pi)) {
            spdlog::error("Failed to create process for command: {}", command);
            return -1;
        }

        // 等待进程结束
        WaitForSingleObject(pi.hProcess, INFINITE);

        DWORD exit_code = 0;
        GetExitCodeProcess(pi.hProcess, &exit_code);

        // 关闭句柄
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);

        return static_cast<int>(exit_code);
    }

压缩PDF的主函数

cpp 复制代码
static int compressPDF(const std::string& inputFile, const std::string& outputFile, int compressionLevel,
                           bool lossless) {
        try {
            // 检查输入文件是否存在
            if (!fileExists(inputFile)) {
                spdlog::error("Input file does not exist: {}", inputFile);
                return false;
            }

            std::string const command = constructGSCommand(inputFile, outputFile, compressionLevel, lossless);

            // 执行命令
            int const result = executeCommand(command);

            return result;
        } catch (const std::exception& e) {
            spdlog::error("Exception during PDF compression: {}", e.what());
            return -1;
        }
    }
相关推荐
蒸汽求职10 分钟前
北美求职身份过渡:Day 1 CPT 的合规红线与安全入职指南
开发语言·人工智能·安全·pdf·github·开源协议
其实秋天的枫1 小时前
【26年四级最新】英语四级2015-2025年12月真题及答案+高频核心词汇1500个pdf电子版
经验分享·pdf
优化控制仿真模型2 小时前
【2026年】新大纲普通话考试真题题库50套(PDF电子版)
经验分享·pdf
优化控制仿真模型3 小时前
【26年6月四级】英语四级2015-2025年12月真题及答案+高频核心词汇1500个pdf电子版
经验分享·pdf
Eiceblue5 小时前
C# 删除 PDF 页面:单页 / 多页批量删除技巧
前端·pdf·c#
其实秋天的枫7 小时前
【2026年】新大纲普通话考试真题题库50套(PDF电子版)
经验分享·pdf
小美丽1238 小时前
苹果Mac电脑下给PDF盖电子印章的方法—Mac批量盖电子印章/盖骑缝章的方法
pdf·电脑
m0_694845571 天前
opendataloader-pdf部署教程:构建PDF数据处理系统
服务器·前端·前端框架·pdf·开源
asdzx671 天前
C#:从 URL 下载 PDF 文档到本地
开发语言·pdf·c#
开开心心就好1 天前
经典塔防游戏移植移动端随时畅玩
java·前端·科技·游戏·edge·django·pdf