Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Exporting a document to PDF/pt

From Qt Wiki
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Não há suporte para aplicações móveis (iOS), diferentes problemas em diferentes platafomas
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

A seguir há um simples exemplo de como imprimir um QTextDocument em 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, "Exportar 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>Olá, Mundo!</h1>\n<p>Lorem ipsum dolor sit amet, consectitur adipisci elit.</p>");
    doc.setPageSize(printer.pageRect().size()); // Isto é necessário se você deseja esconder o número da página
    doc.print(&printer);
}