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` creating a singular table HTML to be printed to a PDF
Forum Updated to NodeBB v4.3 + New Features

`QTextDocument` creating a singular table HTML to be printed to a PDF

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 148 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.
  • canellasC Offline
    canellasC Offline
    canellas
    wrote on last edited by
    #1

    Hi,

    I am trying to print to a PDF file a table like this:

    demo_000.png

    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>&nbsp;&nbsp;&nbsp;&nbsp;<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 : 3adbce54-258b-49bc-975c-25197fd4516c-image.png
    3 - In the PDF file the column size is not formatted correctly:
    6349dcda-dae6-42e7-b45f-ea5c3cc4163c-image.png

    I have very basic knowledge in HTML and in the use of QTextDocument and QPdfWriter, so I am sure I am doing something wrong, and I would mostly appreciate any help.

    TIA

    1 Reply Last reply
    0

    • Login

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