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;
        }
    }
相关推荐
开开心心_Every1 天前
家长控制电脑软件:定时锁屏管理使用时长
网络协议·tcp/ip·游戏·微信·pdf·excel·语音识别
开开心心就好1 天前
免费抽奖工具支持批量导入+自定义主题
linux·运维·服务器·macos·pdf·phpstorm·1024程序员节
pass_port_csdn2 天前
zotero搬家,迁移换机/重装100% 完美克隆指南:文献PDF、插件配置、文献分类、标签、笔记等所有信息全克隆
笔记·pdf·zotero
开开心心_Every2 天前
电脑定时休息软件:久坐提醒养成活动习惯
游戏·微信·pdf·excel·语音识别·散列表·启发式算法
vlln2 天前
【调研报告】PDF解析技术现状与趋势:从人类阅读到大模型适配的需求
pdf
软件资深者2 天前
全能图片缩略图显示工具,体积较大,直接显示AI,PSD,EPS,PDF,INDD,TIFF,CR2,RAW等格式缩略图的图像解码包
windows·microsoft·pdf·windows11·系统修复
DS随心转小程序2 天前
AI公式不乱码
人工智能·pdf·deepseek·ds随心转
luyun0202022 天前
PDF神仙工具,批量处理
windows·pdf·figma
s石有八九2 天前
PDF/文档LLM作业批改:2025年研究现状、技术进展与研究空白
人工智能·python·pdf·教育·ai教育·作业批改
开开心心_Every2 天前
PDF转图片工具推荐:免费支持批量转换
linux·运维·服务器·spring boot·edge·pdf·powerpoint