Is there any size limit about FPDFBitmap_CreateBitmapEx

190 views
Skip to first unread message

Frozen Forest

unread,
Jun 20, 2023, 6:16:30 PM6/20/23
to pdfium
I am using these codes to insert UTexture2D images to PDF files.

TArray64<uint8> Buffer contains raw BGRA (FColor struct) datas.
Right now I can open my saved PDF files if I insert them relatively small images (like smaller than 512x512).

But PDF readers give "PDF Header error" if I insert them bigger images like fullhd.
Interesting is I can render these PDF objects with PDFium and view them in Unreal Engine even I can't open them with other readers.

So, is there any size limitation about FPDFBitmap_CreateBitmapEx ?
I couldn't make it work with LoadJpegInline and even if I solve them, I need to insert UTextures from memory because I am using it with Unreal Engine.

For example
I can insert and open PDF file which contains image ABC_Test_1.png 
but I can't open it with ABC_Test_2.png
Details.png contains texture parameter comparison. As you see there are no differences other than dimension and disk size and all other parameters are default Unreal import parameters for PNG files.
ABC_Test_1.png
Details.png
ABC_Test_2.png

Justin Pierce

unread,
Jun 21, 2023, 10:24:59 PM6/21/23
to pdfium
There shouldn't be any problem adding "ABC_Test_2" to a PDF (see attached)

Here is the pseudo code (sorry, my Pdfium implementation is a bit abstracted):
```
    // get pixel data
    void* data = GetData();
    // load page
    FPDF_PAGE page = LoadPage(page_index);
    // create new bitmap
    // determine parameters
    int unpadded_width = width * bytes_per_pixel;
    int remainder = unpadded_width % 4;
    int padding = remainder > 0 ? (4 - remainder) : 0;
    int bmp_stride = width * bytes_per_pixel + padding;
    int format = FPDFBitmap_BGRA;
    switch (bytes_per_pixel)
    {
    case 3:
        format = FPDFBitmap_BGR;
        break;
    case 4:
        format = FPDFBitmap_BGRA;
        break;
    default:
        Err::Throw("Invalid bytes-per-pixel (%d) for bitmap", bytes_per_pixel);
        break;
    }
    // create bitmap
    FPDF_BITMAP bitmap = CreateBitamp(width, height, format, data, bmp_stride);
    // create new image object
    FPDF_PAGEOBJECT object = CreateImageObject(GetHandle());
    // assign bitmap to image object
    SetBitmap(&page, 1, object, bitmap);
    // configure bitmap
    SetImageMatrix(object, desired_width, 0, 0, desired_height, x, y);
    // add image object to page
    InsertObject(page, object);
    GenerateContent(page);
```

Provided all your Pdfium calls are correct, I assume you're just not extracting pixel data from the png properly or messing up the PDF in some other way.

Cheers,
Justin Pierce
Test0.pdf

Frozen Forest

unread,
Jun 22, 2023, 5:41:50 AM6/22/23
to pdfium
What is your save function ? mines are these
https://github.com/FF-Projects-UE/PDF_Reader/blob/main/Source/PDF_Reader/Private/PDFium_Save.cpp#L60

If there was a problem with pixel data, I think pdfium shouldn't render it. Also I don't have any other function after image adding beside save system.

22 Haziran 2023 Perşembe tarihinde saat 05:24:59 UTC+3 itibarıyla brkfs...@gmail.com şunları yazdı:
Message has been deleted

Frozen Forest

unread,
Jun 22, 2023, 9:23:44 PM6/22/23
to pdfium
Problem is about my save function.
I tried to create a new pdfium doc with bytes from WriteBlock's callback->pData

But pdfium couldn't create a PDF file.
I didn't understand solution but it looks like my problem.

Do you have any sample about it ?
22 Haziran 2023 Perşembe tarihinde saat 12:41:50 UTC+3 itibarıyla Frozen Forest şunları yazdı:

Justin Pierce

unread,
Jun 22, 2023, 9:55:38 PM6/22/23
to pdfium
yea there's a little bit to it

I'd check out the Pdfium testing code, i,e, https://pdfium.googlesource.com/pdfium/+/refs/heads/main/testing/fake_file_access.cpp

I'm sure you can find a nice template there somewhere - `FPDF_FILEACCESS` will invoke a callback several times and hand you the document in chunks, and you can do whatever you want with the data - save to file, store in memory, etc
Reply all
Reply to author
Forward
0 new messages