title: Multi page report with QTextDocument
body: <p>I am generating html report with <code>QTextDocument</code>, my problem it comes when it should print each table on its own page, but it doesn't. I tried to use <code>QPainter</code> along with <code>QTextDocument::drawContents()</code> but this makes it show only the first page.
  Another attempt was to insert a <code>div</code> and set its style to p<code>age-break-after:auto</code> even it is supported by <code>QTextDocument</code> web engine, but this didn't work too.</p>
  
  <pre><code>void SemesterResultsReport::printDivisionStudentsNotes() {
      QMap&lt;int, int&gt; divisionsList = SemesterResultsReport::getSelectedDivisions();
  
      QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
      printer-&gt;setFullPage(true);
      printer-&gt;setResolution(90);
      printer-&gt;setPaperSize(QPrinter::A4);
      printer-&gt;setOrientation(QPrinter::Landscape);
      printer-&gt;setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
      /*printer-&gt;setOutputFormat(QPrinter::PdfFormat);
      printer-&gt;setOutputFileName("sdf.pdf");*/
  
  
      QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, this);
      connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printOrder(QPrinter *)));
      dlg-&gt;exec();
  }
  
  
  void SemesterResultsReport::printOrder(QPrinter *printer) {
      QString strStream;
      QTextStream out(&amp;strStream);
  
      QSqlQuery studentInfo, studentNotes;
      QSqlRecord studentInfoRec, studentNotesRec;
  
      QMap&lt;int, int&gt; selectedDivisions = SemesterResultsReport::getSelectedDivisions();
      QMap&lt;int, int&gt;::const_iterator divisionId;
  
      QTextDocument *document = new QTextDocument();
      QTextCursor cursor(document);
      QTextOption options;
      options.setTextDirection(Qt::RightToLeft);
      QSizeF paperSize;
      paperSize.setWidth(printer-&gt;width());
      paperSize.setHeight(printer-&gt;height());
      document-&gt;setDefaultTextOption(options);
      document-&gt;setPageSize(paperSize);
  
      int mat_div = 0;
      int level = 0;
      int semester = ui.semestersList-&gt;currentText().toInt();
      int school_year = 2015;
      int numMaterials = 0;
  
      // Report
      out &lt;&lt; "&lt;!DOCTYPE html&gt;"
          &lt;&lt; "&lt;html&gt;\n"
          &lt;&lt; "&lt;head&gt;"
          &lt;&lt; "&lt;title&gt;ff&lt;/title&gt;"
          &lt;&lt; "&lt;meta http-equiv=\"Content-Type\" content =\"text/html;charset=utf-8\" &gt;"
          &lt;&lt; "&lt;style type=\"text/css\"&gt; "
          &lt;&lt; "  html, body { margin: 5px; direction: rtl; width: 100% !important; align: right !important; float: right !important; }"
          &lt;&lt; "  *, p { font-family: \"Times New Roman\"; font-size: 16px; }"
          &lt;&lt; "  img { display: block; margin: 0 auto; }"
          &lt;&lt; "  p.title { font-weight: bold; font-size: 22px; align: center !important; }"
          &lt;&lt; "  table { border: 1; border-collapse: collapse; page-break-after:auto !important; width: 100% !important; align: right !important; float: right !important; }"
          &lt;&lt; "  th, td    { border: 1px solid #000; padding: 0; align: center; page-break-inside:avoid; page-break-after:auto; }"
          &lt;&lt; "  tr    { page-break-inside:avoid; page-break-after:auto !important; }"
  
          &lt;&lt; "  thead { display:table-header-group; }"
          &lt;&lt; "  tfoot { display:table-footer-group; }"
          &lt;&lt; "  .pagebreak { page-break-after:auto !important; } "
          &lt;&lt; "&lt;/style&gt;"
          &lt;&lt; "&lt;/head&gt;"
          &lt;&lt; "&lt;body&gt;";
      for (divisionId = selectedDivisions.constBegin(); divisionId != selectedDivisions.constEnd(); ++divisionId) {
          mat_div = divisionId.key();
          level = divisionId.value();
          numMaterials = 0;
  
          // Report header, get division materials to set as header
          QStringList divisionMaterialsNames = SemesterResultsReport::getDivisionMaterialsNames(mat_div, level, school_year);
          out &lt;&lt; "&lt;table float=\"right\" border=1 cellspacing=0 cellpadding=2&gt;";
          numMaterials = divisionMaterialsNames.size();
          out &lt;&lt; "&lt;thead&gt;&lt;tr bgcolor=#f0f0f0&gt;";
          for (int i = numMaterials - 1; i &gt; -1; --i) {
              out &lt;&lt; "&lt;th&gt;" + divisionMaterialsNames[i] + "&lt;/th&gt;";
          }
  
          out &lt;&lt; "&lt;th&gt;" + QString("الإسم") + "&lt;/th&gt;"
              &lt;&lt; "&lt;th&gt;" + QString("اللقب") + "&lt;/th&gt;"
              &lt;&lt; "&lt;th&gt;" + QString("الرقم التسلسلي") + "&lt;/th&gt;"
              &lt;&lt; "&lt;/tr&gt;&lt;/thead&gt;";
  
          // Echo student info plus materials notes
          QString fullName = ", fname, lname";
          QString infoQuery = "SELECT Student.mat_stud as matStud" + fullName + " FROM Student, Class, Division WHERE Class.mat_div = Division.mat_div AND Class.mat_class = Student.mat_class AND school_year = " + QString::number(school_year) + " AND Class.level = " + QString::number(level) + " AND Division.mat_div = " + QString::number(mat_div);
          studentInfo.exec(infoQuery);
          studentInfoRec = studentInfo.record();
          fullName.clear();
          infoQuery = "SELECT Student.mat_stud as matStud FROM Student, Class, Division WHERE Class.mat_div = Division.mat_div AND Class.mat_class = Student.mat_class AND school_year = " + QString::number(school_year) + " AND Class.level = " + QString::number(level) + " AND Division.mat_div = " + QString::number(mat_div);
          QString notesQuery = "SELECT materials_notes FROM Notes WHERE mat_stud IN (" + infoQuery + ") AND school_year = " + QString::number(school_year) + " AND level = " + QString::number(level) + " AND mat_div = " + QString::number(mat_div) + " AND semester = " + QString::number(semester);
          studentNotes.exec(notesQuery);
          studentNotesRec = studentNotes.record();
          QStringList studentNotesList;
          while (studentInfo.next()) {
              out &lt;&lt; "&lt;tr&gt;";
              studentNotes.next();
              studentNotesList = studentNotes.value(studentNotesRec.indexOf("materials_notes")).toString().split(",");
              for (int i = 0; i &lt; numMaterials; ++i) {
                  out &lt;&lt; "&lt;th&gt;" + studentNotesList[i] + "&lt;/th&gt;";
              }
  
              out &lt;&lt; "    &lt;th&gt;" + studentInfo.value(studentInfoRec.indexOf("lname")).toString() + "&lt;/th&gt;"
                  &lt;&lt; "    &lt;th&gt;" + studentInfo.value(studentInfoRec.indexOf("fname")).toString() + "&lt;/th&gt;"
                  &lt;&lt; "    &lt;th&gt;" + studentInfo.value(studentInfoRec.indexOf("matStud")).toString() + "&lt;/th&gt;";
  
              out &lt;&lt; "&lt;/tr&gt;";
          }
  
          // Close table tag, and insert new page, but it doesn't work
          out &lt;&lt; "&lt;/table&gt;&lt;div style=\"page-break-after:auto !important;\"&gt;&lt;/div&gt;";
      }
  
      // Finish report
      out &lt;&lt; "&lt;/body&gt;"
          &lt;&lt; "&lt;/html&gt;";
  
  
      /*
      * Prepare QTextDocument
      */
      document-&gt;setHtml(strStream);
      document-&gt;print(printer);
      // this makes only the first page printed
      /*QPainter painter;
      painter.begin(printer);
      document-&gt;drawContents(&amp;painter, printer-&gt;pageRect());
      painter.end();*/
  }
  </code></pre>