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. Trying to Add image as header for Every Page in PDF
Forum Updated to NodeBB v4.3 + New Features

Trying to Add image as header for Every Page in PDF

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.9k 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.
  • M Offline
    M Offline
    ManiRon
    wrote on 20 Aug 2018, 09:11 last edited by ManiRon
    #1

    I am trying to add header image , That is two images one to the left and another to the right top corner in a PDF file. These Images are seen in the first page but if there are more than one page the image is not coming . I want to display the image in every page created based on the text size input . For Example : If there are 400 words 200 will come in one page and next 200 will come in next page . But in this only the first page has the image as header and the second page doesnt. Is there any way i can insert the image on each page generated.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 20 Aug 2018, 09:18 last edited by
      #2

      Hi and welcome to the forums
      Could you show how you currently print the pages and header ?

      1 Reply Last reply
      1
      • M Offline
        M Offline
        ManiRon
        wrote on 20 Aug 2018, 09:21 last edited by
        #3

        Here I have attached the code:

        QFile styleFile( "widget.qss" );
        styleFile.open( QFile::ReadOnly );
        QString hex = QString::fromLatin1( styleFile.readAll() );
        styleFile.close();

        QTextDocument document;
        
        QString data = ui->teData->toPlainText();
        
        document.setHtml(hex.append(data));
        

        QPrinter printer;
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setPaperSize(QPrinter::A4);
        printer.setOutputFileName("test.pdf");
        QPainter painter;
        painter.begin(&printer);
        document1.setHtml(hex);
        painter.setBrush(Qt::transparent);
        painter.drawPixmap(0, 0, 100, 100, QPixmap("img.png"));
        painter.drawPixmap(200, 0, 100, 100, QPixmap("img.png"));
        painter.drawPixmap(0, 100, 100, 100, QPixmap("img.png"));
        painter.drawPixmap(200, 100, 100, 100, QPixmap("img.png"));
        painter.end();
        printer.setPageMargins(QMarginsF(15, 15, 15, 15));

        document.print(&printer);
        
        1 Reply Last reply
        1
        • M Offline
          M Offline
          ManiRon
          wrote on 20 Aug 2018, 10:12 last edited by
          #4

          Hex variable contains the data read from the file.

          File has the following data:

          <!DOCTYPE html>
          <html>
          <head>
          <style>
          .logo {
          width: 150px;
          height: 120px;

          }

          .logoHeader {
          height: 120px;
          vertical-align: middle;
          text-align: center;
          }

          .floatLeft { float: left; }

          .floatRight { float: right; }
          </style>
          </head>
          <body>

          <header>
          <img class="logo floatLeft" src="Logo.png" alt="Logo" />

          <img class="logo floatRight"  src="Data.gif" alt="Logo" />
          
          <h1 class="logoHeader">REPORT</h1>
          

          </header>

          </body>
          </html>

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 20 Aug 2018, 10:19 last edited by mrjj
            #5

            Hi
            I think you need to make it part of the QTextDocument for each page
            https://forum.qt.io/topic/58550/printing-pdf-with-headers-footers
            as document.print does pagination by itself and as far as i know have no signals
            pr page or anything that allows us to inject new HTML pr page.

            I also found this
            https://gist.github.com/chris-marsh/242b6229c7c8d2ed687833db3087793f
            but not had time to test it.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              ManiRon
              wrote on 20 Aug 2018, 10:21 last edited by
              #6

              but how i will do that. The text size may vary and how to add the header image any eample if possible.

              1 Reply Last reply
              0

              1/6

              20 Aug 2018, 09:11

              • Login

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