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


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

相关推荐
ElasticPDF-新国产PDF编辑器11 分钟前
基于 PDF.js 的 PDF 文字编辑解决方案,纯前端 SDK,跨平台、框架无关、Web 原生
前端·javascript·pdf
郝学胜-神的一滴12 分钟前
Linux网络字节序详解:从理论到实践
linux·服务器·c语言·开发语言·c++·网络协议·程序人生
EmbedLinX20 分钟前
内存池学习笔记(附C++完整实现)
c++·笔记·学习
Trouvaille ~23 分钟前
【Linux】线程概念与控制(一):线程本质与虚拟地址空间
linux·运维·服务器·c++·线程·虚拟地址空间·pcb
难得的我们28 分钟前
C++中的状态模式
开发语言·c++·算法
啊阿狸不会拉杆31 分钟前
《计算机操作系统》第十章 - 多处理机操作系统
c++·算法·计算机组成原理·os·计算机操作系统
秦苒&1 小时前
【脉脉】AI 创作者 xAMA 知无不言:在浪潮里,做会发光的造浪者
大数据·c语言·数据库·c++·人工智能·ai·操作系统
啊阿狸不会拉杆1 小时前
《计算机操作系统》 第十一章 -多媒体操作系统
开发语言·c++·人工智能·os·计算机操作系统
在路上看风景1 小时前
12. 虚函数
c++
千里马-horse1 小时前
Ray Tracing -- Ray query shadows
c++·rendering·vulkan