Size of PDF files generated with Qt
-
Hello,
I'm trying to make a little app to create a PDF file from pictures. The problem is the size of the generated PDF, it's big !
By exemple I generated a PDF file with 93 pictures (about 330 Ko each) and the final size is 81Mo.Is there any way to reduce the size ?
Here is how I make the PDF file :
@
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setFullPage(true);
printer.setPageSize(QPrinter::A4);
printer.setOutputFileName(m_url + m_pdfName);
printer.setOrientation(QPrinter::Portrait);
printer.setPrinterName(printer.printerName());
printer.setResolution(600);QPainter paint; if(!paint.begin(&printer)) return; QMatrix matrix; matrix.rotate(270); QRect rect = paint.viewport(); for (int i = 0; i < m_view->topLevelItemCount(); i++) { if(i > 0) { if(!printer.newPage()) return; } QString imageUrl = m_view->topLevelItem(i)->text(1); if(imageUrl.isEmpty()) continue; QImage image(imageUrl); QSize size = image.size(); if(size.width() > size.height()) { image = image.transformed(matrix); size = image.size(); } size.scale(rect.size(), Qt::KeepAspectRatio); int x = rect.x() + ((rect.size().width() - size.width())/2); int y = rect.y() + ((rect.size().height() - size.height())/2); paint.setViewport(x, y, size.width(), size.height()); paint.setWindow(image.rect()); paint.drawImage(0, 0, image); } paint.end(); exit();
@
Thank you very much,
Kevin -
QPrinter decompresses the image to raw RGB32 format and compresses the image to JPEG (at 94% quality) if the QJPEG plugin is available or with zlib if it isn't.
So, just to be sure, that JPEG compression is used, can you test (with qDebug() for example), the result of @QImageWriter::supportedImageFormats().contains("jpeg")@
-Also, you can use drawPixmap/drawImage to scale the image by specifying the rectangle (QRect) where you want to draw the image as the first parameter rather than scaling the image yourself (the image will be stored with its original size, and scaled by the PDF viewer).-
Edit: it seems that using viewport and window does the same thing as specifying a QRect to draw the image. -
Hello,
Thank you for your response, the result is "true".
If I can use the same compression as initially for the image I'm happy :) Do I need to use QImageWriter instead of QImage and set the compression ?Thank you,
Kevin -
You can't choose the compression options, they are hard-coded in the private class that generate the pdf. Since the JPEG plugin is found, you can't improve the PDF size on that point.
You might have to find an external tool that can recompress the images in the pdf.
-
This post is deleted!