Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Trying to Add image as header for Every Page in PDF



  • 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.


  • Lifetime Qt Champion

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



  • 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);


  • 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>


  • Lifetime Qt Champion

    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.



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


Log in to reply