ajrcarey / pdfium-render

A high-level idiomatic Rust wrapper around Pdfium, the C++ PDF library used by the Google Chromium project.
https://crates.io/crates/pdfium-render
Other
341 stars 52 forks source link

Render into File? #129

Closed emilyyyylime closed 9 months ago

emilyyyylime commented 9 months ago

My application requires rendering PDF pages into a shared memory pool managed through a file descriptor, but the only ways to render using the library is into newly-allocated memory or a user-supplied byte buffer, neither of which allow me to do what I want. Being able to render into a general Write implementor would be awesome

ajrcarey commented 9 months ago

Hi @emilyyyylime , this sounds very interesting. Pdfium only supports rendering into byte arrays, which is why you see that approach also reflected in pdfium-render's API. I would need to understand a lot more about your use case - the crates you are using for your shared memory pool, the pool API available to you, ideally some code demonstrating how you'd like to be able to handle PDF rendering in your application - before I could help you with this. My instinct, however, is that Pdfium's limitation of only rendering into byte arrays is likely to be the limiting factor here.

ajrcarey commented 9 months ago

PS you mentioned the Write trait. Is there a reason why Write::write(bitmap.as_raw_bytes()) isn't sufficient for your use case? - other than the need to fully allocate/render the bitmap prior to writing it, I suppose.

emilyyyylime commented 9 months ago

@ajrcarey thanks, I understand if this is a pdfium limitation, probably no way around allocating then—and yes, the only downside is having to preallocate and copy the bitmap over. Thanks again!

ajrcarey commented 9 months ago

Avoiding the bitmap allocation and rendering directly to a file handle would require an upstream change. Your best bet is to submit a feature request to the Pdfium project. Once the functionality is available there, it's no problem to add support for it in pdfium-render.

That said, in theory you can allocate a single bitmap at the top of your application and reuse it for every page render you do, so there should be a way to avoid unnecessary allocations in your application even without specific upstream support for rendering to a file handle.

emilyyyylime commented 9 months ago

Yes, that is also an approach I've considered.