Results 1 to 5 of 5

Thread: [SOLVED] Using QPainter to Import JPG for PDF Report

  1. #1
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default [SOLVED] Using QPainter to Import JPG for PDF Report

    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:
    Qt Code:
    1. #include <QApplication>
    2. #include <QPdfWriter>
    3. #include <QPainter>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QPdfWriter pdfWriter("my.pdf");
    10. QPainter painter(&pdfWriter);
    11. quint32 iYPos = 10;
    12.  
    13. QPixmap pxPic;
    14. pxPic.load("/usr/local/SMART/public_html/images/logo.jpg", "JPG");
    15. painter.drawPixmap(0, iYPos, pxPic.width(), pxPic.height(), pxPic);
    16. iYPos += pxPic.height() + 250;
    17.  
    18. QImage imgPic;
    19. imgPic.load("/usr/local/SMART/public_html/images/logo.jpg", "JPG");
    20. painter.drawImage(0, iYPos, imgPic, imgPic.width(), imgPic.height());
    21.  
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 

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

    Karl
    Attached Images Attached Images
    Last edited by KaptainKarl; 20th October 2014 at 21:01. Reason: Problem is solved. Wanted to reflect that in the title.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using QPainter to Import JPG for PDF Report

    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.

  3. The following user says thank you to d_stranz for this useful post:

    KaptainKarl (20th October 2014)

  4. #3
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: [SOLVED] Using QPainter to Import JPG for PDF Report

    PDFWriter.width() = 9583
    PDFWriter.height() = 13699

    Karl


    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:

    Qt Code:
    1. // Scaled images
    2. quint32 iWidth = pdfWriter.width();
    3. quint32 iHeight = pdfWriter.height();
    4. QSize s(iWidth/3, iHeight/5);
    5. QPixmap pxScaledPic = pxPic.scaled(s, Qt::KeepAspectRatio, Qt::FastTransformation);
    6. painter.drawPixmap(0, iYPos, pxScaledPic.width(), pxScaledPic.height(), pxScaledPic);
    7. iYPos += pxScaledPic.height() + 250;
    To copy to clipboard, switch view to plain text mode 

    Thanks for the help.

    Karl
    Last edited by KaptainKarl; 20th October 2014 at 20:59.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [SOLVED] Using QPainter to Import JPG for PDF Report

    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?

  6. #5
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: [SOLVED] Using QPainter to Import JPG for PDF Report

    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.

    Karl

Similar Threads

  1. Report library for QT
    By blackliteon in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2014, 08:23
  2. Replies: 0
    Last Post: 17th November 2009, 20:59
  3. QT report
    By triperzz in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2008, 17:58
  4. Crash report
    By Lele in forum Qt Programming
    Replies: 6
    Last Post: 10th October 2007, 09:50
  5. error report
    By Rekha in forum Newbie
    Replies: 1
    Last Post: 14th August 2006, 12:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.