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

QTextDocument Printing Error



  • Every time I try to use ui->Document->print(&printer) I get this error "QWin32PrintEngine::end: EndPage failed (0xbe212904) (The operation completed successfully.)"
    I get the same error when I try using it in textedit but rendering worked for me in that case, however I need to work out the QTextDocument because I'm trying to print a QTableWidget.
    I already searched for this error but no luck, keep in my mind that I'm still a newbie so if my question is too stupid please bear with me. Thanks in advance!

    QString css;
            css="<style type=\"text/css\">";
            css+="table { page-break-inside:auto; border-style:solid;border-width: 1px }";
            css+="tr    { page-break-inside:avoid; page-break-after:auto}";
            css+="thead { display:table-header-group }";
            css+="tfoot { display:table-footer-group }";
            css+="</style>";
    
            QString yazi;
            yazi="<table><thead><tr><th>header</th></tr></thead><tbody>";
            yazi.append("<tr><td>");
            for(int i=0;i<ui->stockreporttableWidget->rowCount()-1;i++)
            {
                yazi.append("<tr><td>"+QString::number(i+1)+"</td>");
                for(int j=0;j<ui->stockreporttableWidget->columnCount();j++)
                {
                    yazi.append("<td width=\"10%\">"+ui->stockreporttableWidget->item(i,j)->text()+"</td>");
                }
                yazi.append("</tr>");
            }
            yazi.append("</tbody><tfoot><tr><td>footer</td></tr></tfoot></table>");
    
    
            QPrinter printer;
                printer.setOrientation(QPrinter::Landscape);
                printer.setPageMargins(10,10,10,10,QPrinter::Millimeter);
                QTextDocument *document=new QTextDocument();
                document->setHtml(css+yazi);
                document->end();
    
                QPrintDialog *dlg = new QPrintDialog(&printer, this);
                if (dlg->exec() != QDialog::Accepted)
                    return;
                document->print(&printer);
    
    

    I took the code from here https://www.qtcentre.org/threads/53181-cell-division-problem-on-QTextDocument?highlight=QTextTable
    I also copied another couple of codes that people claimed they're working and I still get the same error.


  • Lifetime Qt Champion

    Hi and welcome to the forums.
    Never saw that error before and since you show no code its hard to guess at.
    However, this prints a tableWidget

    void MainWindow::PrintWidget(QWidget* widget) {
    
      QPixmap pix(widget->size());
      QPainter painter(&pix);
      widget->render(&painter);
      painter.end();
      QPrinter printer(QPrinter::HighResolution);
      printer.setOrientation(QPrinter::Landscape);
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setPaperSize(QPrinter::A4);
      printer.setOutputFileName("e:/test.pdf"); // CHANGE PATH!
    
      painter.begin(&printer);
      double xscale = printer.pageRect().width() / double(pix.width());
      double yscale = printer.pageRect().height() / double(pix.height());
      double scale = qMin(xscale, yscale);
      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                        printer.paperRect().y() + printer.pageRect().height() / 2);
      painter.scale(scale, scale);
      painter.translate(-widget->width() / 2, -widget->height() / 2);
      painter.drawPixmap(0, 0, pix);
    
    QTextDocument doc;
    
    doc.setHtml("htmlcontent"); // this is at top. just for test
    doc.drawContents(&painter);
    
      painter.end();
    }
    
    
    
    void MainWindow::on_pushButton_2_released() // to print it
    {
        QPrinter printer;
        QPrintDialog printDialog(&printer, this);
        if (printDialog.exec() == QDialog::Accepted) {
        PrintWidget(ui->tableWidget);
        }
    }
    
    


  • @mrjj I edited my post so you can see the code, as for the one you gave me it renders the widget so if the table has too many rows it crops out the data. Through the search I did I read somewhere that I should use print instead of the painter to print all my data.
    Thanks for your reply I really appreciate it.


  • Lifetime Qt Champion

    @ShadyAshraf
    Yes, its more of a screen shot thing.
    Ok, i see you generate html.
    Need to test the code and see if i get same error.



  • @mrjj Thanks so much for your time and help!



  • The problem was I was printing using Microsoft XPS Document Writer, but when I used a real printer it printed everything fine, thanks!


Log in to reply