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. QTextDocument Printing Error

QTextDocument Printing Error

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.5k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    ShadyAshraf
    wrote on last edited by ShadyAshraf
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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);
          }
      }
      
      
      S 1 Reply Last reply
      2
      • mrjjM mrjj

        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);
            }
        }
        
        
        S Offline
        S Offline
        ShadyAshraf
        wrote on last edited by ShadyAshraf
        #3

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

        mrjjM 1 Reply Last reply
        0
        • S ShadyAshraf

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          S 1 Reply Last reply
          0
          • mrjjM mrjj

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

            S Offline
            S Offline
            ShadyAshraf
            wrote on last edited by
            #5

            @mrjj Thanks so much for your time and help!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              ShadyAshraf
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1

              • Login

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