Exporting a document to PDF

From Qt Wiki
Revision as of 11:33, 15 October 2015 by Waldyrious (talk | contribs) (fix/expand snippet to a full working example)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: No print support on mobile platform (iOS), different issues on different targets
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Below is a minimal example of how to print a QTextDocument to PDF:

#include <QtWidgets>
#include <QPrinter>

int main(int argc, char *argv[])
    QApplication app(argc, argv);
    QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Export PDF", QString(), "*.pdf");
    if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); }

    QPrinter printer(QPrinter::PrinterResolution);

    QTextDocument doc;
    doc.setHtml("<h1>Hello, World!</h1>\n<p>Lorem ipsum dolor sit amet, consectitur adipisci elit.</p>");
    doc.setPageSize(printer.pageRect().size()); // This is necessary if you want to hide the page number