creating pdf document with qtextdocument and qpainter (translate)



  • Hi,

    currently I am working on a project where I have to generate a pdf export.

    The first idea was to create a QTextDocument representing the whole stuff and call QTextDocument::print to generate the PDF. With that solution I had problems with page breaks and tables were split somewhere.

    The second idea was creating small QTextDocuments of parts and check whether there is enough space left on the current page and if not call QPrinter::newPage otherwise call QTextDocument::drawContens.

    With that solution I am able controlling the page breaks but QPainter::translate does not seem to work. Each QTextDocument is printed ontop each other.

    QPainter painter;
    painter.begin(&printer);
    for(Project p:projects) {
    
        for(Issue i:p.getIssues()) {
            
            QTextDocument doc;
            doc.setPageSize(printer.pageRect().size());
            QTextCursor cur(&doc);
            
            cur.insertBlock(format);
            drawIssue(i, cur);
            
            float height = doc.documentLayout()->documentSize().height();
            
            qDebug() << "cur " << curHeight + height;
            qDebug() << printer.pageRect().height();
            qDebug();
            
            curHeight += height;
            
            painter.save();
            QRect contentRect(0,0,printer.pageRect().width(), curHeight);
            qDebug() << contentRect;
            
            painter.translate(contentRect.topLeft());
            doc.drawContents(&painter, contentRect);
            painter.restore();
            
            pageCounter++;
            
        }
        
    }
    painter.end();
    

    Anybody an idea?

    Best regards :)



  • Have you tried calling QPrinter::newPage() in the loop?



  • Also, calling translate with a contentRect.topLeft point of 0,0, does not do much :-)



  • @mcosta
    Yes if I do so, I get a new page as requested, thats fine.
    But I like to draw as many as possible parts to each page.

    One QTextDocument fills about 1/3 of an A4 page. So I need to print 3 doc's each page and than I will call newPage. The code above is a snippet to show my current problem only.



  • @MrBlueSky125

    Yes your right but I thought of either calling QPainter::translate or provide the height inside the contentRect.
    I tried both ways.

    QPainter::translate moves the origin of the painter to the new position. After that I have to draw on the origin position.



  • Ok I think I have mixed both ideas and ended up in a not working solution °° ^^

    This is working like expected

    QPainter painter;
    painter.begin(&printer);
    for(Project p:projects) {
    
        for(Issue i:p.getIssues()) {
    
            QTextDocument doc;
            doc.setPageSize(printer.pageRect().size());
            QTextCursor cur(&doc);
    
            cur.insertBlock(format);
            drawIssue(i, cur);
    
            float height = doc.documentLayout()->documentSize().height();
            curHeight += height + space;
    
            if(curHeight > printer.pageRect().height()) {
                printer.newPage();
                curHeight = height + space;
            }
    
            painter.save();
            QRect contentRect(0,0,printer.pageRect().width(), curHeight);
            painter.translate(QPoint(0, curHeight - height));
            doc.drawContents(&painter, contentRect);
            painter.restore();
    
            pageCounter++;
    
        }
    
    }
    painter.end();


  • Hmm, in your example you use the contentRect only to clip what you draw.
    No translation, no scaling



  • @MrBlueSky125 said:

    Hmm, in your example you use the contentRect only to clip what you draw.
    No translation, no scaling

    Yes your right.
    Have a look to my last comment.

    And pointing to a contentRect is not needed anymore since I have defined the QTextDocument pageSize.
    Translating only is sufficient.

    Thank you all :)



  • HI,

    don't forget to mark the thread as [SOLVED]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.