直接上代码,代码中添加注释进行解释,方便自己和大家研究。
首先是头文件
genpdf.h

#ifndef GENPDF_H
#define GENPDF_H#include <QObject>
#include <QWidget>
#include <QPdfWriter>
#include <QPainter>
#include <QDateTime>
#include <QImage>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QSqlQuery>
namespace Ui {class GenPdf;
}class GenPdf : public QWidget
{Q_OBJECTpublic:explicit GenPdf(QWidget *parent = nullptr);~GenPdf();void save_pdf();//设计单独的函数保存PDF,方便其他类进行调用private slots:void on_SavePdf_Btn_clicked();//设计单独的按钮对save_pdf进行调用private:Ui::GenPdf *ui;
};#endif // GENPDF_H

然后是函数实现
genpdf.cpp

#include "genpdf.h"
#include "ui_genpdf.h"
bool has_genPDF=false;
GenPdf::GenPdf(QWidget *parent) :QWidget(parent),ui(new Ui::GenPdf)
{ui->setupUi(this);setPalette(Qt::black); setAutoFillBackground(true);//强制设置背景颜色为黑色
}GenPdf::~GenPdf()
{delete ui;
}void GenPdf::save_pdf()
{ QString filePath="D:";extern QString username;//这个地方是我工程其他类中从数据库中获取的值QDateTime currentTime = QDateTime::currentDateTime();QString timeString = currentTime.toString("yyyy-MM-dd hh:mm:ss ddd");//获取时间QString filetime = currentTime.toString("yyyy-MM-dd_hh_mm_ss_ddd");//获取时间std::string fileName = filePath.toStdString() + "\\Report_by_"+username.toStdString()+"_"+filetime.toStdString()+".pdf";//PDF文件名QString fileNameString = QString::fromStdString(fileName);QFile pdfFile(fileNameString);pdfFile.open(QIODevice::WriteOnly);QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile);//创建PDFWriter才能绘制pdf//@像素3508*2479,分辨率决定了像素大小,便于后续的排版布局//@ 这里操作的所有x,y坐标,都是像素,即在3508*2479的基础上进行操作pdfWriter->setResolution(300);pdfWriter->setTitle("Report");int pageMargin = 0;pdfWriter->setPageMargins(QMarginsF(pageMargin, pageMargin, pageMargin, pageMargin));//设置pdf四周的空白QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter);//字体排版方式,左右,上下居中option.setWrapMode(QTextOption::WordWrap);int yCurrentP = 0;//定义当前操作位置方便后续的画图int xCurrentP = 0;int contentWidth = 2479 - pageMargin;QFont font;font.family();//font.setFamily("simsun.ttc");//font.setFamily("Times New Roman");font.setFamily("楷体");QPainter *pdfPainter = new QPainter(pdfWriter);//创建qpainter,绑定pdfwriter,用于绘制图片,文字,线条等int fontSize =9;int textHeight = 90;font.setPointSize(fontSize);pdfPainter->setFont(font);/** 第一页表头部分 **///设置页眉左上角的logostd::string titleImageFileName = ":/new/IMG/logo.png";//图片名称,可更换为自己的图片路径,这个地方要从资源文件进行添加,这个路径都是自己命名的,不要照搬QPixmap logo;logo.load(QString::fromStdString(titleImageFileName));//加载图像pdfPainter->drawPixmap(0, 0, logo.width()/4, logo.height()/4, logo);//绘制图像,缩放yCurrentP += (logo.height()/4 + 4);//把画笔位置设定在logo图片的下面QPen pen;//声明画笔主要用来设置画笔粗细int penHeigth = 4;pen.setWidth(penHeigth);//设置画笔粗细pen.setColor(Qt::blue);//设置画笔颜色为蓝色pdfPainter->setPen(pen);//让设置生效pdfPainter->drawLine(0, yCurrentP, contentWidth, yCurrentP);//画线pen.setColor(Qt::black);//设置画笔颜色为黑色pdfPainter->setPen(pen);//设置生效yCurrentP += penHeigth;//设置PDF文件标题fontSize =22; //设置字体大小font.setPointSize(fontSize);pdfPainter->setFont(font);//设置生效option.setAlignment(Qt::AlignCenter);//设置居中对齐pdfPainter->drawText(QRect(0, 40, contentWidth, textHeight),QString("涉密场所无线局域网检测报告"), option);/** 表头结束 **/fontSize = 16;font.setPointSize(fontSize);pdfPainter->setFont(font);option.setAlignment(Qt::AlignLeft);pdfPainter->drawText(QRect(10,yCurrentP+20,contentWidth,textHeight),QString("1.设备信息"),option);pdfPainter->drawText(QRect(10,yCurrentP+20+5*textHeight,contentWidth,textHeight),QString("2.检测任务信息"),option);pdfPainter->drawText(QRect(10,yCurrentP+20+11*textHeight,contentWidth,textHeight),QString("3.检测结论"),option);fontSize = 13;font.setPointSize(fontSize);pdfPainter->setFont(font);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+1.5*textHeight,contentWidth,textHeight),QString("软件版本:V1.0"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+2.5*textHeight,contentWidth,textHeight),QString("序列号:TOECSEC001"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+3.5*textHeight,contentWidth,textHeight),QString("校准日期:2022-10-10"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+6.5*textHeight,contentWidth,textHeight),"受检单位:"+company,option);//logo旁输出受检单位pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+7.5*textHeight,contentWidth,textHeight),"检测员:"+Inspector,option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+8.5*textHeight,contentWidth,textHeight),"检测地点:"+locate,option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+9.5*textHeight,contentWidth,textHeight),"检测时间:"+timeString,option);//右对齐输出系统时间pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+12.5*textHeight,contentWidth,textHeight),QString("对该涉密场所无线局域网检测分析结果为:"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+13.5*textHeight,contentWidth,textHeight),QString("a) 该场所内部共有无线局域网接入点2")+QString("个,其中频段在2.4GHz的有1个,5GHz的有1个;"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+14.5*textHeight,contentWidth,textHeight),QString("b) 该场所内部共有活跃终端1")+QString("个,其中频段在2.4GHz的有1个,5GHz的有0个;"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+15.5*textHeight,contentWidth,textHeight),QString("c) 该场所存在非活跃终端")+QString("个;"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+16.5*textHeight,contentWidth,textHeight),QString("d) 该场所存在外部局域网接入点;"),option);pdfPainter->drawText(QRect(10+1.5*textHeight,yCurrentP+17.5*textHeight,contentWidth,textHeight),QString("e) 装备检测到的无线局域网接入点、活跃终端及其拓扑关系详见下页。"),option);//创建新的一页PDF保存表格pdfWriter->newPage();/** 第二页表头部分 **/yCurrentP = 0;xCurrentP = 0;fontSize =9;textHeight = 90;font.setPointSize(fontSize);pdfPainter->setFont(font);logo.load(QString::fromStdString(titleImageFileName));//加载图像pdfPainter->drawPixmap(0, 0, logo.width()/4, logo.height()/4, logo);//绘制图像,缩放三倍//这里的company ,Inspector,locate都来自于我自己的数据,根据实际情况修改不要照抄.pdfPainter->drawText(QRect(logo.width()/4, 0,contentWidth-15,logo.height()/4),"受检单位:"+company,Qt::AlignLeft | Qt::AlignBottom);//logo旁输出受检单位option.setAlignment(Qt::AlignRight | Qt::AlignBottom);//设置右对齐pdfPainter->drawText(QRect(0,0,contentWidth-15,logo.height()/4-80),"检测员:"+Inspector,option);pdfPainter->drawText(QRect(0,0,contentWidth-15,logo.height()/4-40),"检测地点:"+locate,option);pdfPainter->drawText(QRect(0,0,contentWidth-15,logo.height()/4),"检测时间:"+timeString,option);//右对齐输出系统时间yCurrentP += (logo.height()/4 + 4);//把画笔位置设定在logo图片的下面pen.setWidth(penHeigth);//设置画笔粗细pen.setColor(Qt::blue);//设置画笔颜色为蓝色pdfPainter->setPen(pen);//让设置生效pdfPainter->drawLine(0, yCurrentP, contentWidth, yCurrentP);//画线pen.setColor(Qt::black);//设置画笔颜色为黑色pdfPainter->setPen(pen);//设置生效yCurrentP += penHeigth;//设置PDF文件标题fontSize =22;font.setPointSize(fontSize);pdfPainter->setFont(font);option.setAlignment(Qt::AlignCenter);//设置居中对齐pdfPainter->drawText(QRect(0, 40, contentWidth, textHeight),QString("分析报表"), option);/** 表头结束 **//** 第一个表格 **/fontSize = 16;font.setFamily("黑体");font.setPointSize(fontSize);pdfPainter->setFont(font);option.setAlignment(Qt::AlignCenter);yCurrentP+=50;pdfPainter->drawText(QRect(xCurrentP,yCurrentP,contentWidth,textHeight),QString("表1 无线局域网接入点信息表"),option);//打印表格 先设置表格起始位置int tableX = xCurrentP + 50;int tableY = yCurrentP + 150;//设置表格的行数,行高,列数,列宽。int row = 3;int rowHeight = 100;//row*rowHeight不能超过3240这个数int col = 7;int colWidth = 330;//col*colWidth不能超过2380这个数//画线pen.setColor(Qt::red);pdfPainter->setPen(pen);for(int i = 0; i <= row; i++){pdfPainter->drawLine(tableX,tableY+i*rowHeight,tableX+col*colWidth,tableY+i*rowHeight);}for(int i = 0; i <= col; i++){pdfPainter->drawLine(tableX+i*colWidth,tableY,tableX+i*colWidth,tableY+row*rowHeight);}pen.setColor(Qt::black);//设置为黑色pdfPainter->setPen(pen);font.setFamily("宋体");fontSize = 11;font.setPointSize(fontSize);pdfPainter->setFont(font);//使字体设置生效option.setAlignment(Qt::AlignVCenter |Qt::AlignHCenter );//设置文字居中对齐/** 填写表头信息 **/pdfPainter->drawText(QRect(tableX,tableY,colWidth,rowHeight),QString("ESSID"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+colWidth,tableY,colWidth,rowHeight),QString("BSSID"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+2*colWidth,tableY,colWidth,rowHeight),QString("频段"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+3*colWidth,tableY,colWidth,rowHeight),QString("信道"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+4*colWidth,tableY,colWidth,rowHeight),QString("接收信号强度"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+5*colWidth,tableY,colWidth,rowHeight),QString("加密方式"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+6*colWidth,tableY,colWidth,rowHeight),QString("是否在场所内部"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。/** 填入数据 **//** 第二个表格 **/yCurrentP=yCurrentP+rowHeight*row+150;fontSize = 16;font.setFamily("黑体");font.setPointSize(fontSize);pdfPainter->setFont(font);option.setAlignment(Qt::AlignCenter);yCurrentP+=50;pdfPainter->drawText(QRect(xCurrentP,yCurrentP,contentWidth,textHeight),QString("表2 活跃终端信息表"),option);tableX=xCurrentP+450;tableY=yCurrentP+150;pen.setColor(Qt::red);pdfPainter->setPen(pen);for(int i = 0; i <= row; i++){pdfPainter->drawLine(tableX-150,tableY+i*rowHeight,tableX+5*colWidth,tableY+i*rowHeight);}pdfPainter->drawLine(tableX-150,tableY,tableX-150,tableY+row*rowHeight);//第一列要宽一点for(int i = 1; i <= 5; i++){pdfPainter->drawLine(tableX+i*colWidth,tableY,tableX+i*colWidth,tableY+row*rowHeight);}pen.setColor(Qt::black);//设置为黑色pdfPainter->setPen(pen);font.setFamily("宋体");fontSize = 11;font.setPointSize(fontSize);pdfPainter->setFont(font);//使字体设置生效option.setAlignment(Qt::AlignVCenter |Qt::AlignHCenter );//设置文字居中对齐pdfPainter->drawText(QRect(tableX-150,tableY,colWidth+150,rowHeight),QString("MAC地址"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+colWidth,tableY,colWidth,rowHeight),QString("频段"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+2*colWidth,tableY,colWidth,rowHeight),QString("信道"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+3*colWidth,tableY,colWidth,rowHeight),QString("接收信号强度"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。pdfPainter->drawText(QRect(tableX+4*colWidth,tableY,colWidth,rowHeight),QString("是否在场所内部"),option);//将文字写入表格 画笔后移或者下移均以列宽或行高为单位进行加减。/** 填入数据 **//** 第三个表格 **/yCurrentP=yCurrentP+rowHeight*row+150;fontSize = 16;font.setFamily("黑体");font.setPointSize(fontSize);pdfPainter->setFont(font);option.setAlignment(Qt::AlignCenter);yCurrentP+=50;pdfPainter->drawText(QRect(xCurrentP,yCurrentP,contentWidth,textHeight),QString("表3 无线局域网拓扑表"),option);//@ 完成,删除对象delete pdfPainter;delete pdfWriter;pdfFile.close();//关闭ui->Tips->setText("已在默认路径下生成PDF");
}void GenPdf::on_SavePdf_Btn_clicked()
{save_pdf();
}

pdf的效果如下,logo和序列号已被打码。


数据的填入参考表头的填入方式,每次挪动一个列宽行高什么的就可以了。

记录一个有关QT生成PDF的过程相关推荐

  1. SQLServer中一个多用户自动生成编号的过程

    SQLServer中一个多用户自动生成编号的过程 if not exists (select * from dbo.sysobjects where id = object_id(N'[IndexTa ...

  2. 分享一个为js生成PDF添加水印的小技巧

    分享一个为js生成PDF添加水印的小技巧 程序员工作中有时我们会遇到这样的情况--客户为了生成档案的版权问题,要为生成的文本 或者页面添加版权水印,以HTML代码页面生成PDF文件为例,介绍一下添加水 ...

  3. (十七)QT生成PDF文件

    在实际情况中,我们有时候会遇到需要把txt.html或者图片变成PDF文件的情况,例如把检测结果生成PDF文档给客户等等.QT4使用QPrinter来实现这个功能,QT5修改为QPdfWriter这个 ...

  4. qt生成pdf(用图片qgrabwidget抓取图片生成PDF;用文字生成pdf)

    1.用图片生成PDF: QPrinter printerPixmap(QPrinter::HighResolution);printerPixmap.setPageSize(QPrinter::A4) ...

  5. 【编解码】记录一个ffmpeg解码生成YUV的 color range 问题,以及video_full_range_flag用法。

    1. 问题起因 注: 本文软件版本:ffmpeg 4.4 .安霸cv2x SDK 3.0.9 最近有客户在使用我们芯片的avc/hevc 编码器的提了一个问题,很有意思,我花了2天来找原因和做实验.现 ...

  6. Qt 生成pdf文件

    运行效果: 代码: #include <QPdfWriter> #include <QPainter> #include <QDesktopServices>voi ...

  7. 记录一下LibreOffice word生成pdf由于特殊字符报错的问题

    业务大致是,输入表单然后生成word和pdf word没有问题,pdf由于有特殊字符&所以导致生成失败. word本身就是xml所以报错的根本原因是xml不能直接使用 &.<.& ...

  8. 记录一个简单的ASM程序破解过程

    1.双击打开程序 随意输入(333),点击check,弹出MessageBox 2.将程序拖入PEid,查看编译器.链接器.PE区段.导入表等.      导入表中函数如下: 3.将程序拖入OD中,根 ...

  9. 记录一个常用函数fetchall()的使用过程

    fetchall() 作用是返回多个元组,即对应数据库里的多条数据概念: 常见用法是 cursor.execute('select * from table') value = cursor.fetc ...

最新文章

  1. PLSQL developer 连接不上64位Oracle 解决办法
  2. 做中间件的这两年总结(201704-201905)
  3. 小学计算机ppt课教案,小学信息技术公开课教案《让幻灯片变得更加漂亮》教学设计与反思...
  4. (C语言)链表的实现集合的相关操作
  5. python虚拟机 基于寄存器_虚拟机随谈(一):解释器,树遍历解释器,基于栈与基于寄存器,大杂烩...
  6. android本页切换子页,android ViewPager控件实现手势滑动切换页签-Fun言
  7. Git 代码管理常用命令
  8. docker mysql扩容_docker pull mysql 时报错,说空间不足
  9. linux rsync配置文件参数详解
  10. 电力系统及其自动化毕业论文题目【精选】
  11. 面试必问:TCP和UDP的区别(附面试题)
  12. 迅为RK3399开发板嵌入式linux开发指南
  13. CEO “排队”卸任、企业“扎堆”造车,2021 科技圈十大事件你知道几个?
  14. wifi连接状态android,判断android设备wifi连接状态
  15. 浏览器(1):CSDN的浏览器助手使用推荐
  16. 从零开始学C++之标准库类型(一):string 类简介和例程
  17. Elasticsearch集群“脑裂”现象
  18. 关于html中文显示乱码
  19. HTML乘法器制作,一种单象限乘法器的制作方法
  20. Maven基础简介+安装说明

热门文章

  1. 十一、决策树和随机森林
  2. 最小二乘的应用1-不相容方程组
  3. JadePool应用范例:实现中国行政区划管理
  4. 宝塔修改网站中的文件后未生效
  5. 【中间件技术】第二部分 CORBA规范与中间件(4) 编写对象接口
  6. Buffer Overflow Vulnerability Prediction from x86 executables using Static Analysis and ML
  7. HTML5期末大作业:基于html+css+javascript+jquery实现藏族文化15页【学生网页设计作业源码】
  8. 职业规划要看的书单。
  9. 一、软著专利查询网站
  10. u8链接提示计算机拒绝,用友u8软件error错误拒绝访问