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 a PDF from Qt Creator

Create a PDF from Qt Creator

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 7 Posters 8.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.
  • T Offline
    T Offline
    TheCipo76
    wrote on 16 Nov 2018, 10:31 last edited by TheCipo76
    #1

    Hi,
    i'm trying to create a PDF from Qt Creator

    i've tried with this code (Declarated QPainter and QPrinter):

    printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName("AAA.pdf");
        QPainter painter;
        if (! painter.begin(&printer)) { // failed to open file
            qWarning("failed to open file, is it writable?");
            return 1;
        }
        else {
            msg.setText("Printer Begin");
            msg.exec();
        }
        painter.drawText(10, 10, "Primo PDF Creato!");
        painter.drawText(10, 30, "Test 1");
        if (! printer.newPage()) {
            qWarning("failed in flushing page to disk, disk full?");
            return 1;
        }
        else {
            msg.setText("Printer New Page");
            msg.exec();
        }
        painter.drawText(10, 10, "Test 2");
        painter.end();
    

    i had no errors but no PDF was created..

    i've also tried with this other code (Declarated QPdfWriter and QPainter):

    QFile f("AAA.pdf");
        f.open(QIODevice::WriteOnly);
        QPdfWriter* writer = new QPdfWriter(&f);
        writer->setPageSize(QPagedPaintDevice::A4);
        QPainter* p = new QPainter(writer);
        p->drawText(QRect(100, 100, 2000, 200), "test");
        writer->newPage();
        p->drawText(QRect(100, 100, 2000, 200), "test");
        delete p;
        delete writer;
        f.close();
    

    No errors but..
    No way.. PDF was not created!

    Anyone can tell me where i fail??

    J 1 Reply Last reply 16 Nov 2018, 11:30
    0
    • T TheCipo76
      16 Nov 2018, 10:31

      Hi,
      i'm trying to create a PDF from Qt Creator

      i've tried with this code (Declarated QPainter and QPrinter):

      printer.setOutputFormat(QPrinter::PdfFormat);
          printer.setOutputFileName("AAA.pdf");
          QPainter painter;
          if (! painter.begin(&printer)) { // failed to open file
              qWarning("failed to open file, is it writable?");
              return 1;
          }
          else {
              msg.setText("Printer Begin");
              msg.exec();
          }
          painter.drawText(10, 10, "Primo PDF Creato!");
          painter.drawText(10, 30, "Test 1");
          if (! printer.newPage()) {
              qWarning("failed in flushing page to disk, disk full?");
              return 1;
          }
          else {
              msg.setText("Printer New Page");
              msg.exec();
          }
          painter.drawText(10, 10, "Test 2");
          painter.end();
      

      i had no errors but no PDF was created..

      i've also tried with this other code (Declarated QPdfWriter and QPainter):

      QFile f("AAA.pdf");
          f.open(QIODevice::WriteOnly);
          QPdfWriter* writer = new QPdfWriter(&f);
          writer->setPageSize(QPagedPaintDevice::A4);
          QPainter* p = new QPainter(writer);
          p->drawText(QRect(100, 100, 2000, 200), "test");
          writer->newPage();
          p->drawText(QRect(100, 100, 2000, 200), "test");
          delete p;
          delete writer;
          f.close();
      

      No errors but..
      No way.. PDF was not created!

      Anyone can tell me where i fail??

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 16 Nov 2018, 11:30 last edited by
      #2

      @TheCipo76 Are you sure the PDF file was not created? You're using a relative path, that means the file will be created in current working directory.

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

      T 1 Reply Last reply 16 Nov 2018, 12:09
      1
      • A Offline
        A Offline
        Asperamanca
        wrote on 16 Nov 2018, 11:36 last edited by
        #3

        @TheCipo76 said in Create a PDF from Qt Creator:

        setOutputFileName

        Interesting. I use the QPrinter way, and the only difference to my code I spotted was that I explicitly set the page margins and resolution. Also, I assume that you open the file for writing in your first code just as you do it in the second one, right?

        But try out the Text Edit example (in widgets\richtext) - it contains a PDF export.

        T 1 Reply Last reply 16 Nov 2018, 12:13
        0
        • J jsulm
          16 Nov 2018, 11:30

          @TheCipo76 Are you sure the PDF file was not created? You're using a relative path, that means the file will be created in current working directory.

          T Offline
          T Offline
          TheCipo76
          wrote on 16 Nov 2018, 12:09 last edited by
          #4

          @jsulm i've searhced with finder and this is the result

          0_1542370138910_Schermata 2018-11-16 alle 13.08.09.png

          1 Reply Last reply
          0
          • A Asperamanca
            16 Nov 2018, 11:36

            @TheCipo76 said in Create a PDF from Qt Creator:

            setOutputFileName

            Interesting. I use the QPrinter way, and the only difference to my code I spotted was that I explicitly set the page margins and resolution. Also, I assume that you open the file for writing in your first code just as you do it in the second one, right?

            But try out the Text Edit example (in widgets\richtext) - it contains a PDF export.

            T Offline
            T Offline
            TheCipo76
            wrote on 16 Nov 2018, 12:13 last edited by TheCipo76
            #5

            @Asperamanca
            No, i've followed a Video Tutorial and copy the same code
            (https://www.youtube.com/watch?v=lA5D8b_QPxo)

            the only differences is that i've done it in a Dialog and not in MainWindows

            i've already tried to create a new project and do it in MainWindows but the result is the same..

            No PDf was Created

            M T 2 Replies Last reply 16 Nov 2018, 12:17
            0
            • T TheCipo76
              16 Nov 2018, 12:13

              @Asperamanca
              No, i've followed a Video Tutorial and copy the same code
              (https://www.youtube.com/watch?v=lA5D8b_QPxo)

              the only differences is that i've done it in a Dialog and not in MainWindows

              i've already tried to create a new project and do it in MainWindows but the result is the same..

              No PDf was Created

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 16 Nov 2018, 12:17 last edited by
              #6

              @TheCipo76
              Hi
              Code does look fine.
              Could you try this sample ?
              https://wiki.qt.io/Exporting_a_document_to_PDF
              and see if you get pdf from that ?

              T 1 Reply Last reply 16 Nov 2018, 12:28
              0
              • M mrjj
                16 Nov 2018, 12:17

                @TheCipo76
                Hi
                Code does look fine.
                Could you try this sample ?
                https://wiki.qt.io/Exporting_a_document_to_PDF
                and see if you get pdf from that ?

                T Offline
                T Offline
                TheCipo76
                wrote on 16 Nov 2018, 12:28 last edited by
                #7

                @mrjj Yes,
                with this code PDF was created

                1 Reply Last reply
                0
                • T TheCipo76
                  16 Nov 2018, 12:13

                  @Asperamanca
                  No, i've followed a Video Tutorial and copy the same code
                  (https://www.youtube.com/watch?v=lA5D8b_QPxo)

                  the only differences is that i've done it in a Dialog and not in MainWindows

                  i've already tried to create a new project and do it in MainWindows but the result is the same..

                  No PDf was Created

                  T Offline
                  T Offline
                  TheCipo76
                  wrote on 16 Nov 2018, 15:46 last edited by
                  #8

                  @TheCipo76

                  when i launch pdf print in Application Output i saw this message:

                  QPainter::begin(): Returned false
                  QPainter::setPen: Painter not active
                  QPainter::end: Painter not active, aborted

                  Any suggestion to fix it??

                  M 1 Reply Last reply 16 Nov 2018, 16:12
                  0
                  • T TheCipo76
                    16 Nov 2018, 15:46

                    @TheCipo76

                    when i launch pdf print in Application Output i saw this message:

                    QPainter::begin(): Returned false
                    QPainter::setPen: Painter not active
                    QPainter::end: Painter not active, aborted

                    Any suggestion to fix it??

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 16 Nov 2018, 16:12 last edited by
                    #9

                    @TheCipo76
                    Hi
                    My guess would be that printer object goes out of scope ?

                    T 1 Reply Last reply 16 Nov 2018, 16:20
                    0
                    • M mrjj
                      16 Nov 2018, 16:12

                      @TheCipo76
                      Hi
                      My guess would be that printer object goes out of scope ?

                      T Offline
                      T Offline
                      TheCipo76
                      wrote on 16 Nov 2018, 16:20 last edited by TheCipo76
                      #10

                      @mrjj i've tried with this code:

                      qreal top=20, left=15, right=15, bottom=20;
                          QPrinter printer(QPrinter::PrinterResolution);
                          QPrintDialog dialog(&printer, this);
                          if(dialog.exec() == QDialog::Accepted){
                              QPrinterInfo pinfo(printer);
                              qDebug() << "Printer valid: " << printer.isValid();
                              qDebug() << "Printer Name: " << printer.printerName();
                              qDebug() << "Printer program: " << printer.printProgram();
                              qDebug() << "Is printer null: " << pinfo.isNull();
                              qDebug() << "Printer State: " << pinfo.state();
                              qDebug() << "Is printer default: " << pinfo.isDefault();
                              qDebug() << "Is printer remote: " << pinfo.isRemote();
                          }
                          printer.setPaperSize(QPrinter::A4);
                          printer.setPageMargins(left, top, right, bottom, QPrinter::Millimeter);
                          printer.setOutputFormat(QPrinter::PdfFormat);
                          printer.setOutputFileName("PrimoPDF.pdf");
                      
                          QPainter painter;
                          if (! painter.begin(&printer)) { // failed to open file
                              qWarning("failed to open file, is it writable?");
                              return;
                          }
                          painter.drawText(10, 10, "Primo PDF Creato!");
                          painter.drawText(10, 30, "Test 1");
                          if (! printer.newPage()) {
                              qWarning("failed in flushing page to disk, disk full?");
                              return;
                          }
                          painter.drawText(10, 10, "Test 2");
                          painter.end();
                      

                      and this is the result:

                      Printer valid: true
                      Printer Name: "Samsung_CLP_620_Series__SEC001599489B1A_"
                      Printer program: ""
                      Is printer null: false
                      Printer State: 0
                      Is printer default: true
                      Is printer remote: false

                      Printer seem to be OK

                      now the previous message about QPainter were not showed..

                      M 1 Reply Last reply 16 Nov 2018, 16:23
                      0
                      • T TheCipo76
                        16 Nov 2018, 16:20

                        @mrjj i've tried with this code:

                        qreal top=20, left=15, right=15, bottom=20;
                            QPrinter printer(QPrinter::PrinterResolution);
                            QPrintDialog dialog(&printer, this);
                            if(dialog.exec() == QDialog::Accepted){
                                QPrinterInfo pinfo(printer);
                                qDebug() << "Printer valid: " << printer.isValid();
                                qDebug() << "Printer Name: " << printer.printerName();
                                qDebug() << "Printer program: " << printer.printProgram();
                                qDebug() << "Is printer null: " << pinfo.isNull();
                                qDebug() << "Printer State: " << pinfo.state();
                                qDebug() << "Is printer default: " << pinfo.isDefault();
                                qDebug() << "Is printer remote: " << pinfo.isRemote();
                            }
                            printer.setPaperSize(QPrinter::A4);
                            printer.setPageMargins(left, top, right, bottom, QPrinter::Millimeter);
                            printer.setOutputFormat(QPrinter::PdfFormat);
                            printer.setOutputFileName("PrimoPDF.pdf");
                        
                            QPainter painter;
                            if (! painter.begin(&printer)) { // failed to open file
                                qWarning("failed to open file, is it writable?");
                                return;
                            }
                            painter.drawText(10, 10, "Primo PDF Creato!");
                            painter.drawText(10, 30, "Test 1");
                            if (! printer.newPage()) {
                                qWarning("failed in flushing page to disk, disk full?");
                                return;
                            }
                            painter.drawText(10, 10, "Test 2");
                            painter.end();
                        

                        and this is the result:

                        Printer valid: true
                        Printer Name: "Samsung_CLP_620_Series__SEC001599489B1A_"
                        Printer program: ""
                        Is printer null: false
                        Printer State: 0
                        Is printer default: true
                        Is printer remote: false

                        Printer seem to be OK

                        now the previous message about QPainter were not showed..

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 16 Nov 2018, 16:23 last edited by
                        #11

                        @TheCipo76
                        That looks very ok.
                        But no PDF (PrimoPDF.pdf) is produced ?

                        Have a look at this code. it prints screen shots.
                        Looks very similar to yours.

                        void printv2(QPixmap& pix) {
                        
                          QPrinter printer(QPrinter::HighResolution);
                          printer.setOrientation(QPrinter::Landscape);
                          printer.setOutputFormat(QPrinter::PdfFormat);
                          printer.setPaperSize(QPrinter::A4);
                          printer.setOutputFileName("e:/test.pdf");
                        
                          QPainter painter;
                          painter.begin(&printer);
                        
                          double xscale = printer.pageRect().width() / double(pix.width());
                          double yscale = printer.pageRect().height() / double(pix.height());
                          double scale = qMin(xscale, yscale);
                          painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                                            printer.paperRect().y() + printer.pageRect().height() / 2);
                          painter.scale(scale, scale);
                          painter.translate(-pix.width() / 2, -pix.height() / 2);
                          painter.drawPixmap(0, 0, pix);
                          painter.end();
                        
                        }
                        
                        
                        void MainWindow::on_pushButton_released() {
                        
                          QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
                          printv2(pix);
                        
                        }
                        
                        
                        T 1 Reply Last reply 16 Nov 2018, 16:28
                        0
                        • M mrjj
                          16 Nov 2018, 16:23

                          @TheCipo76
                          That looks very ok.
                          But no PDF (PrimoPDF.pdf) is produced ?

                          Have a look at this code. it prints screen shots.
                          Looks very similar to yours.

                          void printv2(QPixmap& pix) {
                          
                            QPrinter printer(QPrinter::HighResolution);
                            printer.setOrientation(QPrinter::Landscape);
                            printer.setOutputFormat(QPrinter::PdfFormat);
                            printer.setPaperSize(QPrinter::A4);
                            printer.setOutputFileName("e:/test.pdf");
                          
                            QPainter painter;
                            painter.begin(&printer);
                          
                            double xscale = printer.pageRect().width() / double(pix.width());
                            double yscale = printer.pageRect().height() / double(pix.height());
                            double scale = qMin(xscale, yscale);
                            painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                                              printer.paperRect().y() + printer.pageRect().height() / 2);
                            painter.scale(scale, scale);
                            painter.translate(-pix.width() / 2, -pix.height() / 2);
                            painter.drawPixmap(0, 0, pix);
                            painter.end();
                          
                          }
                          
                          
                          void MainWindow::on_pushButton_released() {
                          
                            QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
                            printv2(pix);
                          
                          }
                          
                          
                          T Offline
                          T Offline
                          TheCipo76
                          wrote on 16 Nov 2018, 16:28 last edited by
                          #12

                          @mrjj No, "PrimoPDF.pdf" wasn't created..

                          the code you have posted is very similar..

                          M 1 Reply Last reply 16 Nov 2018, 16:32
                          0
                          • T TheCipo76
                            16 Nov 2018, 16:28

                            @mrjj No, "PrimoPDF.pdf" wasn't created..

                            the code you have posted is very similar..

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 16 Nov 2018, 16:32 last edited by mrjj
                            #13

                            @TheCipo76
                            Yep and i get a e:/test.pdf so not really sure why you dont get any.
                            does it try to print to real printer ?

                            Can you try

                            void printv3() {
                            
                              QPrinter printer(QPrinter::HighResolution);
                              printer.setOrientation(QPrinter::Landscape);
                              printer.setOutputFormat(QPrinter::PdfFormat);
                              printer.setPaperSize(QPrinter::A4);
                              printer.setOutputFileName("test.pdf");
                            
                              QPainter painter;
                              painter.begin(&printer);
                            
                              painter.drawText( 200,200, "HELLO" );
                              painter.end();
                            
                            }
                            
                            

                            That give me a test.pdf in the build folder
                            alt text

                            and expected result
                            alt text

                            T 1 Reply Last reply 16 Nov 2018, 16:44
                            0
                            • M mrjj
                              16 Nov 2018, 16:32

                              @TheCipo76
                              Yep and i get a e:/test.pdf so not really sure why you dont get any.
                              does it try to print to real printer ?

                              Can you try

                              void printv3() {
                              
                                QPrinter printer(QPrinter::HighResolution);
                                printer.setOrientation(QPrinter::Landscape);
                                printer.setOutputFormat(QPrinter::PdfFormat);
                                printer.setPaperSize(QPrinter::A4);
                                printer.setOutputFileName("test.pdf");
                              
                                QPainter painter;
                                painter.begin(&printer);
                              
                                painter.drawText( 200,200, "HELLO" );
                                painter.end();
                              
                              }
                              
                              

                              That give me a test.pdf in the build folder
                              alt text

                              and expected result
                              alt text

                              T Offline
                              T Offline
                              TheCipo76
                              wrote on 16 Nov 2018, 16:44 last edited by
                              #14

                              @mrjj i've tried with no luck

                              test.pdf was not created..

                              M 1 Reply Last reply 16 Nov 2018, 16:48
                              0
                              • T TheCipo76
                                16 Nov 2018, 16:44

                                @mrjj i've tried with no luck

                                test.pdf was not created..

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 16 Nov 2018, 16:48 last edited by
                                #15

                                @TheCipo76
                                Really odd. I cant guess sorry. Sample works here.
                                You use the code directly as is ?

                                T 1 Reply Last reply 16 Nov 2018, 16:49
                                0
                                • M mrjj
                                  16 Nov 2018, 16:48

                                  @TheCipo76
                                  Really odd. I cant guess sorry. Sample works here.
                                  You use the code directly as is ?

                                  T Offline
                                  T Offline
                                  TheCipo76
                                  wrote on 16 Nov 2018, 16:49 last edited by TheCipo76
                                  #16

                                  @mrjj Yes, copied and pasted

                                  QPrinter printer(QPrinter::HighResolution);
                                        printer.setOrientation(QPrinter::Landscape);
                                        printer.setOutputFormat(QPrinter::PdfFormat);
                                        printer.setPaperSize(QPrinter::A4);
                                        printer.setOutputFileName("test.pdf");
                                  
                                        QPainter painter;
                                        painter.begin(&printer);
                                  
                                        painter.drawText( 200,200, "HELLO" );
                                        painter.end();
                                  
                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 16 Nov 2018, 16:50 last edited by
                                    #17

                                    and you also searched for test.pdf to make sure
                                    its not created any where else ?

                                    T 1 Reply Last reply 16 Nov 2018, 16:51
                                    0
                                    • M mrjj
                                      16 Nov 2018, 16:50

                                      and you also searched for test.pdf to make sure
                                      its not created any where else ?

                                      T Offline
                                      T Offline
                                      TheCipo76
                                      wrote on 16 Nov 2018, 16:51 last edited by
                                      #18

                                      @mrjj 0_1542387108242_Schermata 2018-11-16 alle 17.51.36.png

                                      M 1 Reply Last reply 16 Nov 2018, 16:55
                                      0
                                      • T TheCipo76
                                        16 Nov 2018, 16:51

                                        @mrjj 0_1542387108242_Schermata 2018-11-16 alle 17.51.36.png

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 16 Nov 2018, 16:55 last edited by
                                        #19

                                        @TheCipo76
                                        Very odd.
                                        Can you try this code. it let u select save location
                                        if that dont work. something up with your Qt.

                                        void printv4() {
                                          QPrinter printer(QPrinter::HighResolution);
                                          printer.setOrientation(QPrinter::Landscape);
                                          printer.setOutputFormat(QPrinter::PdfFormat);
                                          printer.setPaperSize(QPrinter::A4);
                                          QString fileName = QFileDialog::getSaveFileName(0,"file", "c:\\", "pdf (*.pdf)");
                                          printer.setOutputFileName(fileName);
                                        
                                          QPainter painter;
                                          painter.begin(&printer);
                                        
                                          painter.drawText( 200,200, "HELLO" );
                                          painter.end();
                                        
                                        }
                                        
                                        T 1 Reply Last reply 16 Nov 2018, 16:59
                                        2
                                        • M mrjj
                                          16 Nov 2018, 16:55

                                          @TheCipo76
                                          Very odd.
                                          Can you try this code. it let u select save location
                                          if that dont work. something up with your Qt.

                                          void printv4() {
                                            QPrinter printer(QPrinter::HighResolution);
                                            printer.setOrientation(QPrinter::Landscape);
                                            printer.setOutputFormat(QPrinter::PdfFormat);
                                            printer.setPaperSize(QPrinter::A4);
                                            QString fileName = QFileDialog::getSaveFileName(0,"file", "c:\\", "pdf (*.pdf)");
                                            printer.setOutputFileName(fileName);
                                          
                                            QPainter painter;
                                            painter.begin(&printer);
                                          
                                            painter.drawText( 200,200, "HELLO" );
                                            painter.end();
                                          
                                          }
                                          
                                          T Offline
                                          T Offline
                                          TheCipo76
                                          wrote on 16 Nov 2018, 16:59 last edited by
                                          #20

                                          @mrjj

                                          0_1542387477878_Schermata 2018-11-16 alle 17.57.19.png 0_1542387485333_Schermata 2018-11-16 alle 17.57.30.png

                                          Finally works!!!!!! Great!!!
                                          I don't know why but Works!!!!

                                          Thank you very much!!

                                          T 1 Reply Last reply 16 Nov 2018, 17:02
                                          1

                                          9/26

                                          16 Nov 2018, 16:12

                                          topic:navigator.unread, 17
                                          • Login

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