I tried to convert pdf to image with pdfium but noticed that the conversion is not working properly.
Do you have any idea how to solve this problem?
const char* fonts[] = {
"../fonts/",
NULL
};
// FPDF_InitLibrary();
FPDF_LIBRARY_CONFIG config;
config.version = 2;
config.m_pIsolate = NULL;
config.m_v8EmbedderSlot = 0;
config.m_pUserFontPaths = fonts;
FPDF_InitLibraryWithConfig(&config);
int page_count;
FPDF_DOCUMENT doc = FPDF_LoadDocument(pdfFilePath.c_str(), 0);
page_count = FPDF_GetPageCount(doc);
for (int p = 0; p < page_count; p++) {
FPDF_PAGE page = FPDF_LoadPage(doc, p);
if (page) {
// Physical size 72 dots per inch
double pwidth = FPDF_GetPageWidth(page);
double pheight = FPDF_GetPageHeight(page);
// Pixel size based on dpi
int width = (int) (pwidth) * 300 / 72;
int height = (int) (pheight) * 300 / 72;
// BGRx / BGRA 4 channels
FPDF_BITMAP bmp = FPDFBitmap_Create(width, height, 0);
uint8_t *ptr = (uint8_t *) FPDFBitmap_GetBuffer(bmp);
memset(ptr, 255, width * height * 4);
FPDF_RenderPageBitmap(bmp, page, 0, 0, width, height, 0, 0);
uint8_t *dst = (uint8_t *) malloc(width * height * 3);
uint8_t *dptr = dst;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {// BGR->RGB
*dst++ = (ptr[0]);// * ptr[3] + 255 * (255 - ptr[3])) / 255;
*dst++ = (ptr[1]);// * ptr[3] + 255 * (255 - ptr[3])) / 255;
*dst++ = (ptr[2]);// * ptr[3] + 255 * (255 - ptr[3])) / 255;
ptr += 4;
}
}
std::string name = outputFolderPath + "/page" + std::to_string(p) + ".tiff";
cv::Mat mat(height, width, CV_8UC3, dptr);
cv::imwrite(name.c_str(), mat);
mat.release();
free(dptr);
FPDFBitmap_Destroy(bmp);
FPDF_ClosePage(page);
}
}
FPDF_CloseDocument(doc);
FPDF_DestroyLibrary();