侧边栏壁纸
博主头像
三味的小站 博主等级

世界上没有偶然,有的只是必然的结果。

  • 累计撰写 63 篇文章
  • 累计创建 14 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
Qt

Qt导出PDF(QPainter、QTextDocument)

三味线
2020-07-19 / 0 评论 / 0 点赞 / 16 阅读 / 0 字
// 添加打印支持
QT       += printsupport
// 包含头文件
#include <QDesktopServices>
#include <QFileDialog>
#include <QTextBlock>
#include <QTextDocument>
#include <QPainter>
#include <QtPrintSupport/QPrinter>
#include <QUrl>

通过QPainter导出

QString outputFile = QFileDialog::getSaveFileName(this, "Save File","D://report.pdf","*.pdf");
if (outputFile.isEmpty()) {
    return;
}
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPageSize(QPagedPaintDevice::A4);
printer.setOutputFileName(outputFile);
printer.setOrientation(QPrinter::Landscape);
printer.setPageMargins(10, 10, 10, 10, QPrinter::Millimeter);
QPainter painter;
painter.begin(&printer);
painter.drawText(QRect(0, 0, printer.width(), printer.height()), "你好,世界!");
printer.newPage();
painter.drawText(QRect(0, 0, printer.width(), printer.height()), "你好,世界!");
painter.end();
QDesktopServices::openUrl(QUrl(outputFile));

通过QTextDocument导出

QString outputFile = QFileDialog::getSaveFileName(this, "Save File","D://report.pdf","*.pdf");
if (outputFile.isEmpty()) {
    return;
}
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPageSize(QPagedPaintDevice::A4);
printer.setOutputFileName(outputFile);
printer.setOrientation(QPrinter::Landscape);
printer.setPageMargins(10, 10, 10, 10, QPrinter::Millimeter);
QString html = QString("<table width='600' border='1' align='center' frame='border' cellspacing='0' cellpadding='3px' rules='all'>");
html += "<tr>";
// 表头字段
QStringList headers;
headers<<"标题1"<<"标题2"<<"标题3"<<"标题4"<<"标题5"<<"标题6";
for (QString ti: headers) {
    html += QString("<th>%1</th>").arg(ti);
}
html += "</tr>";
// 数据
for (int i = 0; i < 10; i++) {
    html += "<tr>";
    for (int j = 0; j < 6; j++) {
        html += QString("<td>内容%1</td>").arg(j+1);
    }
    html += "</tr>";
}
html += "</table>";
QTextDocument textDoc;
textDoc.setHtml(html);
textDoc.print(&printer);
textDoc.end();
QDesktopServices::openUrl(QUrl(outputFile));

这里有一个问题是给table设的属性未生效,可能是我Qt版本及其兼容问题。

同时使用

如果要导出的内容既需要painter绘制,也包含富文本,那么这样导出:

QString outputFile = QFileDialog::getSaveFileName(this, "Save File","D://report.pdf","*.pdf");
if (outputFile.isEmpty()) {
    return;
}
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPageSize(QPagedPaintDevice::A4);
printer.setOutputFileName(outputFile);
printer.setOrientation(QPrinter::Landscape);
printer.setPageMargins(10, 10, 10, 10, QPrinter::Millimeter);
QPainter painter;
painter.begin(&printer);
painter.drawText(QRect(0, 0, printer.width(), printer.height()), "你好,世界!");
printer.newPage();
painter.drawText(QRect(0, 0, printer.width(), printer.height()), "你好,世界!");
QString html = QString("<table width='%1' border='1' align='center' frame='border' cellspacing='0' cellpadding='3px' rules='all'>").arg(printer.width());
html += "<tr>";
// 表头字段
QStringList headers;
headers<<"标题1"<<"标题2"<<"标题3"<<"标题4"<<"标题5"<<"标题6";
for (QString ti: headers) {
    html += QString("<th>%1</th>").arg(ti);
}
html += "</tr>";
// 数据
for (int i = 0; i < 10; i++) {
    html += "<tr>";
    for (int j = 0; j < 6; j++) {
        html += QString("<td>内容%1</td>").arg(j+1);
    }
    html += "</tr>";
}
html += "</table>";
printer.newPage();
QTextDocument textDoc;
textDoc.setHtml(html);
textDoc.drawContents(&painter);
textDoc.end();
painter.end();
QDesktopServices::openUrl(QUrl(outputFile));

这里还有另一种写法:

// 替换textDoc.drawContents(&painter);
QAbstractTextDocumentLayout::PaintContext ctx;
ctx.clip = QRectF( 0, 0, printer.width(), printer.height());
textDoc.documentLayout()->draw(&painter, ctx);

0

评论区