Question

I am creating a pdf using libharu in C++ (compiled as a .cgi) that features .png images. The code is fine, but my pdf's are ridiculously oversized. Each page features one image of around 30kb and around 4 text characters in libharu's system font. If I open a 20 page output file of 25mb and "print" it to a file in my operating system it becomes 256kb or so with no visible change to the images.

I think the issue is related to libharu because this guy see's it too, here. He is using php so, libharu as a compiled .cgi. (my C++ code is also compiled .cgi, linked to libharu).

Another guy here on stack overflow has also seen size issues with libharu, but his problem does not mention anything to do with .png so it may be unrelated.

Code for reference:

WorkingGraphic = HPDF_LoadPngImageFromMem  (    *gPdfPtr,
                                                PngAssets[AssetIndex],  //Image data ptr
                                                PngSizes[AssetIndex]);  //data length


//Render Appropriate
HPDF_Page_DrawImage  (*BlitParams->page,
                        WorkingGraphic,
                        BlitParams->OutputRect->X,
                        BlitParams->OutputRect->Y,
                        BlitParams->OutputRect->Width,
                        BlitParams->OutputRect->Height);

Does anyone know how to drive libharu so it creates sensible sized pdf's when you use .png images?

Was it helpful?

Solution

Right I don't know how to remove a question but maybe this info will be useful to others anyway.

I may have had the same issue as this fellow here where I have duplicated this answer.

What I needed to do was enable compression of the .pdf, which I had not done.

Documentation link

C Code:

HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);

It's because I didn't do enough research to know that .pdf format does not natively support .png, or if it has been updated to do so, libharu still doesn't. So, this option tells libharu to use zlib to zip compress everything it can, including your images.

The implementation is not perfect (you will still see a size difference if you zip your output .pdf) but it is acceptable for my use case.

OTHER TIPS

If you don't need the full-size image in the PDF, you can reduce the image to a thumbnail using GDI+ APIs, equal in size to however big you want the image to appear in the PDF.

Save the scaled PNG to a temporary file, and pass the thumbnail PNG to Haru PDF. This will reduce the size of the PDF file.

The image will be pixellated when the viewer zooms in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top