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. Create and write into a PDF File

Create and write into a PDF File

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 8 Posters 21.2k 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.
  • M Offline
    M Offline
    Marcel92
    wrote on 15 May 2017, 15:18 last edited by
    #1

    I know this topic has been discussed a million times, but i dont understand why it isnt working for me.
    I want to create and write into a PDF file, when i click on a button.

    void MainWindow::on_button_clicked()
    {
    QPrinter printer2;
    printer2.setOutputFormat(QPrinter::PdfFormat);
    printer2.setOutputFileName("test.pdf");
    QPainter painter2;
    painter2.drawText(10,10, "Test");
    painter2.end();
    }

    This is just one of many different approaches i tried.
    The problem is, QT doesnt create any file.

    Thanks in advance!

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eddy
      wrote on 15 May 2017, 15:36 last edited by
      #2

      Hi,

      do you have

      QT += printsupport
      

      in your pro file?

      Qt Certified Specialist
      www.edalsolutions.be

      M 1 Reply Last reply 15 May 2017, 17:22
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 15 May 2017, 16:03 last edited by
        #3

        Hi! Here's a working example.

        1 Reply Last reply
        3
        • E Eddy
          15 May 2017, 15:36

          Hi,

          do you have

          QT += printsupport
          

          in your pro file?

          M Offline
          M Offline
          Marcel92
          wrote on 15 May 2017, 17:22 last edited by
          #4

          @Eddy Yes.

          @Wieland

          Thanks for your answers.
          I am not sure about the arguments in painter.drawText().
          When i run the programm and press the button, QT says "QPainter::begin(): Returned false
          Could you help me with that?

          void MainWindow::writePdf()
          {
          const QString filename("D://Programme/QT/MeineProjekte/Projekt/test.pdf");

          QString testData = "test";
          QPdfWriter pdfwriter(filename);
          pdfwriter.setPageSize(QPageSize(QPageSize::A4));
          QPainter painter(&pdfwriter);
          painter.drawText(0,0, testData);
          

          }

          1 Reply Last reply
          0
          • H Online
            H Online
            hskoglund
            wrote on 15 May 2017, 17:29 last edited by
            #5

            Hi, it seems to be 2 slashes after D:, maybe try
            ``const QString filename("`D:/Programme/QT/MeineProjekte/Projekt/test.pdf");```

            M 2 Replies Last reply 15 May 2017, 17:35
            3
            • H hskoglund
              15 May 2017, 17:29

              Hi, it seems to be 2 slashes after D:, maybe try
              ``const QString filename("`D:/Programme/QT/MeineProjekte/Projekt/test.pdf");```

              M Offline
              M Offline
              Marcel92
              wrote on 15 May 2017, 17:35 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • H hskoglund
                15 May 2017, 17:29

                Hi, it seems to be 2 slashes after D:, maybe try
                ``const QString filename("`D:/Programme/QT/MeineProjekte/Projekt/test.pdf");```

                M Offline
                M Offline
                Marcel92
                wrote on 15 May 2017, 17:40 last edited by
                #7

                @hskoglund

                Thanks a lot. It works now.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  drich
                  wrote on 12 Jul 2017, 11:54 last edited by
                  #8

                  Hi I want to create a program that will create a pdf file with a logo/image to it. using a QPrintPreviewDialog, QPixmap, QPrint, QDialog.

                  M 1 Reply Last reply 12 Jul 2017, 12:00
                  0
                  • D drich
                    12 Jul 2017, 11:54

                    Hi I want to create a program that will create a pdf file with a logo/image to it. using a QPrintPreviewDialog, QPixmap, QPrint, QDialog.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 12 Jul 2017, 12:00 last edited by
                    #9

                    @drich
                    hi
                    Its same concept.
                    Just use
                    painter2.drawPixmap instead of drawText

                    D 1 Reply Last reply 12 Jul 2017, 12:02
                    1
                    • M mrjj
                      12 Jul 2017, 12:00

                      @drich
                      hi
                      Its same concept.
                      Just use
                      painter2.drawPixmap instead of drawText

                      D Offline
                      D Offline
                      drich
                      wrote on 12 Jul 2017, 12:02 last edited by
                      #10

                      @mrjj
                      thanks
                      I still having error.
                      This is my code
                      void MainWindow::on_pushButton_createpdf_clicked()
                      {
                      QPrintPreviewDialog dialog;
                      QPainter painter(this);
                      connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
                      dialog.exec();
                      painter.setPen(Qt::black);
                      painter.setFont(QFont("Arial", 30));
                      painter.drawText(rect(), Qt::AlignCenter, "Technological University of the Philipines - Manila");
                      painter.end();
                      painter.drawPixmap (C:\User\Downloads\logo.png);
                      }

                      artwawA jsulmJ 2 Replies Last reply 12 Jul 2017, 12:09
                      0
                      • D drich
                        12 Jul 2017, 12:02

                        @mrjj
                        thanks
                        I still having error.
                        This is my code
                        void MainWindow::on_pushButton_createpdf_clicked()
                        {
                        QPrintPreviewDialog dialog;
                        QPainter painter(this);
                        connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
                        dialog.exec();
                        painter.setPen(Qt::black);
                        painter.setFont(QFont("Arial", 30));
                        painter.drawText(rect(), Qt::AlignCenter, "Technological University of the Philipines - Manila");
                        painter.end();
                        painter.drawPixmap (C:\User\Downloads\logo.png);
                        }

                        artwawA Offline
                        artwawA Offline
                        artwaw
                        wrote on 12 Jul 2017, 12:09 last edited by
                        #11

                        @drich Hi,
                        what kind of an error?
                        Have you read the QPainter documentation?

                        For more information please re-read.

                        Kind Regards,
                        Artur

                        D 1 Reply Last reply 12 Jul 2017, 12:19
                        1
                        • D drich
                          12 Jul 2017, 12:02

                          @mrjj
                          thanks
                          I still having error.
                          This is my code
                          void MainWindow::on_pushButton_createpdf_clicked()
                          {
                          QPrintPreviewDialog dialog;
                          QPainter painter(this);
                          connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
                          dialog.exec();
                          painter.setPen(Qt::black);
                          painter.setFont(QFont("Arial", 30));
                          painter.drawText(rect(), Qt::AlignCenter, "Technological University of the Philipines - Manila");
                          painter.end();
                          painter.drawPixmap (C:\User\Downloads\logo.png);
                          }

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on 12 Jul 2017, 12:13 last edited by
                          #12

                          @drich said in Create and write into a PDF File:

                          painter.end();
                          painter.drawPixmap (C:\User\Downloads\logo.png);

                          I guess

                          painter.end();
                          

                          should be last line and

                          painter.drawPixmap (C:\User\Downloads\logo.png);
                          

                          is not valid C++. Should be:

                          painter.drawPixmap ("C:/User/Downloads/logo.png");

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          D 1 Reply Last reply 12 Jul 2017, 12:14
                          0
                          • jsulmJ jsulm
                            12 Jul 2017, 12:13

                            @drich said in Create and write into a PDF File:

                            painter.end();
                            painter.drawPixmap (C:\User\Downloads\logo.png);

                            I guess

                            painter.end();
                            

                            should be last line and

                            painter.drawPixmap (C:\User\Downloads\logo.png);
                            

                            is not valid C++. Should be:

                            painter.drawPixmap ("C:/User/Downloads/logo.png");
                            D Offline
                            D Offline
                            drich
                            wrote on 12 Jul 2017, 12:14 last edited by
                            #13

                            @jsulm ahh okie thanks. I try it.

                            1 Reply Last reply
                            0
                            • artwawA artwaw
                              12 Jul 2017, 12:09

                              @drich Hi,
                              what kind of an error?
                              Have you read the QPainter documentation?

                              D Offline
                              D Offline
                              drich
                              wrote on 12 Jul 2017, 12:19 last edited by
                              #14

                              @artwaw it says error: no matching function for call to 'QPainter::drawPixmap(const char [52])'

                              artwawA 1 Reply Last reply 12 Jul 2017, 12:20
                              0
                              • D drich
                                12 Jul 2017, 12:19

                                @artwaw it says error: no matching function for call to 'QPainter::drawPixmap(const char [52])'

                                artwawA Offline
                                artwawA Offline
                                artwaw
                                wrote on 12 Jul 2017, 12:20 last edited by
                                #15

                                @drich That's the problem @jsulm addressed in his reply.

                                For more information please re-read.

                                Kind Regards,
                                Artur

                                D 2 Replies Last reply 12 Jul 2017, 12:25
                                0
                                • artwawA artwaw
                                  12 Jul 2017, 12:20

                                  @drich That's the problem @jsulm addressed in his reply.

                                  D Offline
                                  D Offline
                                  drich
                                  wrote on 12 Jul 2017, 12:25 last edited by
                                  #16

                                  @artwaw in the painter.drawPixmap

                                  drawPixmap was underlined and it note "Too few arguments"

                                  what does it means?

                                  artwawA 1 Reply Last reply 12 Jul 2017, 12:30
                                  0
                                  • artwawA artwaw
                                    12 Jul 2017, 12:20

                                    @drich That's the problem @jsulm addressed in his reply.

                                    D Offline
                                    D Offline
                                    drich
                                    wrote on 12 Jul 2017, 12:30 last edited by
                                    #17

                                    @artwaw hi, I remove the painter.Pixmap and I run my program I clicked the createpdf button and it show a blank paper it didn't show this
                                    painter.setPen(Qt::black);
                                    painter.setFont(QFont("Arial", 30));
                                    painter.drawText(rect(), Qt::AlignCenter, "Technological University of the Philipines - Manila");

                                    artwawA 1 Reply Last reply 12 Jul 2017, 12:34
                                    0
                                    • D drich
                                      12 Jul 2017, 12:25

                                      @artwaw in the painter.drawPixmap

                                      drawPixmap was underlined and it note "Too few arguments"

                                      what does it means?

                                      artwawA Offline
                                      artwawA Offline
                                      artwaw
                                      wrote on 12 Jul 2017, 12:30 last edited by
                                      #18

                                      @drich That means that you passed too few arguments to the method. Please refer to the documentation I linked in one of my previous posts - in that webpage together with a quite detailed overview on how to use QPainter there is also a list of the methods provided. You will see that QPainter::drawPixmap comes in many variants with different arguments list so you can choose the one that best suits your needs.
                                      It seems to me that taking a look at Paint System would also bring you some significant benefits in terms of overall understanding how to accomplish what you aim for.

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      D 1 Reply Last reply 12 Jul 2017, 12:39
                                      2
                                      • D drich
                                        12 Jul 2017, 12:30

                                        @artwaw hi, I remove the painter.Pixmap and I run my program I clicked the createpdf button and it show a blank paper it didn't show this
                                        painter.setPen(Qt::black);
                                        painter.setFont(QFont("Arial", 30));
                                        painter.drawText(rect(), Qt::AlignCenter, "Technological University of the Philipines - Manila");

                                        artwawA Offline
                                        artwawA Offline
                                        artwaw
                                        wrote on 12 Jul 2017, 12:34 last edited by
                                        #19

                                        @drich I think that is because you have not had supplied a valid rectangle area which tells QPainter::drawText() where to draw. Please refer to the documentation I linked above to learn how to draw on widgets. It would be more helpful and consistent than explaining every detail in here.

                                        For more information please re-read.

                                        Kind Regards,
                                        Artur

                                        1 Reply Last reply
                                        0
                                        • artwawA artwaw
                                          12 Jul 2017, 12:30

                                          @drich That means that you passed too few arguments to the method. Please refer to the documentation I linked in one of my previous posts - in that webpage together with a quite detailed overview on how to use QPainter there is also a list of the methods provided. You will see that QPainter::drawPixmap comes in many variants with different arguments list so you can choose the one that best suits your needs.
                                          It seems to me that taking a look at Paint System would also bring you some significant benefits in terms of overall understanding how to accomplish what you aim for.

                                          D Offline
                                          D Offline
                                          drich
                                          wrote on 12 Jul 2017, 12:39 last edited by
                                          #20

                                          @artwaw Now I understand Thank you very much! :)

                                          artwawA 1 Reply Last reply 12 Jul 2017, 12:41
                                          0

                                          • Login

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