Exporting a document to PDF: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Decode HTML entity names)
No edit summary
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
Found it very easy to convert a {{DocLink|QTextDocument}} to PDF in Qt, and useful in scenarios like creating reports etc.


= Exporting a document to PDF =
For example, consider the following snippet:
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,
<code lang="cpp">
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "untitled", tr("PDF Document (....pdf)"));
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
QTextDocument doc;
doc->print(&printer);
</code>


<code> QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
[[Category:snippets]]
"untitled",tr("PDF Document ('''.pdf)"));
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
doc->print(&printer); // doc is QTextDocument'''</code>
 
We can use the printer class to print the document to a file : a pdf file.
 
'''Categories'''
[[snippets]]

Revision as of 08:04, 28 May 2015

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:

QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "untitled", tr("PDF Document (....pdf)"));
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
QTextDocument doc;
doc->print(&printer);