Exporting a document to PDF

Found it very easy to convert a QTextDocument to PDF in Qt. And useful in scenarios like creating reports etc.

For example, Consider the following snippet,

  1.        QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
  2.     "untitled",tr("PDF Document (*.pdf)"));
  3.     QPrinter printer;
  4.     printer.setOutputFormat(QPrinter::PdfFormat);
  5.     printer.setOutputFileName(fileName);
  6.     doc->print(&printer); // doc is QTextDocument *

We can use the printer class to print the document to a file : a pdf file.

Categories
Snippets
HowTo