QPrinter cannot correctly configure an HTML page to render an image in a PDF

I want to create a PDF document with properly formatted HTML. This is what I am trying:

QTextDocument *doc = new QTextDocument();
const char* s1 = "<html><head><style>body { font-family: sans-serif;}.note-atomic { background: rgb(242,242,242); width: 1000px; margin: 5px auto; border: 1px solid grey; border-radius: 8px;padding: 5px;}</style></head><body><div class = 'note-atomic'>Hi</div><img src = '/Users/attitude/Desktop/RnSghvV.png' width='400' height='300' /></body></html>";
doc->setHtml(QString::fromStdString(s1));

QPrinter *printer = new QPrinter();
printer->setOutputFileName("/Users/attitude/Desktop/fool.pdf");
printer->setOutputFormat(QPrinter::PdfFormat);

QPainter *painter = new QPainter();
painter->begin( printer );
doc->drawContents(painter, printer->pageRect().translated( -printer->pageRect().x(), -    printer->pageRect().y() ));
doc->print(printer);

const char* s2 = "<html><body><div>Bye</div></body></html>";
doc->setHtml(QString::fromStdString(s2));

printer->newPage();
doc->drawContents(painter, printer->pageRect().translated( -printer->pageRect().x(), - printer->pageRect().y() ));

doc->print(printer);
painter->end();

I expect there will be two pages in the PDF - the first with the text Hi , and the next and the last with the text Bye . And the first page, similar to it, is displayed on the HTML page with the same markup:

enter image description here

However, the completely damaged content appears on the first page of the PDF:

enter image description here

How do I look as expected?

The image I'm trying to use in an HTML document: http://imgur.com/RnSghvV

Platform - Qt 5.3.1, 32 bit. OS X Yosemite.

+4
2

QTextDocument. ( HTML + CSS).

, http://doc.qt.io/qt-5/richtext-html-subset.html (, css border p div).

, Qt Designer, html- QTextEdit.

HTML QWebView (http://doc.qt.io/qt-5/qwebframe.html#print)

webview->mainFrame()->print(printer);

+3

QTextDocument -. URL- . QVariant QTextDocument::loadResource(int type, const QUrl & name) QObject (QWidget is-a QObject!) QTextDocument . loadResource, QNetworkAccessManager .

, , . . , URL- , QVariant. , , , .

.

+1

Source: https://habr.com/ru/post/1626906/


All Articles