Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a PDF with an image in memory #252

Open
nosleduc opened this issue May 24, 2024 · 4 comments
Open

Make a PDF with an image in memory #252

nosleduc opened this issue May 24, 2024 · 4 comments

Comments

@nosleduc
Copy link

Hi

I have compiled the library without any image libraries (tif, png, jpeg).
I would like to make a pdf and put only one image in it. My image is a raw buffer in memory.
How can I add it without the need to write my image on the disk as a png file and compiling the library with png support ?

@galkahana
Copy link
Owner

You could use PDFWriter::CreateFormXObjectFromPNGStream to read a PNG image from memory. It gets a general purpose IByteReaderWithPosition interface that can be implemented to read from an abstract source. There's several implementations of this interface with the library. One that might be fitting to read from memory is InputByteArrayStream that accepts an unsigned char array that you can put the image data there.

I do however not really understand the part about "without .... and compiling the library with png support". png support is required for reading png images regardless of whether the source of png image data is from - disk or memory. it's required in order to decode png data to raw binary color data which can then be compressed to one of the compression formats available with PDF.

There's an option to compile the library without PNG support, if you don't expect png image input. check this section of the readme document.

@nosleduc
Copy link
Author

Thanks for answering me.

Today I have compiled the library without any image support because I am using it to read existing PDF files.
I am retrieving some information stored in the PDF like texts and paths only.
So there was no need to use libpng, libjpeg or libtiff and so that's why I disabled the support of them.

But now I am planning to use it to write small PDF file containing just only image in it.
My image is the result of a 2D rendering and it is stored as a QImage ( Qt framework ).
So in my case, I have a raw buffer and not a png image in memory.

I was wondering how can I do that by just using my raw buffer.

@galkahana
Copy link
Owner

oh gotcha. ok...so you got the raw image data and you want to write an image object.
ok sure, that's possible, that's basically how all the image embedding works with Hummus, and you got access to the methods to do it.

You can learn how to do such a thing from the PNG implementation actually, or otherwise read the PDF format and look for Image XObject definition. then you can use the ObjectsContext to build the image.

So you can track https://github.com/galkahana/PDF-Writer/blob/master/PDFWriter/PNGImageHandler.cpp specifically CreateFormXObjectFromPNGStream, and even more specifically CreateImageXObjectForData which directly writes the image object. i normally like to surround the image with a form xobject scaling the image, so it's later convenient to just place it.

@nosleduc
Copy link
Author

Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants