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. Create PDF with automatically new page (QTExtDocument, QTextCursor, ...)
QtWS25 Last Chance

Create PDF with automatically new page (QTExtDocument, QTextCursor, ...)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.9k Views
  • 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.
  • CharlieGC Offline
    CharlieGC Offline
    CharlieG
    wrote on last edited by
    #1

    Hello,

    Here a code which allow me te create a pdf with automatically add a new page :

    void Class1::addContent()
    {
        m_painter->setPen(Qt::green);
        m_painter->drawRect(m_rectContent);
    
        QTextDocument tdContent;
        QTextOption textOptionContent;
        textOptionContent.setAlignment(Qt::AlignJustify);
        textOptionContent.setWrapMode(QTextOption::WordWrap);
        tdContent.setDefaultTextOption(textOptionContent);
        tdContent.setTextWidth(m_writer->width());
    
        setContent(&tdContent, 0); 
    }
    
    void Class1::setContent(QTextDocument *doc, int beginPosition)
    {
        doc->clear();
        QString htmlText;
        m_cursor = new QTextCursor(doc);
        m_cursor->insertHtml(m_totalHtml);
        //m_html is my QString to print
    
        int lenTextToPrint = m_cursor->selectionEnd()-beginPosition;
    
        qDebug() << "   ";
        qDebug() << "============";
        qDebug() << "Page n°" << m_nbPage;
        qDebug() << "Initial text lenght" << m_cursor->selectionEnd();
        qDebug() << "Begin position" << beginPosition;
        qDebug() << "Text to print" << lenTextToPrint;
    
        if (beginPosition != 0)
        {
            m_cursor->setPosition(beginPosition-1);
            m_cursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
            htmlText = m_cursor->selection().toHtml();
            m_cursor->select(QTextCursor::Document);
            m_cursor->removeSelectedText();
            m_cursor->insertHtml(htmlText);
        }
        else {
            htmlText = m_totalHtml;
        }
    
        qDebug() << "Current TextDoc height" << doc->documentLayout()->documentSize().height();
        qDebug() << "Available per page" << m_rectContent.height();
    
        int actPosi = 0;
        if (doc->documentLayout()->documentSize().height() > m_rectContent.height()){
            doc->clear();
            while(doc->documentLayout()->documentSize().height() < m_rectContent.height())
            {
                QTextCursor *cursor = new QTextCursor(doc);
                cursor->insertHtml(htmlText);
                cursor->setPosition(actPosi);
                cursor->movePosition(QTextCursor::Down);
                actPosi = cursor->position();
    
                cursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
                cursor->removeSelectedText();
            }
            paintContent(doc);
    
            m_alreadyPaint += actPosi;
            qDebug() << "Last Position" << actPosi;
            qDebug() << "Already print" << m_alreadyPaint;
            if (actPosi < lenTextToPrint){
                newPage();
                setContent(doc, m_alreadyPaint);
            }
        }
        else {
            paintContent(doc);
        }
    }
    
    void Class1::paintContent(QTextDocument *doc)
    {
        m_painter->save();
        m_painter->translate(m_rectContent.topLeft());
        doc->drawContents(m_painter);
        m_painter->restore();
    }
    
    void Class1::print()
    {
        m_writer = new QPdfWriter(m_fileName);
        m_writer->setResolution(m_resolution);
        QPageSize pageSize(QPageSize::A4);
        QMarginsF margins(m_margins, m_margins, m_margins, m_margins);
        QPageLayout pageLayout(pageSize, QPageLayout::Portrait, margins, QPageLayout::Millimeter);
        m_writer->setPageLayout(pageLayout);
        m_nbPage = 1;
        m_heightAvailable = m_writer->height();
        m_rectContent.setCoords(0, 0, m_writer->width(), 0);
        m_painter = new QPainter(m_writer);
    
        addContent();
    }
    

    I have 2 questions :

    • Do you have any suggestions for improving my code or otherwise ?
    • Sometimes the new pages begin with a letter less to the first, have you an idea to fixe the issues ?

    You can test my code by downloading it here : https://github.com/jiyuusoft/QMLReports

    Thank you in advance for your help and comments.

    Charlie

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      I'm not sure I got what the end goal is. Are you trying to achieve something like this?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      CharlieGC 1 Reply Last reply
      0
      • VRoninV VRonin

        I'm not sure I got what the end goal is. Are you trying to achieve something like this?

        CharlieGC Offline
        CharlieGC Offline
        CharlieG
        wrote on last edited by
        #3

        @VRonin
        Indeed this allows to simplify the code for the creation of the new pages automatically. However, I need to review how to handle headers and footers for each page. If it's simpler, why not ...

        1 Reply Last reply
        0

        • Login

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