qt绘制生成PDF文件

引言

之前做项目的时候,需要自己生成一个pdf文件,好久之前保存的草稿,今天就把它发表一下吧,留着自己以后有需要的时候在来查阅。

QString ReportMainWindow::createPdfFile()
{QString strDirPath = QDir::tempPath() + "/TempPdf";QDir dir(strDirPath);if (!dir.exists()){bool bIsCreate = dir.mkdir(strDirPath);LOG_INFO("Create temp pdf dir");}QString strPdfFile = strDirPath + "/temp.pdf";QFile pdfFile(strPdfFile);  // 输出文件名if (!pdfFile.open(QIODevice::WriteOnly)){LOG_INFO("Cannot open file");return strPdfFile = "";}QPdfWriter *pdfWriter =new QPdfWriter(&pdfFile);  // 实例化QPdfWriter 可以设置PDF文件的一些参数pdfWriter->setPageSize(QPagedPaintDevice::A4);  // 设置纸张为A4纸pdfWriter->setResolution(QPrinter::ScreenResolution);  // 设置分辨率 屏幕分辨率 打印机分辨率// 高分辨率pdfWriter->setPageMargins(QMarginsF(30, 30, 30, 30));  // 设置页边距 顺序是:左上右下QPainter *pdfPainter = new QPainter(pdfWriter);  // qt绘制工具QRect viewRect = pdfPainter->viewport();int nWidth = viewRect.width();int nHeight = viewRect.height();LOG_INFO("----viewRect width:", viewRect.width(),"height:", viewRect.height());QRect reportRect = this->rect();int nReportWidth = reportRect.width();int nReportHeight = reportRect.height();// 设置标题QTextOption option(Qt::AlignCenter);        // 标题居中显示option.setWrapMode(QTextOption::WordWrap);  // 标题自动换行// 设置标题字体 需要使用QT的QFontQFont font;font.setFamily("宋体");  // 设置字体 微软雅黑、宋体之类的font.setPointSize(15);   // 设置字体大小font.setBold(false);     // 加粗pdfPainter->setFont(font);pdfPainter->drawText(QRect(170.00 / nReportWidth * nWidth, 40.00 / nReportHeight * nHeight,650.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("Coronary Image Reconstruction Report"), option);LOG_INFO("X", 371.00 / nReportWidth * nWidth, "Y",53.00 / nReportHeight * nHeight, "W",450.00 / nReportWidth * nWidth, "H",20.00 / nReportHeight * nHeight);option.setAlignment(Qt::AlignLeft);font.setPointSize(9);  // 设置字体大小font.setBold(false);   // 加粗pdfPainter->setFont(font);pdfPainter->drawText(QRect(89.00 / nReportWidth * nWidth, 75.00 / nReportHeight * nHeight,450.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("Institutional Information"));font.setPointSize(8);  // 设置字体大小font.setBold(false);   // 加粗pdfPainter->setFont(font);// 以上的内容可以直接拿来使用,按照自己的需求进行修改。// 下面的内容涉及到自己的项目中需要展示的参数,需要自己根据情况进行改变。pdfPainter->drawText(QRect(89.00 / nReportWidth * nWidth, 105.00 / nReportHeight * nHeight,130.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("Hospital name:"));// 这里的m_pPatientInfo.institutionName是一个结构体变量pdfPainter->drawText(QRect(169.00 / nReportWidth * nWidth, 105.00 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),m_pPatientInfo.institutionName);pdfPainter->drawText(QRect(664.00 / nReportWidth * nWidth, 105.00 / nReportHeight * nHeight,130.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("Reporting time:"));pdfPainter->drawText(QRect(744.00 / nReportWidth * nWidth, 105.00 / nReportHeight * nHeight,280.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),m_pPatientInfo.aquisitionTime);option.setAlignment(Qt::AlignLeft);font.setPointSize(9);  // 设置字体大小font.setBold(false);   // 加粗pdfPainter->setFont(font);pdfPainter->drawText(  // 105QRect(89.00 / nReportWidth * nWidth, 133.00 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("Patient Info"));font.setPointSize(8);  // 设置字体大小font.setBold(false);   // 加粗pdfPainter->setFont(font);pdfPainter->drawText(QRect(89.00 / nReportWidth * nWidth, 161.00 / nReportHeight * nHeight,300.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("name"));pdfPainter->drawText(QRect(139.00 / nReportWidth * nWidth, 161.00 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.patientName);pdfPainter->drawText(QRect(384.00 / nReportWidth * nWidth, 161.00 / nReportHeight * nHeight,50.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("gender"));pdfPainter->drawText(QRect(444.00 / nReportWidth * nWidth, 161.00 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.patientSex);pdfPainter->drawText(QRect(694.00 / nReportWidth * nWidth, 161.00 / nReportHeight * nHeight,61.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("Date of Birth"));pdfPainter->drawText(QRect(755.00 / nReportWidth * nWidth, 161.00 / nReportHeight * nHeight,130.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.patientBirthDate);pdfPainter->drawText(QRect(89.00 / nReportWidth * nWidth, 180.00 / nReportHeight * nHeight,50.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("patient ID"));pdfPainter->drawText(QRect(139.0 / nReportWidth * nWidth, 180.0 / nReportHeight * nHeight,150 / nReportWidth * nWidth, 17 / nReportHeight * nHeight),m_pPatientInfo.patientID);pdfPainter->drawText(QRect(384.00 / nReportWidth * nWidth, 180.00 / nReportHeight * nHeight,61.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("Date of inspection"));pdfPainter->drawText(QRect(444.0 / nReportWidth * nWidth, 180.0 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.aquisitionDate);pdfPainter->drawText(QRect(694.00 / nReportWidth * nWidth, 180.00 / nReportHeight * nHeight,61.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("Image Type"));pdfPainter->drawText(QRect(755.00 / nReportWidth * nWidth, 180.00 / nReportHeight * nHeight,61.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.modality);font.setPointSize(9);  // 设置字体大小font.setBold(false);   // 加粗pdfPainter->setFont(font);pdfPainter->drawText(QRect(90.00 / nReportWidth * nWidth, 233.00 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("Conclusion"));pdfPainter->drawText(QRect(674.00 / nReportWidth * nWidth, 233.00 / nReportHeight * nHeight,550.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("The results of the report are for clinicians' reference only"));QPen pen(QColor(128, 128, 128));pen.setWidth(4);pen.setStyle(Qt::SolidLine);pdfPainter->setPen(pen);pdfPainter->drawLine(QLineF(90.00 / nReportWidth * nWidth, 255.00 / nReportHeight * nHeight,903.00 / nReportWidth * nWidth, 255.00 / nReportHeight * nHeight));pen.setColor(Qt::black);font.setPointSize(10);  // 设置字体大小font.setFamily("宋体");font.setBold(false);  // 加粗pdfPainter->setPen(pen);pdfPainter->setFont(font);double dHeight = 0.00;int nEveryHeight = 0;int nRow1 = 0;QStringList strMsg1List = m_pPatientInfo.strMsg1.split(QString("\n"));drawConclussionImpression(dHeight, strMsg1List, nRow1, pdfPainter,nEveryHeight, dHeight, nReportWidth,nReportHeight, nWidth, nHeight);dHeight += 30.00 + nEveryHeight;  // qAbs fontRect.height()font.setPointSize(9);             // 设置字体大小font.setBold(false);              // 加粗pdfPainter->setFont(font);pdfPainter->drawText(QRect(90.00 / nReportWidth * nWidth, dHeight / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 20.00 / nReportHeight * nHeight),tr("imression"));// dHeight += 14;// pen.setWidth(1);// pen.setStyle(Qt::SolidLine);// pdfPainter->setPen(pen);// pdfPainter->drawLine(QLineF(//    90.00 / nReportWidth * nWidth, dHeight / nReportHeight * nHeight,//    883.00 / nReportWidth * nWidth, dHeight / nReportHeight * nHeight));dHeight += 28;font.setFamily("宋体");font.setPointSize(10);  // 设置字体大小font.setBold(false);    // 加粗pdfPainter->setFont(font);double dUpdateHeight = 0;int nRow = 0;QStringList strMsg2List = m_pPatientInfo.strMsg2.split(QString("\n"));drawConclussionImpression(dHeight, strMsg2List, nRow, pdfPainter,nEveryHeight, dUpdateHeight, nReportWidth,nReportHeight, nWidth, nHeight);font.setFamily("宋体");font.setPointSize(9);  // 设置字体大小pdfPainter->drawText(QRect(89.00 / nReportWidth * nWidth, 870.00 / nReportHeight * nHeight,130.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("Reporting time:"));font.setFamily("Times New Roman");pdfPainter->drawText(QRect(185.00 / nReportWidth * nWidth, 870.00 / nReportHeight * nHeight,450.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.aquisitionTime);font.setFamily("宋体");pdfPainter->drawText(QRect(400.00 / nReportWidth * nWidth, 870.00 / nReportHeight * nHeight,130.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("Reporting Doctor:"));pdfPainter->drawText(QRect(530.00 / nReportWidth * nWidth, 870.00 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.doctorName);pdfPainter->drawText(QRect(690.00 / nReportWidth * nWidth, 870.00 / nReportHeight * nHeight,130.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),tr("Reviewing physicians:"));pdfPainter->drawText(QRect(820.00 / nReportWidth * nWidth, 870.00 / nReportHeight * nHeight,150.00 / nReportWidth * nWidth, 17.00 / nReportHeight * nHeight),m_pPatientInfo.strReviewingPhysicians);pen.setColor(QColor(128, 128, 128));pen.setWidth(4);pen.setStyle(Qt::SolidLine);pdfPainter->setPen(pen);pdfPainter->drawLine(QLineF(90.00 / nReportWidth * nWidth, 887.00 / nReportHeight * nHeight,903.00 / nReportWidth * nWidth, 887.00 / nReportHeight * nHeight));delete pdfPainter;delete pdfWriter;pdfFile.close();//    QDesktopServices::openUrl(QUrl::fromLocalFile(strPdfFile));return strPdfFile;
}

注意

以上这段代码只是简单的提供了一些思路,真正项目中将将一个界面中的内容生成指定格式的pdf需要自己再重新实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/630342.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#封装服务

C#封装服务 新建服务项目;重构 OnStart 和 OnStop using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using S…

STM32 -- 串口引脚整理

一、引脚分配列表 串口端口发送引脚 (TX)接收引脚 (RX)USART1PA9PA10USART2PA2PA3USART3PB10PB11UART4PC10PC11UART5PC12PD2USART6PC6PC7 二、串口引脚的规律 STM32芯片上,引脚功能的布局安排,被设计得很有规律。 不同型号间&#x…

【Gradle】Maven-Publishing

使用Java开发完成一个模块或者一个基础框架需要提供给团队项目使用,这个时候有两种方式可提供,一是提供源码,二是提供编译构建好的jar包供使用,这个时候需要讲构建好的包发布到公司的私服(公司maven仓库)&a…

Python如何操作RabbitMQ实现fanout发布订阅模式?有录播直播私教课视频教程

fanout发布订阅模式 基本用法 生产者 import json import rabbitmq# 建立连接 credentials rabbitmq.PlainCredentials(zhangdapeng,zhangdapeng520, ) # mq用户名和密码 connection_target rabbitmq.ConnectionParameters(host127.0.0.1,port5672,virtual_host/,credent…

HCIP BGP(一)

任务: 1.R1上有两个环回,分别为192.168.1.0/24&192.168.2.0/24,只允许学到汇总&1.0 2.R7上有两个环回172.16.1.0/24&172.16.2.0/24,要求全部宣告,但是只有2.0可以通过 3.全网可达 拓扑图如下&#xff…

C语言经典算法之顺序查找算法

目录 前言 A.建议 B.简介 一 代码实现 二 算法时空复杂度 A.时间复杂度: B.空间复杂度: 三 优点和缺点 A.优点: B.缺点: 四 现实中的应用 前言 A.建议 1.学习算法最重要的是理解算法的每一步,而不是记住算…

常见的反爬虫风控 | 验证码风控

一.前言 在当今信息技术迅速发展的背景下,网站和在线服务面临着日益增长的自动化访问威胁,这些大多来自于各类爬虫程序。这种大量的自动化访问不仅对网站的正常运行构成压力,还可能导致敏感数据的泄露,甚至被用于不正当竞争和恶意…

多表关联查询

基本信息: 1.创建student和score表 CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY , name VARCHAR(20) NOT NULL , sex VARCHAR(4) , birth YEAR, department VARCHAR(20) , address VARCHAR(50) ); 创建score表。SQL代码如下&#…

PHP反序列化漏洞-魔术方法绕过

一、__wakeup()魔法函数绕过: 在PHP中,__wakeup()是一个魔术方法,用于在反序列化对象时自动调用。当反序列化字符串中的对象属性个数大于实际属性个数时,可以利用这个漏洞进行绕过。 触发条件: PHP版本为5.6.25或早期版本,或者PHP7版本小于7.0.10。反序列化字符串中的对…

C++大学教程(第九版)5.15修改GradeBook

目录 题目 代码 运行命令(在控制台输入) 运行截图 题目 (修改GradeBook)修改图5.9~图5.11所示的 GradeBook 程序,使它计算一组成绩的平均成绩。 成绩A为4分,成绩B为3分,依次类推。 A:4 B:3…

transbigdata 笔记: 轨迹密集化/稀疏化 轨迹平滑

1 密集化 transbigdata.traj_densify(data, col[Vehicleid, Time, Lng, Lat], timegap15) 轨迹致密化,保证至多每隔timegap秒都有一个轨迹点 这边插补使用的是pandas的interpolate,method设置的是index 1.1 举例 transbigdata 笔记: 官方…

开发实践7_文件上传

以下学习 朔宁夫 开发课(Python)。 文件上传&表单类 一 Django文件上传 表单上传。 前端:表单设置 enctype "multipart/form-data" 后端:获取上传文件对象 upload_dile request.FILES.get("文件域名…

《数据结构、算法与应用C++语言描述》-红黑树的C++实现-百万级数据量测试通过

红黑树 完整可编译运行代码见仓库:GitHub - Jasmine-up/Data-Structures-Algorithms-and-Applications/_35Red black tree。 如有问题请在评论区指出。另外,Github仓库会根据我的学习情况持续更新,欢迎大家点star,谢谢。 基本概…

【LeetCode】151. 反转字符串中的单词(中等)——代码随想录算法训练营Day08

题目链接:151. 反转字符串中的单词 题目描述 给你一个字符串 s ,请你反转字符串中 单词 的顺序。 单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。 返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。 …

【卡码网】55. 右旋转字符串——代码随想录算法训练营Day08

题目链接:55. 右旋转字符串 题目描述 字符串的右旋转操作是把字符串尾部的若干个字符转移到字符串的前面。给定一个字符串 s 和一个正整数 k,请编写一个函数,将字符串中的后面 k 个字符移到字符串的前面,实现字符串的右旋转操作…

YOLOv8改进 | 主干篇 | 低照度增强网络PE-YOLO改进主干(改进暗光条件下的物体检测模型)

一、本文介绍 本文给大家带来的改进机制是低照度图像增强网络PE-YOLO中的PENet,PENet通过拉普拉斯金字塔将图像分解成多个分辨率的组件,增强图像细节和低频信息。它包括一个细节处理模块(DPM),用于通过上下文分支和边缘分支增强图像细节,以及一个低频增强滤波器(LEF),…

力扣909. 蛇梯棋

广度优先搜索 动态规划 思路: 定义 pair {id, step} 为到达格子编号 id,使用的步数 step,记作 step[id];记录下所摇骰子 1 - 6 到达的格子编号 next,step[next] step[id] 1: 走了 1 步,所能…

指纹浏览器为什么要搭配代理IP?如何选择?

跨境电商无论是店群模式还是社媒矩阵运营,都必须涉及管理多个社媒/电商帐户的动作,但这很容易引发网站怀疑并最终被批量封号。使用指纹浏览器浏览器的主要目的是通过创建新的浏览器指纹来隐藏用户的真实浏览器指纹。 但浏览器指纹并不是网站关注的唯一…

ELK之Filebeat安装配置及日志抓取

一、Filebeat是什么 轻量型日志采集器 无论您是从安全设备、云、容器、主机还是 OT 进行数据收集,Filebeat 都将为您提供一种轻量型方法,用于转发和汇总日志与文件,让简单的事情不再繁杂。 Filebeat 随附可观测性和安全数据源模块,这些模块简化了常见格式的日志的收集、解…

边缘计算发展的瓶颈

上次我们讨论了边缘计算,并用无人零售和边缘计算的例子,说明了边缘计算的便利性。 但是边缘计算,也有很多限制。 硬件资源限制 边缘设备通常具有有限的计算、存储和网络资源。这种资源限制使得在处理大量数据或执行复杂任务时,…