Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Printing QImage to PDF
Forum Update on Monday, May 27th 2025

Printing QImage to PDF

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 1 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pantxo
    wrote on last edited by
    #1

    Hi,

    I use Qt for printing some graphics to PDF. Unfortunately, some QImages are not rendered correctly, I don't get the expected pixel colours.

    I would expect the following code (adapted from this example https://wiki.qt.io/Exporting_a_document_to_PDF) to print a vertical image composed of 3 pixels : blue, green and red from top to bottom. Instead I get 3 pixels blue(Ok), light greenish, greenish (see attached pdf [0_1535805235367_toto.pdf](Uploading 100%) )

    #include <QApplication>
    #include <QtCore>
    #include <QPrinter>
    #include <QImage>
    #include <QPrinter>
    #include <QPixmap>
    #include <QPainter>
    #include <QFileDialog>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
     
        QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Export PDF", QString(), "*.pdf");
        if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); }
    
        QPrinter printer(QPrinter::PrinterResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setPaperSize(QPrinter::A4);
        printer.setOutputFileName(fileName);
    
        QPainter painter;
        painter.begin (&printer);
        
        QImage img (1, 3, QImage::Format_ARGB32);
        img.setPixelColor (0,0,QColor (Qt::red));
        img.setPixelColor (0,1,QColor (Qt::green));
        img.setPixelColor (0,2,QColor (Qt::blue));
    
        
        // Increase the apparent size of the image
        QTransform tform(55.8,0,0,-55.8,74.88,236.34);
        painter.setTransform (tform);
        
        painter.drawImage (QRect (0,0,1, 3), img);
        
        painter.end ();
        
    }
    

    What am I doing wrong?

    Thanks,

    Pantxo

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pantxo
      wrote on last edited by
      #2

      Oh, and if I modify the original example (see below) to print a 6 pixels image (red, red, green, green, blue, blue), then I get a better result but note that the two blue pixels don't have the exact same colour (see pdf attached)[0_1535807541749_toto.pdf](Uploading 100%) .

      #include <QApplication>
      #include <QtCore>
      #include <QPrinter>
      #include <QImage>
      #include <QPrinter>
      #include <QPixmap>
      #include <QPainter>
      #include <QTextDocument>
      #include <QFileDialog>
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
       
          QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Export PDF", QString(), "*.pdf");
          if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); }
      
          QPrinter printer(QPrinter::PrinterResolution);
          printer.setOutputFormat(QPrinter::PdfFormat);
          printer.setPaperSize(QPrinter::A4);
          printer.setOutputFileName(fileName);
      
          QPainter painter;
          painter.begin (&printer);
          
          QImage img (1, 6, QImage::Format_ARGB32);
          img.setPixelColor (0,0,QColor (Qt::red));
          img.setPixelColor (0,1,QColor (Qt::red));
          img.setPixelColor (0,2,QColor (Qt::green));
          img.setPixelColor (0,3,QColor (Qt::green));
          img.setPixelColor (0,4,QColor (Qt::blue));
          img.setPixelColor (0,5,QColor (Qt::blue));
      
          
          // Increase the apparent size of the image
          QTransform tform(58.68,0,0,-58.68,268.74,384.48);
          painter.setTransform (tform);
          
          painter.drawImage (QRect (0,0,1, 6), img);
          
          painter.end ();
          
      }
      
      

      In case it may be relevant I use Qt 5.9 on linux Mint 19.

      Thanks

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pantxo
        wrote on last edited by
        #3

        Nevermind, I found out that this is an already reported bug :

        https://bugreports.qt.io/browse/QTBUG-54809

        Pantxo

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved