Exporting a document to PDF: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(fix code rendering)
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
h1. Exporting a document to PDF
{{Cleanup|reason=No print support on mobile platform (iOS), different issues on different targets}}


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


For example, Consider the following snippet,
For a more advanced example that can handle HTML5, see [https://doc.qt.io/qt-6/qtwebengine-webenginewidgets-html2pdf-example.html Qt WebEngine Widgets html2PDF example]


<code> QString fileName = QFileDialog::getSaveFileName(this, tr(&quot;Save File&amp;quot;),<br /> &quot;untitled&amp;quot;,tr(&quot;PDF Document ('''.pdf)&quot;));<br /> QPrinter printer;<br /> printer.setOutputFormat(QPrinter::PdfFormat);<br /> printer.setOutputFileName(fileName);<br /> doc-&gt;print(&amp;printer); // doc is QTextDocument'''</code>
Here is a minimal example of how to print a {{DocLink|QTextDocument}} to PDF:


We can use the printer class to print the document to a file : a pdf file.
<syntaxhighlight lang="cpp">
#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QPrinter>
#endif


'''Categories'''<br />[[snippets]]
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);
}
</syntaxhighlight>
 
[[Category:snippets]]

Latest revision as of 23:25, 22 November 2022

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

For a more advanced example that can handle HTML5, see Qt WebEngine Widgets html2PDF example

Here 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);
}