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. Combine Multiple Images Into a Single PDF
Forum Updated to NodeBB v4.3 + New Features

Combine Multiple Images Into a Single PDF

Scheduled Pinned Locked Moved Unsolved General and Desktop
pdfsavewidget
38 Posts 7 Posters 37.0k Views 2 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.
  • ? A Former User

    Hi! No need for external libraries. QPdfWriter can create documents with multiple pages.

    georG Offline
    georG Offline
    geor
    wrote on last edited by
    #5

    @Wieland I took a look at the functions for that class and all I see is way to edit the pages but how do you add the images?

    thank you

    mrjjM ? 2 Replies Last reply
    0
    • koronaboraK koronabora

      Use one of them:

      1. http://podofo.sourceforge.net
      2. https://github.com/pocoproject/poco/tree/master/PDF
      georG Offline
      georG Offline
      geor
      wrote on last edited by
      #6

      @koronabora thank you but I prefer not to use 3rd party lib

      1 Reply Last reply
      0
      • georG geor

        @Wieland I took a look at the functions for that class and all I see is way to edit the pages but how do you add the images?

        thank you

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

        @geor
        Hi
        You draw the images to it.
        http://www.codeprogress.com/cpp/libraries/qt/showQtExample.php?index=570&key=QPdfWriterWriteImage

        Keep in mind about DPI.

        georG 1 Reply Last reply
        4
        • georG geor

          @Wieland I took a look at the functions for that class and all I see is way to edit the pages but how do you add the images?

          thank you

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #8

          @geor Like this:

          void writePdf()
          {
              const QString fileName("/home/patrick/mydoc.pdf");
              const QImage image("/home/patrick/truck.jpg");
              const QPoint imageCoordinates(0,0);
          
              QPdfWriter pdfWriter(fileName);
              pdfWriter.setPageSize(QPageSize(QPageSize::A4));
              QPainter painter(&pdfWriter);
          
              for (int i=0; i<3; ++i) {
                  painter.drawImage(imageCoordinates, image);
                  pdfWriter.newPage();
              }
          }
          
          georG 1 Reply Last reply
          6
          • mrjjM mrjj

            @geor
            Hi
            You draw the images to it.
            http://www.codeprogress.com/cpp/libraries/qt/showQtExample.php?index=570&key=QPdfWriterWriteImage

            Keep in mind about DPI.

            georG Offline
            georG Offline
            geor
            wrote on last edited by
            #9

            @mrjj thank you

            1 Reply Last reply
            0
            • ? A Former User

              @geor Like this:

              void writePdf()
              {
                  const QString fileName("/home/patrick/mydoc.pdf");
                  const QImage image("/home/patrick/truck.jpg");
                  const QPoint imageCoordinates(0,0);
              
                  QPdfWriter pdfWriter(fileName);
                  pdfWriter.setPageSize(QPageSize(QPageSize::A4));
                  QPainter painter(&pdfWriter);
              
                  for (int i=0; i<3; ++i) {
                      painter.drawImage(imageCoordinates, image);
                      pdfWriter.newPage();
                  }
              }
              
              georG Offline
              georG Offline
              geor
              wrote on last edited by
              #10

              @Wieland thank you

              ? 1 Reply Last reply
              0
              • georG geor

                @Wieland thank you

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #11

                @geor :-)

                georG 1 Reply Last reply
                1
                • ? A Former User

                  @geor :-)

                  georG Offline
                  georG Offline
                  geor
                  wrote on last edited by
                  #12

                  @Wieland I keep getting qpainter::begin() reutnr false

                  I am even using your exact code

                          const QString fileName("C:/Qt");
                  const QImage image("C:/Users/georg/Desktop/testPdf/Untitled.jpg");
                  const QPoint imageCoordinates(0,0);
                  
                  QPdfWriter pdfWriter(fileName);
                  pdfWriter.setPageSize(QPagedPaintDevice::A1);
                  QPainter painter(&pdfWriter);
                  
                  for (int i=0; i<3; ++i)
                  {
                      painter.drawImage(imageCoordinates, image);
                      pdfWriter.newPage();
                      qDebug()<<2;
                  }
                  

                  except for the page size becuase I am using qt 5.2.1

                  do you have any idea why ebcuase I cannot figure it out the debug still runs i just get the eror and it does not save

                  mrjjM 1 Reply Last reply
                  1
                  • georG geor

                    @Wieland I keep getting qpainter::begin() reutnr false

                    I am even using your exact code

                            const QString fileName("C:/Qt");
                    const QImage image("C:/Users/georg/Desktop/testPdf/Untitled.jpg");
                    const QPoint imageCoordinates(0,0);
                    
                    QPdfWriter pdfWriter(fileName);
                    pdfWriter.setPageSize(QPagedPaintDevice::A1);
                    QPainter painter(&pdfWriter);
                    
                    for (int i=0; i<3; ++i)
                    {
                        painter.drawImage(imageCoordinates, image);
                        pdfWriter.newPage();
                        qDebug()<<2;
                    }
                    

                    except for the page size becuase I am using qt 5.2.1

                    do you have any idea why ebcuase I cannot figure it out the debug still runs i just get the eror and it does not save

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

                    @geor said:

                    const QString fileName("C:/Qt");

                    that is the actual filename ?

                    I would expect something like
                    const QString fileName("c:/mydoc.pdf");

                    georG 1 Reply Last reply
                    2
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #14

                      To add to what @mrjj said, do you have write access to C:/?

                      georG 2 Replies Last reply
                      3
                      • mrjjM mrjj

                        @geor said:

                        const QString fileName("C:/Qt");

                        that is the actual filename ?

                        I would expect something like
                        const QString fileName("c:/mydoc.pdf");

                        georG Offline
                        georG Offline
                        geor
                        wrote on last edited by
                        #15

                        @mrjj said:

                        @
                        I still get the same error

                        mrjjM 1 Reply Last reply
                        0
                        • ? A Former User

                          To add to what @mrjj said, do you have write access to C:/?

                          georG Offline
                          georG Offline
                          geor
                          wrote on last edited by
                          #16
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • ? A Former User

                            To add to what @mrjj said, do you have write access to C:/?

                            georG Offline
                            georG Offline
                            geor
                            wrote on last edited by
                            #17

                            @Wieland

                            once again thank you for your help it worked when I switch to the drive where the build is saved .However for some reason it is 4 empty pages maybe it's becuase of the size or something I'll try another image and see but again thank you

                            mrjjM 1 Reply Last reply
                            0
                            • georG geor

                              @mrjj said:

                              @
                              I still get the same error

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

                              @geor
                              I think its just a path thing

                              void writePdf()
                              {
                                  const QString fileName("e:/mydoc.pdf");
                                     QPdfWriter pdfWriter(fileName);
                                  pdfWriter.setPageSize(QPageSize(QPageSize::A4));
                                  QPainter painter(&pdfWriter);
                              
                                  for (int i=0; i<3; ++i) {
                                      painter.drawPixmap(QRect(0,0,pdfWriter.logicalDpiX()*8.3,pdfWriter.logicalDpiY()*11.7), QPixmap("e:/truck.png"));
                                      pdfWriter.newPage();
                                  }
                              }
                              
                              works here. makes PDF on e:
                              
                              1 Reply Last reply
                              2
                              • georG geor

                                @Wieland

                                once again thank you for your help it worked when I switch to the drive where the build is saved .However for some reason it is 4 empty pages maybe it's becuase of the size or something I'll try another image and see but again thank you

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

                                @geor
                                hi
                                You will / might need to scale the picture or its really small.

                                georG 2 Replies Last reply
                                1
                                • mrjjM mrjj

                                  @geor
                                  hi
                                  You will / might need to scale the picture or its really small.

                                  georG Offline
                                  georG Offline
                                  geor
                                  wrote on last edited by
                                  #20

                                  @mrjj

                                  yeh it worked after scaling thank you.

                                  1 Reply Last reply
                                  1
                                  • mrjjM mrjj

                                    @geor
                                    hi
                                    You will / might need to scale the picture or its really small.

                                    georG Offline
                                    georG Offline
                                    geor
                                    wrote on last edited by
                                    #21

                                    @mrjj one more question lol

                                    the size of my widget is too big to fit well on a A4 paper without looking deformed. So I want to break the layout of it's parent, resize my widget it and then take it's pixmap to put it in an A4 so it is not deformed (with the grab function) but the problem is I can't break the layout of the parent i can only delete (in code) and when I delete it it deletes everything in it any tips?

                                    mrjjM 1 Reply Last reply
                                    0
                                    • georG geor

                                      @mrjj one more question lol

                                      the size of my widget is too big to fit well on a A4 paper without looking deformed. So I want to break the layout of it's parent, resize my widget it and then take it's pixmap to put it in an A4 so it is not deformed (with the grab function) but the problem is I can't break the layout of the parent i can only delete (in code) and when I delete it it deletes everything in it any tips?

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

                                      @geor
                                      Hi you can use the takeAt method to get your widget back from a layout.
                                      But maybe you can just ask the widget to render directly?
                                      it has a render function that can draw itself to anything you like.
                                      http://doc.qt.io/qt-5/qwidget.html#render

                                      georG 1 Reply Last reply
                                      3
                                      • mrjjM mrjj

                                        @geor
                                        Hi you can use the takeAt method to get your widget back from a layout.
                                        But maybe you can just ask the widget to render directly?
                                        it has a render function that can draw itself to anything you like.
                                        http://doc.qt.io/qt-5/qwidget.html#render

                                        georG Offline
                                        georG Offline
                                        geor
                                        wrote on last edited by
                                        #23

                                        @mrjj

                                        it doesn't seem to chnage much unless I am using it wrong? here is my code 1870 and 994 is the size of my widget.making the pixmap the same as the size of my widget gives me excatly the same ouput as before so i made it smaller it's better but cuts most of the widget out

                                                           ui->page1->setBackgroundRole(QPalette::BrightText);
                                        
                                                     QPixmap pixMap1(600,900);
                                                   ui->page1->render(&pixMap1,QPoint(),QRegion(0,0,1870,994));
                                        
                                        
                                        
                                        
                                            const QString fileName("D:/mydoc2.pdf");
                                               QPdfWriter pdfWriter(fileName);
                                            pdfWriter.setPageSize(QPagedPaintDevice::A4);
                                            QPainter painter(&pdfWriter);
                                        
                                           
                                                painter.drawPixmap(QRect(0,0,pdfWriter.logicalDpiX()*8.3,pdfWriter.logicalDpiY()*11.7), pixMap1);
                                                pdfWriter.newPage();
                                        
                                        mrjjM 1 Reply Last reply
                                        1
                                        • georG geor

                                          @mrjj

                                          it doesn't seem to chnage much unless I am using it wrong? here is my code 1870 and 994 is the size of my widget.making the pixmap the same as the size of my widget gives me excatly the same ouput as before so i made it smaller it's better but cuts most of the widget out

                                                             ui->page1->setBackgroundRole(QPalette::BrightText);
                                          
                                                       QPixmap pixMap1(600,900);
                                                     ui->page1->render(&pixMap1,QPoint(),QRegion(0,0,1870,994));
                                          
                                          
                                          
                                          
                                              const QString fileName("D:/mydoc2.pdf");
                                                 QPdfWriter pdfWriter(fileName);
                                              pdfWriter.setPageSize(QPagedPaintDevice::A4);
                                              QPainter painter(&pdfWriter);
                                          
                                             
                                                  painter.drawPixmap(QRect(0,0,pdfWriter.logicalDpiX()*8.3,pdfWriter.logicalDpiY()*11.7), pixMap1);
                                                  pdfWriter.newPage();
                                          
                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #24

                                          @geor
                                          hi
                                          Sorry, didn't read the right docs.
                                          The rect is for clipping not resize the widget.
                                          So render will not help you to render it smaller.

                                          so yes, i guess to render it in different size, you must take widget out , call resize +render and put it back it.
                                          Not sure how well it will work if there is other stuff in the layout.
                                          If sole widget, it should work ok.

                                          georG 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