3

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?

Community
  • 1
  • 1
M J Barrow
  • 111
  • 1
  • 7
  • 2
    Quick question: the PNG format is not *natively* supported in a PDF. Can you find out if it gets translated to an uncompressed bitmap? That could explain the size ballooning. – Jongware Sep 02 '13 at 20:08
  • yes you were right. I wanted to answer my own question but had to wait 7 hours... then forgot :) But very well spotted – M J Barrow Sep 04 '13 at 23:42

2 Answers2

2

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.

Community
  • 1
  • 1
M J Barrow
  • 111
  • 1
  • 7
  • I am using the libharu library to add qr codes to a pdf. All the QR code images were being generated dynamically except for a PNG logo that I was using as the page header. The size of the PDF File droped from 17 MB to 2.4 after applying the suggested line of code. Thank you! – user2325031 Mar 30 '14 at 04:27
  • @M J Barrow: I am also facing the same problem of large size pdfs. I already have the code `HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);` but it is not making any difference. A simple example of my code is here ( http://stackoverflow.com/questions/40111433/hpdf-setcompressionmode-not-working-in-libharu ). Could you please share a little part of your code? – skm Oct 20 '16 at 07:45
0

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.

Pierre
  • 4,114
  • 2
  • 34
  • 39