title: How to set QTextDocument margins and other properties (setHTML, print to pdf)?
body: <p>I have the following <code>certificate</code> class for producing pdf document out of some images and data. After setting image sources, I call <code>generate()</code> function and get test.pdf output file. The document is created based on <code>QTextDocument</code> class using <code>setHtml(html)</code> method.</p>
  
  <p>The problem is that I have huge white spaces around the document, while I want the title 'REPORT' with logo image to be on the very top of the page. I would also like to add lower border to the table, but as I understand it is not supported by Qt (<a href="http://qt-project.org/doc/qt-4.8/richtext-html-subset.html" rel="noreferrer">Supported HTML Subset</a>).</p>
  
  <p>Python3 code:</p>
  
  <pre><code>class certificate:
  
  def __init__(self):
      self.logo = None
      pdffile = 'test.pdf'
      self.histogram = None
      self.printer = QPrinter()
      self.printer.setPageSize(QPrinter.Letter)
      self.printer.setOutputFormat(QPrinter.PdfFormat)
      self.printer.setOutputFileName(pdffile)
  
  def generate(self):
      document = QTextDocument()
      html = ""
      html += ('&lt;head&gt;&lt;title&gt;Report&lt;/title&gt;&lt;style&gt;&lt;/style&gt;&lt;/head&gt;'
                   '&lt;body&gt;&lt;table width="100%"&gt;&lt;tr&gt;'
                      '&lt;td&gt;&lt;img src="{}" width="30"&gt;&lt;/td&gt;'
                      '&lt;td&gt;&lt;h1&gt;REPORT&lt;/h1&gt;&lt;/td&gt;'
                   '&lt;/tr&gt;&lt;/table&gt;'
                   '&lt;p align=right&gt;&lt;img src="{}" width="300"&gt;&lt;/p&gt;'
                   '&lt;p align=right&gt;Sample&lt;/p&gt;&lt;/body&gt;').format(self.logo, self.histogram)
      document.setHtml(html)
      document.print_(self.printer)
  </code></pre>
  
  <p>I never extensively used html before and never worked with QTextDocument, and would appreciate any advice on how to control document margins and table properties.</p>
  
  <p>Other related property I want to control is resolution - I use pixel image size and need to know page and margin sizes in pixels.</p>
  
  <p><strong>EDITED:</strong> The question is almost answered by @mata. I can set now any margins and resolution, but do not understand how to control image and font sizes. E.g. if I need that an image is always 50mm wide, and html header and main text font sizes are visually the same - how to implement it?</p>
  
  <p><strong>EDITED2:</strong> The last part is solved too. Here is modified code by @mata, it gives the same result for any <code>dpi</code> value:</p>
  
  <pre><code>dpi=96
  
  document = QTextDocument()
  html = """
  &lt;head&gt;
      &lt;title&gt;Report&lt;/title&gt;
      &lt;style&gt;
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;table width="100%"&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;img src="{0}" width="{1}"&gt;&lt;/td&gt;
              &lt;td&gt;&lt;h1&gt;REPORT&lt;/h1&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;hr&gt;
      &lt;p align=right&gt;&lt;img src="{2}" width="{3}"&gt;&lt;/p&gt;
      &lt;p align=right&gt;Sample&lt;/p&gt;
  &lt;/body&gt;
  """.format('D:\Documents\IST Projects\diashape\docbook\Installation\images\istlogo_medium.png',
                40*dpi/96, 
                'D:\Documents\IST Projects\diashape\docbook\Installation\images\istlogo_medium.png', 
                200*dpi/96)
  
  document.setHtml(html)
  
  printer = QPrinter()
  font = QFont()
  font.setPointSize(12*dpi/96)
  document.setDefaultFont(font)
  printer.setResolution(dpi)
  ...
  </code></pre>