运行效果:
代码:
#include <QPdfWriter>
#include <QPainter>
#include <QDesktopServices>
void Widget::on_pushButton_5_clicked()
{
//创建PDF文件
QString file_path = QApplication::applicationDirPath() + "/text.pdf";
QFile pdfFile(file_path);
pdfFile.open(QIODevice::WriteOnly);
QPdfWriter *pWriter = new QPdfWriter(&pdfFile);
pWriter->setPageSize(QPagedPaintDevice::A4);
pWriter->setResolution(300);
pWriter->setPageMargins(QMarginsF(30, 30, 30, 30));
QPainter *pPainter = new QPainter(pWriter);
QFont font[5] = {QFont("宋体", 26, 60), QFont("宋体", 26, 61), QFont("宋体", 26, QFont::Normal), QFont("宋体", 26, QFont::Normal), QFont("宋体", 26, QFont::Normal)};
font[0].setPixelSize(86);
font[1].setPixelSize(61);
font[2].setPixelSize(61);
font[3].setPixelSize(42);
font[4].setPixelSize(54);
qDebug()<<pPainter->viewport().width()<<" "<<pPainter->viewport().height();
int nPDFWidth = pPainter->viewport().width();
int nPDFHeight = pPainter->viewport().height();
//标题
int y = 10;
pPainter->setFont(font[0]);
pPainter->drawText(QRect(0, y, nPDFWidth, 100), Qt::AlignCenter, QString("这是第一个PDF文档"));
//画两条横线
y += 140;
pPainter->setPen(QPen(QBrush(QColor(0, 0, 0)), 5));
pPainter->drawLine(0, y, nPDFWidth, y);
pPainter->drawLine(0, y + 18, nPDFWidth, y + 18);
//添加文字
y += 60;
pPainter->setFont(font[2]);
pPainter->drawText(QRect(100, y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("器件型号: YT-CR810"));
pPainter->setFont(font[2]);
pPainter->drawText(QRect(nPDFWidth/2 + 100 , y, nPDFWidth/2 - 100, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("器件编号: YT-CR810"));
y += 90;
pPainter->setFont(font[2]);
pPainter->drawText(QRect(100, y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("装车车号: YT-CR810"));
pPainter->setFont(font[2]);
pPainter->drawText(QRect(nPDFWidth/2 + 100 , y, nPDFWidth/2 - 100, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("实验日期: YT-CR810"));
//小标题
y += 110;
pPainter->setFont(font[1]);
pPainter->drawText(QRect(0, y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "减 振 器 标 准 参 数");
//画表格
y += 120;
QStringList list;
list<<"速度"<<"压力"<<"拉力"<<"阻尼系数"<<"不对称率"<<"正允差"<<"负允差";
list<<"12.5m/s"<<"17KN"<<"15KN"<<"1.3kNs/m"<<"12%"<<"13%"<<"14%";
pdfDrawForm(pPainter, y, 0, 2, 7, 100, font[3], list);
y += 260;
pPainter->setFont(font[1]);
pPainter->drawText(QRect(0, y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "示功图");
//插入图片
y += 100;
int imageBorder = 150;
QPixmap pixmap = QPixmap::grabWidget(this, this->rect());
float x = (float)(nPDFWidth - imageBorder * 2) / (float)pixmap.width();
pixmap = pixmap.scaled(nPDFWidth - imageBorder * 2, x * pixmap.height(), Qt::IgnoreAspectRatio);
pPainter->drawPixmap(imageBorder, y, pixmap);
y += pixmap.height() + 90;
pPainter->setFont(font[1]);
pPainter->drawText(QRect(0, y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "减 震 器 测 试 结 果");
y += 120;
list.clear();
list<<"拉伸行程"<<"0.00mm"<<"压缩行程"<<"0.00mm"<<"最大速度"<<"0.000m/s"<<"阻尼系数"<<"0.00kNs/m"
<<"最大压力"<<"0.00kN"<<"压力偏差"<<"0.00%"<<"最大拉力"<<"0.00kN"<<"拉力偏差"<<"0.00%"
<<"不对称率"<<"0.00%"<<"运动时间"<<"5S"<<"实验结果"<<"100%"<<"曲线结论"<<"100";
pdfDrawForm(pPainter, y, 0, 3, 8, 100, font[3], list);
pPainter->setFont(font[4]);
pPainter->drawText(QRect(50, nPDFHeight, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "试验员: 张三"));
pPainter->drawText(QRect(nPDFWidth/3+50, nPDFHeight, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "检查员签字: "));
pPainter->drawText(QRect(nPDFWidth/3*2, nPDFHeight, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "签字日期: "));
//绘制完毕
delete pPainter;
delete pWriter;
pdfFile.close();
//通过其他PDF阅读器来打开PDF
QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));
}
void Widget::pdfDrawForm(QPainter *paint, int y, int horzBorder, int row, int column, int unitHeight, QFont &font, QStringList &list)
{
paint->setFont(font);
paint->setPen(QPen(QBrush(QColor(0, 0, 0)), 2));
int Width = paint->viewport().width() - horzBorder * 2;
int unitWidth = Width / column;
for(int i=0;i<row;i++) {
int x = horzBorder;
for(int j=0;j<column;j++) {
paint->drawText(QRect(x, y, unitWidth, unitHeight), Qt::AlignCenter, list[i*column + j]);
paint->drawRect(QRect(x, y, unitWidth, unitHeight));
x += unitWidth;
}
y += unitHeight;
}
}