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


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

相关推荐
可均可可22 分钟前
C++之OpenCV入门到提高004:Mat 对象的使用
c++·opencv·mat·imread·imwrite
白子寰44 分钟前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
小芒果_011 小时前
P11229 [CSP-J 2024] 小木棍
c++·算法·信息学奥赛
gkdpjj1 小时前
C++优选算法十 哈希表
c++·算法·散列表
王俊山IT1 小时前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
-Even-1 小时前
【第六章】分支语句和逻辑运算符
c++·c++ primer plus
我是谁??2 小时前
C/C++使用AddressSanitizer检测内存错误
c语言·c++
发霉的闲鱼2 小时前
MFC 重写了listControl类(类名为A),并把双击事件的处理函数定义在A中,主窗口如何接收表格是否被双击
c++·mfc
小c君tt2 小时前
MFC中Excel的导入以及使用步骤
c++·excel·mfc
xiaoxiao涛2 小时前
协程6 --- HOOK
c++·协程