View Full Version : [SOLVED] Using QPainter to Import JPG for PDF Report

20th October 2014, 16:20
Qt Version: 5.3.1
OS: CentOS 6.5

I'm attempting to create a PDF report using QPainter.
This seems to work better than using QTextDocument.
However, I am faced with an issue involving certain graphics files when I attempt to load them.

I have a JPEG file that contains a company logo (attached). When loaded in the GNOME image viewer, the tool indicates the image is 170 pixels x 80 pixels.

I have attempted to use QPixmap and QImage as the mechanism for loading and putting the image into the QPainter object that then dump to PDF.

Here is the issue:
The QPixmap method produces an image that is extremely small in the PDF file. If I attempt to scale it up, the image quality becomes very poor.
The QImage method produces no image in the PDF file.

Here is the code:

#include <QApplication>
#include <QPdfWriter>
#include <QPainter>

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

QPdfWriter pdfWriter("my.pdf");
QPainter painter(&pdfWriter);
quint32 iYPos = 10;

QPixmap pxPic;
pxPic.load("/usr/local/SMART/public_html/images/logo.jpg", "JPG");
painter.drawPixmap(0, iYPos, pxPic.width(), pxPic.height(), pxPic);
iYPos += pxPic.height() + 250;

QImage imgPic;
imgPic.load("/usr/local/SMART/public_html/images/logo.jpg", "JPG");
painter.drawImage(0, iYPos, imgPic, imgPic.width(), imgPic.height());

return 0;

I would appreciate any input as to how to make this work.


20th October 2014, 21:34
Sounds like QPdfWriter is using a page size in pixels at 300 or higher DPI, in which case the pixmap is going to be mapped 1 for 1 to output pixels. What do QPdfWriter::width() and QPdfWriter::height() report?

If that's the case, then you'll need a pixmap scaled to the pixel size you need to map to the size needed for the resolution of the QPdfWriter.

20th October 2014, 21:59
PDFWriter.width() = 9583
PDFWriter.height() = 13699


Added after 18 minutes:

The scaling concept helped.
Here's how I got a logo that looks appealing.
I do not want the logo to consume more than a third of the width of the page nor a fifth of height.
So I scaled using these values:

// Scaled images
quint32 iWidth = pdfWriter.width();
quint32 iHeight = pdfWriter.height();
QSize s(iWidth/3, iHeight/5);
QPixmap pxScaledPic = pxPic.scaled(s, Qt::KeepAspectRatio, Qt::FastTransformation);
painter.drawPixmap(0, iYPos, pxScaledPic.width(), pxScaledPic.height(), pxScaledPic);
iYPos += pxScaledPic.height() + 250;

Thanks for the help.


20th October 2014, 22:11
In general, for good quality print output you need much larger images that render at the right physical size at the print resolution (or that you scale down). So, for example, at 300DPI you need a 600x300 pixel image to get a 2x1 inch image on paper.

What is the output like if you use the original pixmap and specify the drawPixmap() width and height in painter cordinates? These look like they might be 300 DPI or thereabouts.

Do you get the QImage result if you call QPainetr::end() before you exit the program?

20th October 2014, 22:32
Unfortunately I will not have control over the size of the image.
I can only make suggestions to the customer as to what will look best.
I will include comments similar to yours in the documentation.

The output of drawPixMap with adjusted width and height arguments does not look as good as the scaled QPixmap.

Putting painter.end() at the bottom of the program eliminated all the images; QPixmap and QImage.