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;
        }
    }
相关推荐
codingPower17 小时前
制作ftl文件通过FreeMarke生成PDF文件(含图片处理)
java·开发语言·pdf
拓端研究室17 小时前
专题:2025年脑机接口产业蓝皮书:市场规模、专利技术、投融资与临床应用|附40+份报告PDF、数据、可视化模板汇总下载
pdf
日日行不惧千万里17 小时前
孤勇者歌词拼音打印版PDF
pdf
小易吾18 小时前
VISIO导出高清PDF有效方法
笔记·pdf
A0_張張19 小时前
记录一个PDF盖章工具(PyQt5 + PyMuPDF)
开发语言·python·qt·pdf
大卡拉米1 天前
前端组件库 PDF、word、Excel预览
前端·pdf·word
dagouaofei2 天前
全面整理6款文档生成PPT工具,PDF转PPT不再难
python·pdf·powerpoint
yesyesyoucan2 天前
PDF全能处理站:压缩、拆分、合并一站式解决方案与核心技术解析
pdf
sunon_2 天前
解决linux系统PDF中文乱码问题
linux·运维·pdf