引言
之前做项目的时候,需要自己生成一个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需要自己再重新实现。