void MainWindow::on_actionSave_Document_triggered() { QString fileName = QFileDialog::getSaveFileName(this, "Save document...", qApp->applicationDirPath(), "*.pdf"); if (!fileName.isEmpty()) { QPrinter printer; printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(fileName); QMargins pageMargins(20, 20, 20, 20); #if QT_VERSION < QT_VERSION_CHECK(5, 3, 0) printer.setFullPage(false); printer.setPaperSize(QPrinter::A4); printer.setOrientation(QPrinter::Portrait); printer.setPageMargins(pageMargins.left(), pageMargins.top(), pageMargins.right(), pageMargins.bottom(), QPrinter::Millimeter); #else QPageLayout pageLayout; pageLayout.setMode(QPageLayout::StandardMode); pageLayout.setOrientation(QPageLayout::Portrait); pageLayout.setPageSize(QPageSize(QPageSize::A4)); pageLayout.setUnits(QPageLayout::Millimeter); pageLayout.setMargins(QMarginsF(pageMargins)); printer.setPageLayout(pageLayout); #endif #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) ui->textEdit->document()->setPageSize(printer.pageRect().size()); #else ui->textEdit->document()->setPageSize(printer.pageLayout().paintRectPixels(printer.resolution()).size()); #endif ui->textEdit->document()->print(&printer); } }