Exporting a document to PDF: Difference between revisions
Jump to navigation
Jump to search
m (Reentered Cleanup note) |
Waldyrious (talk | contribs) (fix/expand snippet to a full working example) |
||
Line 1: | Line 1: | ||
{{Cleanup|reason=No | {{Cleanup|reason=No print support on mobile platform (iOS), different issues on different targets}} | ||
{{LangSwitch}} | {{LangSwitch}} | ||
Below is a minimal example of how to print a {{DocLink|QTextDocument}} to PDF: | |||
<code lang="cpp"> | <code lang="cpp"> | ||
QString fileName = QFileDialog::getSaveFileName( | #include <QtWidgets> | ||
QPrinter printer; | #ifndef QT_NO_PRINTER | ||
printer.setOutputFormat(QPrinter::PdfFormat); | #include <QPrinter> | ||
printer.setOutputFileName(fileName); | #endif | ||
QTextDocument doc; | |||
doc | 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); | |||
printer.setOutputFormat(QPrinter::PdfFormat); | |||
printer.setPaperSize(QPrinter::A4); | |||
printer.setOutputFileName(fileName); | |||
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 | |||
doc.print(&printer); | |||
} | |||
</code> | </code> | ||
[[Category:snippets]] |
Revision as of 11:33, 15 October 2015
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. |
Below is a minimal example of how to print a QTextDocument to PDF:
#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QPrinter>
#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);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4);
printer.setOutputFileName(fileName);
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
doc.print(&printer);
}