c++qtqtextdocument

Qt - QTextDocument breaks word wrap if font is specified in html


I have to convert HTML into a pdf with QTextDocument but, if i specify the font in the html (that's what I need) the word wrap result broken.

To make this code compile add QT += printsupport in .pro

Example:

#include <QGuiApplication>
#include <QPrinter>
#include <QTextDocument>

int main(int argc, char *argv[])
{
  QGuiApplication app(argc, argv);

  QPrinter printer(QPrinter::PrinterResolution);
  printer.setPaperSize(QPrinter::A4);
  printer.setOrientation(QPrinter::Portrait);
  printer.setOutputFormat(QPrinter::PdfFormat);
  printer.setOutputFileName("test.pdf");

  QString html;
  html.append("<center><pre style=\"font-size:26pt;font-family:DejaVu Sans Mono\"><br />");
  html.append("<b>Test 1:</b><br />");
  html.append("AAAAA AAAAA AAAAA AAAAA AAAAA AAAAA AAAAA<br />");
  html.append("<em>--------------------</em><br />");
  html.append("<b>Test 2:</b><br />");
  html.append("BBBBB BBBBB BBBBB BBBBB BBBBB BBBBB BBBBB<br />");
  html.append("<em>--------------------</em><br />");
  html.append("</pre></center><br />");

  QTextDocument document;
  document.setPageSize(printer.pageRect().size());
  document.setHtml(html);

  document.print(&printer);

  return 0;
}

In this case, the style breaks word wrap.

Example 2:

#include <QGuiApplication>
#include <QPrinter>
#include <QTextDocument>
    
int main(int argc, char *argv[])
{
  QGuiApplication app(argc, argv);

  QPrinter printer(QPrinter::PrinterResolution);
  printer.setPaperSize(QPrinter::A4);
  printer.setOrientation(QPrinter::Portrait);
  printer.setOutputFormat(QPrinter::PdfFormat);
  printer.setOutputFileName("test.pdf");

  QString html;
  html.append("<center><br />");
  html.append("<b>Test 1:</b><br />");
  html.append("AAAAA AAAAA AAAAA AAAAA AAAAA AAAAA AAAAA<br />");
  html.append("<em>--------------------</em><br />");
  html.append("<b>Test 2:</b><br />");
  html.append("BBBBB BBBBB BBBBB BBBBB BBBBB BBBBB BBBBB<br />");
  html.append("<em>--------------------</em><br />");
  html.append("</center><br />");

  QTextDocument document;
  document.setPageSize(printer.pageRect().size());
  document.setDefaultFont(QFont("DejaVu Sans Mono", 26));
  document.setHtml(html);

  document.print(&printer);

  return 0;
}

In this case it works perfectly.

Am I missing something or is the style specification unusable with word wrap?

Thank you.


Solution

  • You just need to add following text to the style field of <pre>:

    white-space: pre-wrap
    

    like this:

    
    #include <QGuiApplication>
    #include <QPrinter>
    #include <QTextDocument>
    
    int main(int argc, char *argv[])
    {
      QGuiApplication app(argc, argv);
    
      QPrinter printer(QPrinter::PrinterResolution);
      printer.setPaperSize(QPrinter::A4);
      printer.setOrientation(QPrinter::Portrait);
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setOutputFileName("test.pdf");
    
      QString html;
      html.append("<center><pre style=\"font-size:26pt;font-family:DejaVu Sans Mono;white-space: pre-wrap;\"><br />");
      html.append("<b>Test 1:</b><br />");
      html.append("AAAAA AAAAA AAAAA AAAAA AAAAA AAAAA AAAAA<br />");
      html.append("<em>--------------------</em><br />");
      html.append("<b>Test 2:</b><br />");
      html.append("BBBBB BBBBB BBBBB BBBBB BBBBB BBBBB BBBBB<br />");
      html.append("<em>--------------------</em><br />");
      html.append("</pre></center><br />");
    
      QTextDocument document;
      document.setPageSize(printer.pageRect().size());
      document.setHtml(html);
    
      document.print(&printer);
    
      return 0;
    }