2

After an earlier stackoverflow discussion, I'm trying to generate a pdf with text and images using Qt and QTextDocument.

Here is my code as an MCVE:

#include <QApplication>
#include <QIcon>
#include <QDesktopServices>
#include <QWidget>
#include <QPrinter>
#include <QPainter>
#include <QPagedPaintDevice>
#include <QUrl>
#include <QFile>
#include <QTextDocument>

#include <sstream>
#include <memory>
#include <assert.h>

std::shared_ptr<QPrinter> getPrinter()
{
    std::shared_ptr<QPrinter> printer( new QPrinter( QPrinter::ScreenResolution ) );
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setOrientation(QPrinter::Portrait);
    printer->setPaperSize(QPrinter::A4);
    printer->setPageMargins(10.0,10.0,10.0,10.0,QPrinter::Millimeter);
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setColorMode(QPrinter::Color);
    printer->setFullPage( true );
    return printer;
}

bool generateReport( const std::string& fileName )
{
    auto printer = getPrinter();
    QSize pageSize = printer->pageRect().size();

    QTextDocument qtdoc;  // start with a QTextDocument
    qtdoc.setPageSize(pageSize);
    qtdoc.setDocumentMargin( 10 );

    std::stringstream str;
    str << "<html><head/><body>";
    str << "<table style=\"width: 100%\" border=\"1\"><tbody><tr><td>Foo</td><td>Bar</td></tr></tbody></table>";
    str << "</body></html>";

    qtdoc.setHtml(str.str().c_str());

    printer->setOutputFileName(fileName.c_str());
    qtdoc.print(printer.get());

    QDesktopServices::openUrl(QUrl::fromLocalFile(fileName.c_str()));

    return true;
}

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

    generateReport( "report.pdf" );

    return 0;
}

It does not work that bad, but the html content is not fitted to page: the table marked with "width=100%" does not actually occupy the full width of the pdf:

enter image description here

How can we force the html content to fit to page (meaning a table with width=100% should occupy the full page width!)

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • Are you sure you're producing valid HTML in the end? I'm suspicious because `static std::string htmlContent = "\ \` should be cutting off the leading `<` brackets, if I understand it right. – Maxim Paperno Dec 20 '17 at 17:56
  • Perhaps you could try setting margins on the QTextDocument itself? Though I've not had to do that in the past. What does `printer->pageRect().size()` return? Also, it would be easier for others to test this w/out QwtPlot involved (perhaps a static image of the plot?). – Maxim Paperno Dec 20 '17 at 18:15
  • @MaxPaperno: Simplified the example to isolate the problem. – jpo38 Dec 20 '17 at 19:12

1 Answers1

2

The problem was just that Qt does not recognize <table style=\"width: 100%\" border=1>

Changing it to <table width=100% border=1> fixed the issue, not the table occupies the full page width!

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • Good to hear! If you haven't seen it yet, you may find [this answer](https://stackoverflow.com/questions/10294692/how-to-set-qtextdocument-margins-and-other-properties-sethtml-print-to-pdf) useful for you font size/DPI issues. – Maxim Paperno Dec 20 '17 at 19:31
  • @MaxPaperno: Thanks, the `setDefaultFont(font)` is what I was looking for! – jpo38 Dec 20 '17 at 20:49