View Full Version : PDF resize?

1st May 2013, 08:30

I've got following problem: My app creates several different kind of PDFs and problem is that file sizes are way too large. The PDFs include pictures that cause the problem. After debugging a while, I found out that if I just scaled the pictures into smaller size (scaledToWidth() ), the PDF size would be ok. Now the problem is that the PDF is created with "highResolution" which I think is 1200dpi and if I reduce the picture size, it will not fill the whole PDF width. As a result, I think I need to set the resolution of the QPainter for example to 600dpi, so that the picture can be scaled to smaller size and it will still fill the whole PDF width. But this is easier said than done because there are so much small details in the PDF, adjusted just in the correct place. I just would not like to do it all again, for every PDF type.

Is there any other way? Something that crossed my mind:

1. Could I somehow rescale the PDF after it's ready?
2. Copy PDF to another file with lower resolution?
3. Set the resolution to 600dpi in the beginning, but somehow change the coordinate system so that even text is written with the highResolution (1200dpi) coordinates, it would be written correctly to the 600dpi PDF?
4. Some other way?

PS. I'm using Qt's internal PDF libraries.


2nd May 2013, 02:22
QPrinter::setResolution(), also see QPrinter::PrinterMode

2nd May 2013, 16:59
Yes, I've seen those already - actually using them. The problem is that those are not much of help after the last detail/line is written to the PDF, right? If I set the resolution smaller in the end, at least the file size does not decrease. I have noticed that using setResolution() with smaller resolution decreases the file size only if used at the beginning of the PDF creation, not at the end which is my target.

I hope someone knows solution for this, otherwise it will take many days to get the PDFs recreated.

2nd May 2013, 18:46
The problem is that those are not much of help after the last detail/line is written to the PDF, right?
It only makes sense to call it before sending anything to QPrinter. I don't understand why you'd want to call it after you're done sending data to the printer.

8th May 2013, 15:08
Ok, I got it solved by my self without changing the PDF resolution from HighResolution.

I noticed that using scaledToWidth() for pictures did not affect to the PDF file size, so I first resized the photos to 1/5 of the original "highResolution" size. Then the for the pic which was 1/5 of the PDF width I used the scaledToWidth function to make it fill the whole PDF width. Because the scaledToWidth() does not increase the PDF file size, as a result, the PDF size is the same as if it was created only with 1/5 size photos. And to my eyes it seems that the quality of the picture in the PDF is still very good if PDF is looked at 100% size. Only if zoomed a lot you can see the difference so this is good enough for my purposes.

9th May 2013, 07:57
I'm sorry I didn't remember the solution correctly and gave some wrong information. Basically this is how I got it solved:

painter.scale(5, 5); // scaling 5x
QImage scaled = ScalePic(pic, QString("W"), 1600); //this function uses scaletoWidth function and 1600 is PDF width / 5
painter.drawImage(20, 100, scaled); // using 1/5 coordinates due to 5x scaling
painter.scale(0.2, 0.2); // scaling set back to default so that following text prints can use the high resolution coordinates.

As a result the image quality is still very good for my purposes and the PDF size is a lot smaller, just like the pics were just 1/5 sized. Btw, I tried to use pics that were resized to the wanted size before using them in PDF, but it did not make any difference to the PDF size for some reason (pic size 7mb vs 250kb -> no difference).