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. QPrinter + QPainter writes invalid PDF file
Forum Updated to NodeBB v4.3 + New Features

QPrinter + QPainter writes invalid PDF file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 553 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    Jason Smith
    wrote on last edited by Jason Smith
    #1

    I am simply trying to draw a rectangle to a PDF file using QPrinter + QPainter:

    #include <QtWidgets>
    #include <QPrinter>
    
    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
        QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFileName("/Users/jason/Desktop/example.pdf");
        printer.setOutputFormat(QPrinter::PdfFormat);
    
        QPainter painter;
        painter.begin(&printer);
        int width = painter.viewport().width();
        int height = painter.viewport().height();
        painter.setPen(Qt::black);
        painter.drawRect(0.25*width, 0.25*height, 0.5*width, 0.5*height);
        painter.end();
    }
    

    Using MacOS 10.15.4 and Qt 5.15.2 this results in a blank/invalid PDF file. The same code without the QPrinter.setOutputFormat and QPrinter.setOutputFileName correctly prints a rectangle on paper.

    How can I using QPrinter/QPdfWriter + QPainter to draw to a PDF file?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      For QPdfWriter, just use QPainter on it directly.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        For QPdfWriter, just use QPainter on it directly.

        J Offline
        J Offline
        Jason Smith
        wrote on last edited by
        #3

        @SGaist Thanks, but I get the same invalid/blank PDF when using

        QPdfWriter pdfWriter("/Users/jason/Desktop/example.pdf");
        QPainter painter;
        painter.begin(&pdfWriter);
        ...
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          In what way is it invalid ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • J Offline
            J Offline
            Jason Smith
            wrote on last edited by
            #5

            I am a fool. I thought the PDF was blank, but it turns out if I zoom in really really far, I can see a faint grey line. Apparently the resolution of a PDF is much higher than that of a printer!

            Using painter.setPen(QPen(QBrush(Qt::red), 100.0)); shows a clear rectangle as expected both using QPrinter and QPdfWriter.

            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