galkahana / PDF-Writer

High performance library for creating, modiyfing and parsing PDF files in C++
http://www.pdfhummus.com
Apache License 2.0
900 stars 215 forks source link

Make a PDF with an image in memory #252

Open nosleduc opened 5 months ago

nosleduc commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

Thx