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


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

相关推荐
zxctsclrjjjcph4 小时前
【高并发内存池】从零到一的项目之centralcache整体结构设计及核心实现
开发语言·数据结构·c++·链表
winfredzhang4 小时前
使用Python 打造多格式文件预览工具 — 图、PDF、Word、Excel 一站式查看
python·pdf·word·excel·照片·查看,zip,复制
炯哈哈5 小时前
【上位机——MFC】单文档和多文档视图架构
开发语言·c++·mfc·上位机
利刃大大5 小时前
【网络编程】四、守护进程实现 && 前后台作业 && 会话与进程组
linux·网络·c++·网络编程·守护进程
oioihoii5 小时前
C++23 std::tuple与其他元组式对象的兼容 (P2165R4)
c++·链表·c++23
赵和范5 小时前
C++:书架
开发语言·c++·算法
龙湾开发7 小时前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 05.纹理贴图
c++·笔记·学习·3d·图形渲染·贴图
yasuniko7 小时前
C++线程库
开发语言·c++
还有几根头发呀8 小时前
深入理解C/C++内存管理:从基础到高级优化实践
c++
zxctsclrjjjcph9 小时前
【递归、搜索和回溯】递归、搜索和回溯介绍及递归类算法例题
开发语言·c++·算法·力扣