C++ PDF转图片

cpp 复制代码
C++ PDF转图片

#include "include/fpdfview.h"
#include <fstream>
#include <include/core/SkImage.h>

sk_sp<SkImage> pdfToImg(sk_sp<SkData> pdfData)
{
    sk_sp<SkImage> img;
    FPDF_InitLibrary(nullptr);
    FPDF_DOCUMENT doc;
    FPDF_PAGE     page;
    doc = FPDF_LoadMemDocument(pdfData->bytes(), pdfData->size(), NULL);
    if (doc == NULL) {
        printf("failed to open test document\n");
        return img;
    }
    int numPages = FPDF_GetPageCount(doc);
    printf("document has %d pages\n", numPages);
    for (int i = 0; i < numPages; ++i) {
        page = FPDF_LoadPage(doc, i);
        if (page == NULL) {
            printf("failed to open page %s\n", i);
            continue;
        }
        double width  = FPDF_GetPageWidth(page);
        double height = FPDF_GetPageHeight(page);
        printf("page %d is : %f x %f\n", i, width, height);

        // 创建空白位图对象
        FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
        FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFF, 0xFF, 0xFF, 0xFF);

        // 渲染图片
        FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
        const char *buffer = reinterpret_cast<const char *>(FPDFBitmap_GetBuffer(bitmap));

        SkImageInfo imageInfo = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);
        // const int   totalByte = (imageInfo.minRowBytes() * imageInfo.height());
        SkPixmap pixmap(imageInfo, buffer, imageInfo.minRowBytes());
        img = SkImage::MakeRasterCopy(pixmap);

        FPDFBitmap_Destroy(bitmap);
        FPDF_ClosePage(page);
        break;  //只读取第一页
    }
    FPDF_DestroyLibrary();
    return img;
}

void main() {
	std::string strPath("D:/test.pdf");
	std::ifstream xdZipPath(strPath.c_str(), std::ios::in | std::ios::binary);  // 压缩包zip路径
    if (!xdZipPath.is_open()) {
        std::cout << strPath << " not exist!";
        return;
    }
    size_t   bufSize   = xdZipPath.seekg(0, std::ios::end).tellg();
    uint8_t *buf       = new uint8_t[bufSize];
    xdZipPath.seekg(0, std::ios::beg).read((char *)buf, bufSize);
    xdZipPath.close();

	sk_sp<SkData>  imgData = SkData::MakeWithCopy(buf, bufSize);
	delete[] buf;              // 提前释放内存
	sk_sp<SkImage> img = pdfToImg(imgData);
}

参考

https://pdfium.googlesource.com/pdfium

The C# PDF Library | Pdfium.Net SDK

https://github.com/PDFium/PDFium

https://github.com/bblanchon/pdfium-binaries

pdfium_render - Rust

pdfium_render::pdfium - Rust

Class: PDFium::Page --- Documentation for pdfium (0.0.2)

@taggun/pdfium - npm

https://blogs.embarcadero.com/pdfium-pdf-engine-for-your-delphi-c-builder-firemonkey-applications/

pypdfium2 · PyPI


创作不易,小小的支持一下吧!

相关推荐
星恒随风几秒前
C++ 类和对象入门(三):拷贝构造、赋值运算符重载和深浅拷贝
开发语言·c++·笔记·学习
Cx330❀1 分钟前
【MySQL基础】库与表的全面操纵指南
linux·服务器·网络·数据库·c++·mysql
凡人叶枫3 分钟前
Effective C++ 条款03:尽可能使用 const
linux·开发语言·c++·嵌入式开发
小欣加油17 分钟前
Leetcode31 下一个排列
数据结构·c++·算法·leetcode·职场和发展
Cx330❀22 分钟前
【Linux网络】高性能 TCP 服务器:从多线程到线程池的架构演进与落地实践
linux·运维·服务器·网络·c++·tcp/ip·架构
c2385626 分钟前
C++的IO流深入理解(上)
开发语言·c++
炘爚31 分钟前
Phase 4:业务线程池 — IO/计算解耦
linux·c++
张小姐的猫33 分钟前
【Linux】多线程 —— 线程池 | 单例模式 | 常见锁
linux·运维·服务器·c++·单例模式·设计模式·策略模式
郝学胜-神的一滴35 分钟前
力扣 662 :二叉树最大宽度
java·数据结构·c++·python·算法·leetcode·职场和发展
加油码43 分钟前
Linux IO 多路转接详解:从 select、poll 到 epoll
linux·c++