Exporting a document to PDF

From Qt Wiki
Jump to: navigation, search

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:

  1. include <QtWidgets>
  2. ifndef QT_NO_PRINTER
  3. include <QPrinter>
  4. endif

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;

Hello, World!


Lorem ipsum dolor sit amet, consectitur adipisci elit.

   doc.setPageSize(printer.pageRect().size()); // This is necessary if you want to hide the page number