Important: Please read the 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" ); QFile::ReadOnly );
    QString hex = QString::fromLatin1( styleFile.readAll() );

    QTextDocument document;
    QString data = ui->teData->toPlainText();

    QPrinter printer;
    QPainter painter;
    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"));
    printer.setPageMargins(QMarginsF(15, 15, 15, 15));


  • Hex variable contains the data read from the file.

    File has the following data:

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


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

    .floatLeft { float: left; }

    .floatRight { float: right; }

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

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



  • Lifetime Qt Champion

    I think you need to make it part of the QTextDocument for each page
    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
    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