There are several ways to create a PDF document in Qt. You already mentioned two of them. I propose some improvement for the QTextDocument
approach. Rather than manually writing a QTextDocument
, you can create it from HTML-formatted text.
QString html =
"<div align=right>"
"City, 11/11/2015"
"</div>"
"<div align=left>"
"Sender Name<br>"
"street 34/56A<br>"
"121-43 city"
"</div>"
"<h1 align=center>DOCUMENT TITLE</h1>"
"<p align=justify>"
"document content document content document content document content document content document content document content document content document content document content "
"document content document content document content document content document content document content document content document content document content document content "
"</p>"
"<div align=right>sincerly</div>";
QTextDocument document;
document.setHtml(html);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4);
printer.setOutputFileName("/tmp/test.pdf");
printer.setPageMargins(QMarginsF(15, 15, 15, 15));
document.print(&printer);
Warning: QTextDocument
supports a limited subset of HTML 4 markup.