码迷,mamicode.com
首页 > 其他好文 > 详细

pdfium 保存pdf

时间:2019-12-10 00:34:40      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:document   tor   exception   exce   UNC   return   dwr   clu   amp   

//
// Created by svenj on 2019/2/3.
//

extern "C" {
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
}

#include <fpdfview.h>
#include <fpdf_save.h>
#include "util.hpp"

struct PdfToFdWriter : FPDF_FILEWRITE {
    int dstFd;
};

static bool writeAllBytes(const int fd, const void *buffer, const size_t byteCount) {
    char *writeBuffer = static_cast<char *>(const_cast<void *>(buffer));
    size_t remainingBytes = byteCount;
    while (remainingBytes > 0) {
        ssize_t writtenByteCount = write(fd, writeBuffer, remainingBytes);
        if (writtenByteCount == -1) {
            if (errno == EINTR) {
                continue;
            }
            LOGE("Error writing to buffer: %d", errno);
            return false;
        }
        remainingBytes -= writtenByteCount;
        writeBuffer += writtenByteCount;
    }
    return true;
}

static int writeBlock(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) {
    const PdfToFdWriter* writer = reinterpret_cast<PdfToFdWriter*>(owner);
    const bool success = writeAllBytes(writer->dstFd, buffer, size);
    if (success < 0) {
        LOGE("Cannot write to file descriptor. Error:%d", errno);
        return 0;
    }
    return 1;
}

JNI_FUNC(jboolean, PdfWriter, nativeWrite)(JNI_ARGS, jlong docPtr, jint fd) {
    auto document = reinterpret_cast<FPDF_DOCUMENT>(docPtr);
    PdfToFdWriter writer;
    writer.dstFd = fd;
    writer.WriteBlock = &writeBlock;
    FPDF_BOOL success = FPDF_SaveAsCopy(document, &writer, FPDF_NO_INCREMENTAL);
    if (!success) {
        jniThrowExceptionFmt(env, "java/io/IOException",
                "cannot write to fd. Error: %d", errno);
    }
}

  

pdfium 保存pdf

标签:document   tor   exception   exce   UNC   return   dwr   clu   amp   

原文地址:https://www.cnblogs.com/hshy/p/12014069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!