`QTextDocument` creating a singular table HTML to be printed to a PDF
Unsolved
General and Desktop
-
Hi,
I am trying to print to a PDF file a table like this:
using a code very similar to this:
void MainWindow::on_pushButton_clicked() { struct cell { char letter{0}; int16_t id_hori{-1}; int16_t id_vert{-1}; bool ver_and_hor() const { return (id_hori != -1) && (id_vert != -1); } bool ver() const { return (id_vert != -1); } bool hor() const { return (id_hori != -1); } }; using column = std::array<cell, 3>; using grid = std::array<column, 4>; const grid _grid{ column{cell{'B', 23, 45}, cell{'U'}, cell{'Y', -1, 9}}, column{cell{'I'}, cell{}, cell{'E'}}, column{cell{'L'}, cell{}, cell{'S'}}, column{cell{'L', 12}, cell{'O'}, cell{'W'}}, }; const int _cols{_grid[0].size()}; const int _rows{_grid.size()}; QString _html{"<!DOCTYPE html> " "<html> " "<head> " "<style> " "table { border-collapse: collapse; } " "td { border: 1px solid Gray; } " "td.special { border: none; } " "td.alignRight { text-align: right; } " "td.alignLeft { text-align: left; } " "</style> " "</head> " "<body> " "<table style=\"width:100%\"> "}; int _col_width = 100 / _rows; _html += "<tr> "; for (int _col = 0; _col < _cols; ++_col) { _html += "<th style=\"width: " + QString::number(_col_width) + "%\"></th> "; } _html += "</tr>"; for (int _row = 0; _row < _rows; ++_row) { _html += "<tr style=\"height:80px\">"; for (int _col = 0; _col < _cols; ++_col) { if (_grid[_row][_col].letter == 0) { _html += "<td class=\"special\"></td> "; } else if (_grid[_row][_col].ver_and_hor()) { _html += "<td <sup>" + QString::number(_grid[_row][_col].id_hori) + "</sup> <sup>" + QString::number(_grid[_row][_col].id_vert) + "</sup></td>"; } else if (_grid[_row][_col].hor()) { _html += "<td class=\"alignLeft\" <sup>" + QString::number(_grid[_row][_col].id_hori) + "</sup> </ td> "; } else if (_grid[_row][_col].ver()) { _html += "<td class=\"alignRight\" <sup>" + QString::number(_grid[_row][_col].id_vert) + "</sup></td> "; } else { _html += "<td></td> "; } } _html += "</tr> "; } _html += "</table> " "</body> " "</html> "; std::ofstream _out("/var/tmp/out.html"); _out << _html.toStdString(); QTextDocument _text; _text.setHtml(_html); QPdfWriter _pdf("/var/tmp/out.pdf"); _pdf.setPageSize(QPageSize::A4); _pdf.setPageOrientation(QPageLayout::Orientation::Portrait); _pdf.setPageMargins(QMargins(10, 10, 10, 10), QPageLayout::Millimeter); _text.print(&_pdf); }
Problems:
1 - In cells where must be a superscript number on the left and on the right, I can not use<td class=\"alignLeft\"...
and<td class=\"alignRight\"...
, so I don't know to align the numbers in this situation.
2 - The superscript number that stays on the left is not superscripted :
3 - In the PDF file the column size is not formatted correctly:
I have very basic knowledge in HTML and in the use of
QTextDocument
andQPdfWriter
, so I am sure I am doing something wrong, and I would mostly appreciate any help.TIA