title: Create PDF document for printing in Qt from template
body: <p>I write an application, when user inserts data in a dialog window (document title, sender name and address, etc) and then my application should generate a pdf file from this user data.</p>
  
  <p>PDF file should have defined layout, something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/tsGXC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/tsGXC.png" alt="enter image description here"></a></p>
  
  <p>I tried to do this with <code>QPdfWriter</code> but have problems aligning text in pdf. Here's my code:</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QtCore&gt;
  #include &lt;QPrinter&gt;
  #include &lt;QPdfWriter&gt;
  #include &lt;QPainter&gt;
  
  QString currDate()
  {
      QDate date = QDate::currentDate();
      return date.toString("dd.MM.yyyy");
  }
  
  void pdf(QString filename)
  {
      QPdfWriter writer(filename);
      writer.setPageSize(QPagedPaintDevice::A4);
      writer.setPageMargins(QMargins(30, 30, 30, 30));
  
      QPainter painter(&amp;writer);
      painter.setPen(Qt::black);
      painter.setFont(QFont("Times", 10));
  
      QRect r = painter.viewport();
  
      QString citydate = "City, ";
      citydate += currDate();
  
      painter.drawText(r, Qt::AlignRight, citydate);
  
  
  QString sender = "COMPANY XYZ\n";
  sender += "Random Street 12/314A\n";
  sender += "123-1232 City\n";
  
  painter.drawText(r, Qt::AlignLeft, sender);
      painter.end();
  }
  
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
  
      pdf("example1.pdf");
  
      return a.exec();
  }
  </code></pre>
  
  <p>Date printed to pdf is on the left, but:</p>
  
  <ol>
  <li>I have trouble with further text - how to "move" painter to
  different locations, to print also the sender name, document title,
  and document content inside the page (is the <code>translate</code> method of the painter enough, or can it be done simpler?)</li>
  <li>I don't know how to handle page breaks, in case the document content will be very long (will spread on 2 or more pages)</li>
  </ol>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I also tried the <code>QTextDocument</code> approach, but its hard to write any document, with almost any example available on the web. I came up only with this:</p>
  
  <pre><code>void pdf(QString filename)
  {
      QPrinter printer(QPrinter::PrinterResolution);
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setPaperSize(QPrinter::A4);
      printer.setOutputFileName(filename);
      printer.setPageMargins(QMarginsF(30, 30, 30, 30));
  
      QFont headerFont("Times New Roman", 8);
      QFont titleFont("Times New Roman", 14, QFont::Bold);
  
      QTextCharFormat txtformat = QTextCharFormat();
  
  
      QTextDocument doc;
      doc.setPageSize(printer.pageRect().size());
  
      QTextCursor* cursor = new QTextCursor(&amp;doc);
  
      txtformat.setFont(headerFont);
      cursor-&gt;insertText("Company XYZ", txtformat);
  
      cursor-&gt;movePosition(QTextCursor::Right &amp;&amp; QTextCursor::EndOfLine, QTextCursor::KeepAnchor, 1000);
      cursor-&gt;insertText(currDate(), txtformat);
  
      doc.print(&amp;printer);
  }
  </code></pre>