QPrinter unable to style HTML page properly to render image in PDF document
<p>I want to create a PDF document with properly styled HTML. This is what I am trying:</p>

<pre><code>QTextDocument *doc = new QTextDocument();
const char* s1 = "&lt;html&gt;&lt;head&gt;&lt;style&gt;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;}&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class = 'note-atomic'&gt;Hi&lt;/div&gt;&lt;img src = '/Users/attitude/Desktop/RnSghvV.png' width='400' height='300' /&gt;&lt;/body&gt;&lt;/html&gt;";
doc-&gt;setHtml(QString::fromStdString(s1));

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

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

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

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

doc-&gt;print(printer);
painter-&gt;end();
</code></pre>

<p>I expect the PDF to have 2 pages - first one with text <strong>Hi</strong>, and the next and last one with text <strong>Bye</strong>. And the first page styled like it appears in a HTML page with the same markup:</p>

<p><a href="https://i.stack.imgur.com/4aOI5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/4aOI5.png" alt="enter image description here"></a></p>

<p>However, the first page of the PDF comes up with content completely messed up:</p>

<p><a href="https://i.stack.imgur.com/o43Wm.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/o43Wm.jpg" alt="enter image description here"></a></p>

<p>How do I make it look as expected?</p>

<p>The image I am trying in the HTML doc: <a href="https://imgur.com/RnSghvV" rel="noreferrer">http://imgur.com/RnSghvV</a></p>

<p>Platform - Qt 5.3.1, 32 bit. OS X Yosemite.</p>