直接上代码,代码中添加注释进行解释,方便自己和大家研究。
首先是头文件
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. Netty和JDK源码来看Netty的NIO和JDK的NIO有什么不同
  2. 基于 abp vNext 和 .NET Core 开发博客项目 - 再说Swagger,分组、描述、小绿锁
  3. python3程序转python2_python2到python3代码转化:2to3
  4. Java中文字符所占的字节数
  5. Dubbo源码分析笔记-一(工程目录介绍)
  6. 人工智能或可称之为人造智能
  7. 单片机c语言实验报告心得,单片机实习心得体会
  8. Win Server2003常见问题的解决然方案
  9. C++ 设计模式 面向对象设计原则 相关概念介绍(结合UnrealEngine4)
  10. 中国历史朝代歌:夏商与西周,东周分两段
  11. OpenGL深入探索——广告牌(Billboard)和几何着色器
  12. 常用数据库的种类与特点
  13. 骗子公司 黑中介 黑到几时?
  14. mfc加载ocx失败
  15. 基于GPT硬盘模式重装win10操作系统
  16. 非华为手机可以刷鸿蒙系统吗,非华为手机用户,你愿意尝试鸿蒙系统吗?
  17. 【深挖字符串操作】·万字总结,这些知识点你真的懂了吗?
  18. BADI 第三代增强 BADI 和 NEW BADI
  19. avc水平什么意思_西方经济学中AVC是什么意识?
  20. php 302临时重定向,php实现301永久重定向和302临时重定向

热门文章

  1. python可以不对变量初始化_Python 可以不对变量如 a 初始化就可在表达式如 b=a+1 中使用该变量。...
  2. HTML+CSS静态页面网页设计作业——餐饮店铺(1页) web前端设计与开发期末作品_期末大作业
  3. Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day36】—— 实战那些事儿1
  4. 使用Python,OpenCV进行基本的图像处理——提取红色圆圈轮廓并绘制
  5. 微信中怎么打开apk下载链接 微信跳转打开外部浏览器打开apk文件
  6. 树莓派3B qt+mplayer制作音乐播放器(10)
  7. 朗润国际期货:02月03日周五《新闻联播》主要内容
  8. 音频质量的评价方法:简单梳理
  9. 原生js、css分别实现提示框渐渐消失的效果
  10. SEO之搜索引擎劫持