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. make a beautiful pdf with qt
Forum Updated to NodeBB v4.3 + New Features

make a beautiful pdf with qt

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 3.5k Views 4 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.
  • AlbatorA Offline
    AlbatorA Offline
    Albator
    wrote on last edited by
    #10

    to take the whole qtablewidget with a lot of rows and columns, just go to the .ui and change the :
    minimum size

    then we use this piece of code to insert the tab in a pdf :

    QPixmap pix(QSize(1800,1000)); //with the new size of minimum size
    QPainter painter(&pix);
    ui->tab_indic->render(&painter);
    painter.end();
    
    QPrinter printer(QPrinter::HighResolution);
    printer.setOrientation(QPrinter::Portrait);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setPaperSize(QPrinter::A4);
    printer.setPageMargins(15,15,15,15,QPrinter::Millimeter);
    printer.setOutputFileName("test.pdf"); // will be in build folder
    
    painter.begin(&printer);
    
    painter.drawText(0,printer.pageRect().y()*1,QString::fromUtf8("Ligne 0"));
    
    painter.save(); // save the parameters of painter for a normal use to write texte
    
    qDebug() <<"before translate"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()<<printer.pageRect().height()<<ui->tab_indic->size();
    
    double xscale = printer.pageRect().width() / double(pix.width());
    double yscale = printer.pageRect().height() / double(pix.height());
    double scale = qMin(xscale, yscale);
    
    qDebug() <<"scale = "<< scale;
    
    /////////// Translate new parameters to the painter for draw the qtablewidget /////////////////
    
    painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,printer.paperRect().y() + printer.pageRect().height() / 2);
    
    painter.scale(scale, scale);
    
    painter.translate(-ui->tab_indic->width() / 2, -ui->tab_indic->height() / 2);
    
    painter.drawPixmap(0, 0, pix);
    
    qDebug() <<"post translate"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()
    <<printer.pageRect().height()<<ui->tab_indic->size()
    <<printer.paperRect().x() + printer.pageRect().width() / 2
    <<printer.paperRect().y() + printer.pageRect().height() / 2;
    
    painter.restore();// restore the parameters save latest
    
    qDebug() <<"post restore"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()<<printer.pageRect().height()<<ui->tab_indic->size();
    
    QFont police("Tekton Pro",10);
    QFontMetrics tailleMotPx = painter.fontMetrics();
    
    
    painter.setPen(Qt::black);
    
    painter.drawText((printer.paperRect().x())/2,0,"titre");
    
    painter.drawText(0, printer.pageRect().y()*2,QString::fromUtf8("Ligne 1"));
    
    printer.newPage();
    

    I still have some questions to do a mad pdf but for now it's cool.

    mrjjM 1 Reply Last reply
    0
    • AlbatorA Albator

      to take the whole qtablewidget with a lot of rows and columns, just go to the .ui and change the :
      minimum size

      then we use this piece of code to insert the tab in a pdf :

      QPixmap pix(QSize(1800,1000)); //with the new size of minimum size
      QPainter painter(&pix);
      ui->tab_indic->render(&painter);
      painter.end();
      
      QPrinter printer(QPrinter::HighResolution);
      printer.setOrientation(QPrinter::Portrait);
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setPaperSize(QPrinter::A4);
      printer.setPageMargins(15,15,15,15,QPrinter::Millimeter);
      printer.setOutputFileName("test.pdf"); // will be in build folder
      
      painter.begin(&printer);
      
      painter.drawText(0,printer.pageRect().y()*1,QString::fromUtf8("Ligne 0"));
      
      painter.save(); // save the parameters of painter for a normal use to write texte
      
      qDebug() <<"before translate"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()<<printer.pageRect().height()<<ui->tab_indic->size();
      
      double xscale = printer.pageRect().width() / double(pix.width());
      double yscale = printer.pageRect().height() / double(pix.height());
      double scale = qMin(xscale, yscale);
      
      qDebug() <<"scale = "<< scale;
      
      /////////// Translate new parameters to the painter for draw the qtablewidget /////////////////
      
      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,printer.paperRect().y() + printer.pageRect().height() / 2);
      
      painter.scale(scale, scale);
      
      painter.translate(-ui->tab_indic->width() / 2, -ui->tab_indic->height() / 2);
      
      painter.drawPixmap(0, 0, pix);
      
      qDebug() <<"post translate"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()
      <<printer.pageRect().height()<<ui->tab_indic->size()
      <<printer.paperRect().x() + printer.pageRect().width() / 2
      <<printer.paperRect().y() + printer.pageRect().height() / 2;
      
      painter.restore();// restore the parameters save latest
      
      qDebug() <<"post restore"<< printer.pageRect().y()<<printer.pageRect().x()<<printer.pageRect().width()<<printer.pageRect().height()<<ui->tab_indic->size();
      
      QFont police("Tekton Pro",10);
      QFontMetrics tailleMotPx = painter.fontMetrics();
      
      
      painter.setPen(Qt::black);
      
      painter.drawText((printer.paperRect().x())/2,0,"titre");
      
      painter.drawText(0, printer.pageRect().y()*2,QString::fromUtf8("Ligne 1"));
      
      printer.newPage();
      

      I still have some questions to do a mad pdf but for now it's cool.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #11

      Hi
      Can i ask what a "mad pdf" is ?

      1 Reply Last reply
      0
      • AlbatorA Offline
        AlbatorA Offline
        Albator
        wrote on last edited by Albator
        #12

        it's a good question,@mrjj
        well I'd say it's a mad pdf when it's visually beautiful but not only when I've managed to make a code that automate pdf creation by including all the widgets I need in a fast and ergonomic way without any placement hassle, no problem to cut a table when the table is bigger than the page and it doesn't display on another page etc etc etc
        it's not exactly crazy for programming champions like you jajaja!

        1 Reply Last reply
        0
        • AlbatorA Albator

          hy tout le monde :)

          I'm trying to make a nice pdf document and I'd like some information and explanations to make a good document.

          in my to do list :
          put a picture - is okay
          center a title - is okay
          grow the writing title - not okay
          soooo ...

          for explain i want this :
          pdf_style.png

          my graphs are qcustomplot

          so if you have any template or advice for beautiful pdf it's super cool :)

          MucipM Offline
          MucipM Offline
          Mucip
          wrote on last edited by
          #13

          Dear @Albator

          Please use LimeReport .
          Don't make torture yourselves. :)

          Regards,
          Mucip:)

          1 Reply Last reply
          2
          • AlbatorA Offline
            AlbatorA Offline
            Albator
            wrote on last edited by
            #14

            limereport it looks really nice, I didn't know it at all ! then you have to see if it's easy to use.
            For the moment the report I want to create contains only:

            • 1 table
            • 2 graphs
            • a dozen text boxes in qlineEdit or qtextEdit
              it's not a big deal. but the report could be useful one day thanks to me :)

            and I have one more question:
            Can you make line breaks appear in qtextEdit with a basic painter.drawText?

            mrjjM 1 Reply Last reply
            0
            • AlbatorA Albator

              limereport it looks really nice, I didn't know it at all ! then you have to see if it's easy to use.
              For the moment the report I want to create contains only:

              • 1 table
              • 2 graphs
              • a dozen text boxes in qlineEdit or qtextEdit
                it's not a big deal. but the report could be useful one day thanks to me :)

              and I have one more question:
              Can you make line breaks appear in qtextEdit with a basic painter.drawText?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #15

              @Albator said in make a beautiful pdf with qt:

              Can you make line breaks appear in qtextEdit with a basic painter.drawText?

              drawText can wordwrap if you use the overload that takes a rect.

              using LimeReport might be a good option as not to hand program all of it.
              Its very powerfull and once you learn it good, you can make almost anything.

              MucipM 1 Reply Last reply
              2
              • mrjjM mrjj

                @Albator said in make a beautiful pdf with qt:

                Can you make line breaks appear in qtextEdit with a basic painter.drawText?

                drawText can wordwrap if you use the overload that takes a rect.

                using LimeReport might be a good option as not to hand program all of it.
                Its very powerfull and once you learn it good, you can make almost anything.

                MucipM Offline
                MucipM Offline
                Mucip
                wrote on last edited by
                #16

                @mrjj said in make a beautiful pdf with qt:

                Its very powerfull and once you learn it good, you can make almost anything.

                Hi,
                Absolutely true...

                Dive into LimeReport... :)

                Regards,
                Mucip:)

                AlbatorA 1 Reply Last reply
                1
                • MucipM Mucip

                  @mrjj said in make a beautiful pdf with qt:

                  Its very powerfull and once you learn it good, you can make almost anything.

                  Hi,
                  Absolutely true...

                  Dive into LimeReport... :)

                  Regards,
                  Mucip:)

                  AlbatorA Offline
                  AlbatorA Offline
                  Albator
                  wrote on last edited by
                  #17

                  @Mucip @mrjj @SGaist
                  Okay, you convinced me :) thanks again

                  1 Reply Last reply
                  1
                  • AlbatorA Offline
                    AlbatorA Offline
                    Albator
                    wrote on last edited by
                    #18

                    I've been watching limereport and I'm thinking that at first glance it's not bad because you can find all our widgets directly in the database and all you have to do is drag and drop them to get them in a report.

                    But it doesn't do it automatically? we have to place them manually, at first glance anyway.

                    mrjjM 1 Reply Last reply
                    0
                    • AlbatorA Albator

                      I've been watching limereport and I'm thinking that at first glance it's not bad because you can find all our widgets directly in the database and all you have to do is drag and drop them to get them in a report.

                      But it doesn't do it automatically? we have to place them manually, at first glance anyway.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #19

                      @Albator
                      Well that is very normal with report generators as its very hard to guess
                      what to put in the report for the app.
                      So often that is the creative process.

                      1 Reply Last reply
                      1

                      • Login

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