qt绘制生成PDF文件

本文介绍了如何使用Qt库在QStringReportMainWindow类中创建PDF文件,包括设置页面尺寸、分辨率、标题和内容区域,如医院信息、患者信息等,供项目开发参考。
摘要由CSDN通过智能技术生成

引言

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

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肩上风骋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值