PDA

View Full Version : Why QPrinter always generates PDF docs with the same size?



SkripT
21st April 2006, 10:59
Hi, I need to use QPrinter to generate a PDF doc with several images (one image for page). I use the code below to generate a PDF doc with one image in one page, the problem is that the size of the resulting doc is extremely big (7 MB :confused: ). I've tried to set the resolution to the minimum resolution (72dpi) that supports the printer driver (PDFCreator) and the printer mode to "ScreenResolution" but it doesn't works. I also have tried to set to the maximum compresion of images in PDFCreator but it neither works. Very strange. Somebody could suggest me something else? Thanks.


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QPrinter printer(QPrinter::ScreenResolution);
printer.setResolution(72);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("../test.pdf");
printer.setFullPage( TRUE );
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);

QPainter painter;
painter.begin(&printer);
painter.drawImage(printer.pageRect(), QImage("../proba.jpg");
painter.end();

return 0;
}

PD: The original size of the test image ("proba.jpg") is 3368x2440 pix and it's in grayscale. But I also have tried to set the colorMode of the printer to graysCale but the resulting size is always the same

wysota
21st April 2006, 11:48
What is the volume of the image file? And in what format does the image get saved into the pdf? Maybe it's a png (don't ask me why)?

SkripT
21st April 2006, 11:59
What is the volume of the image file?
1,36 MB


And in what format does the image get saved into the pdf? Maybe it's a png (don't ask me why)?

Maybe that's the problem: that it's saved in png or bmp. Is it a parameter that can be fixed with Qt or I have to set it with PDFCreator?

wysota
21st April 2006, 12:11
I don't know that. First check if it's saved as png/bmp. There are surely applications which can check that.

Another possibility could be that the font embedding bloated the size a little too.

Which Qt version do you use?

SkripT
21st April 2006, 12:11
As I commented before I have set the option in PDFCreator to compress the PDF images in color and grayscale to "JPEG-Maximum" but it's like QPrinter doesn't use them because the final document is always the same :eek: (PDFCreator is the only driver that I have installed to do the conversion, so it has to use it :confused: ) Maybe QPrinter with QPainter uses some particular method to "insert" the images?

PD: I use Qt4.1.1

wysota
21st April 2006, 12:16
I don't get one thing... If you're printing directly to PDF, what do you need PDFCreator for?

SkripT
21st April 2006, 12:22
I don't get one thing... If you're printing directly to PDF, what do you need PDFCreator for?

I thought it too, but I've tried to run the code above without having installed PDFCreator and I get a warning that QPrinter didn't found any correct driver :confused:

SkripT
21st April 2006, 12:27
Well I tried it by scaling the image to the correct size before painting it and the size of the final doc has been reduced a lot. The problem is that If I try to zoom the image, it loses a lot of quality. So I don't know why painting it directly in the correct rect dosen't works. All I want is have the original image fitting all the page in the final PDF doc and zoom it if it's greater than the page without losing quality (it's possible because if I enable setFullPage with QPrinter, the image is painted fitting all the page and it doesn't loses quality if I try to zoom it, the problem is that the resulting doc has a big size ;) ). I think that internally the document stores the original image and shows it scaled fitting all the avaiable page space. Anybody knows how to make it?

PD: Maybe the problem is that painting to a printer with QPainter is different than "inserting" directly an image to the PDF doc....

SkripT
21st April 2006, 18:50
Does anybody knows if there's some another way to print an image using QPrinter than painting it with QPainter. I'm almost sure that the problem that the final doc has a big size is caused by painting the image using QPainter. I think it "stores" it like if the images has no compression or something similar... All I want is have the same image than the original in the final PDF doc filling all the page space.

Brandybuck
21st April 2006, 19:45
I thought it too, but I've tried to run the code above without having installed PDFCreator and I get a warning that QPrinter didn't found any correct driver
That's sounds very wrong to me. I just uninstalled my PDF driver (pdf995), then ran the Text Edit demo. I'm not having any problems exporting to PDF without a separate PDF driver.

SkripT
22nd April 2006, 10:27
That's sounds very wrong to me. I just uninstalled my PDF driver (pdf995), then ran the Text Edit demo. I'm not having any problems exporting to PDF without a separate PDF driver.

I don't understand this. What I'm sure is that before installing PDFCreator there wasn't any printer in my system and I've tried to run the code above and it alerted me that no driver was found (for sure). But nevermind, my real problem is that, with or without a printer driver, if i try to paint an image with QPainter under a QPrinter, the resulting doc has a big size :crying:
I would like to know if there's some other way to convert a succesion of images into a PDF doc?

SkripT
22nd April 2006, 15:59
Well, I add a new post but I don't know if anyone can help me :( . I've installed Adobe Acrobat Reader 7.0 and has an option that allows working with the images of a PDF document. If I open the document created with Qt, the option is disabled. Otherwise, if I open another doc not created with Qt where the images are compressed, the option is enabled and allows extract the images of the doc. Maybe the problem is the driver that I use (PDfCreator). Anyone knows if there's some way to generate docs with QPrinter where the images are compressed? Can anyone check if, using Qt, can generate docs where the images are compressed using the code that I've posted above or some other way?

wysota
22nd April 2006, 17:33
It might be a good idea to take a look at the source code responsible for generating images in pdf files in Qt4. The answer for your problem may be there. If that doesn't help, writing to Trolltech is an option too. I doubt we'll be able to help much here...

SkripT
23rd April 2006, 16:47
If anybody is interested, I have asked my problem in Qt Interest mailing list, and the response from a trolltech's member is the following:

The problem is not compression but the fact that the image is embedded
multiple times, even though it is the same. You can either wait until Qt
4.1.3, which will contain a fix for that, or you can try the current
snapshots which should have it, too.

So, finally seems that's just a bug... Does anybody knows how much should I have to wait until the next release?

wysota
23rd April 2006, 16:58
You can always use a snapshot as suggested.

SkripT
23rd April 2006, 17:04
You can always use a snapshot as suggested.

Yes but I'm a little lazy to build it all (> 2hrs) :p

SkripT
25th April 2006, 13:41
Finally the Trolls have opened a new task tracker with my bug (ID: 111669): http://www.trolltech.com/developer/tasktracker.html?id=111669&method=entry.
I hope it will be fixed soon.