qt绘制生成PDF文件

这篇具有很好参考价值的文章主要介绍了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的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需要自己再重新实现。文章来源地址https://www.toymoban.com/news/detail-800341.html

到了这里,关于qt绘制生成PDF文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 十二、Qt 操作PDF文件(2)

    一、在《十、Qt 操作PDF文件-CSDN博客》中我们用Poppler类库打开了PDF文件,并显示到窗体上,但只能显示一页,功能还没完善,在本章节中,加入了: 通过选择框选择PDF文件并打开,默认打开第一页。 通过上一页、下一页按钮实现翻页功能。 通过第一页、最后一页、跳转到某

    2024年01月19日
    浏览(25)
  • QT自带PDF库的使用

          QT自带PDF库可以方便的打开PDF文件,并将文件解析为QImage,相比网上提供的开源库,QT自带PDF库使用更方便,也更加可靠,然而,QT自带PDF库的使用却不同于其他通用库的使用,具备一定的技巧。 1. 安装       QTPDF在QT中属于QWebEngine ,在安装时一定要选择“Qt WebEngine”

    2024年02月14日
    浏览(70)
  • java JFreeChart 绘制各种图形 并使用itextPdf生成PDF导出

    参考: https://blog.csdn.net/hfy1237/article/details/126693786 https://blog.csdn.net/qq_39149275/article/details/119381389

    2024年01月19日
    浏览(32)
  • QT技术实现Word模板编辑及转PDF

    本文详细介绍了如何使用QT技术进行Word模板的编辑,包括添加书签、替换文本和图片等操作,以及如何将Word文档转换为PDF格式。

    2024年02月12日
    浏览(34)
  • Java doc等文件生成PDF、多个PDF合并

    之前写过一遍文章是 图片生成PDF。 今天继续来对 doc等文件进行pdf合并以及多个pdf合并为一个pdf。 兄弟们,还是开箱即用。 依赖 示例代码 依赖 示例代码

    2024年02月10日
    浏览(32)
  • java生成pdf文件

    pom添加依赖 util工具类 controller层,返回前端流数据,前端自己下载文件

    2024年02月16日
    浏览(45)
  • Android 生成pdf文件

    使用官方的方式也就是PdfDocument类的使用 1.1 基本使用 注意事项 1.需要申请写入文件的权限 2.API最低是19,有api版本的限制 1.2 将根布局的内容生成pdf文件 也同样简单。binding.getRoot()就是xml文件的根布局 1.3 TextView有很多行,超过一屏 1.4 小结 对于Itext,主要有两个版本,一个是

    2024年02月10日
    浏览(26)
  • Springboot -- 按照模板生成docx、pdf文件,docx转pdf格式

    使用 poi-tl 根据模板生成 word 文件。 使用 xdocreport 将 docx 文件转换为 pdf 文件。 xdocreport 也支持根据模板导出 word ,但是 poi-tl 的功能更齐全,操作更简单,文档清晰。 poi-tl 、xdocreport 内部均依赖了 poi ,要注意两者中 poi 和 自身项目引用的 poi 的版本是否存在冲突。 使用 p

    2024年02月15日
    浏览(27)
  • Itext生成pdf文件,html转pdf时中文一直显示不出来

    尝试好多种方式,最后可能是跟字体有关系 字体设置为C:/Windows/Fonts/simhei.ttf  黑体,同时html页面上样式要添加 pdf生成方式参考项目:E:myfilesprojectgithubdemo-html2pdf 字体问题参考文章:https://blog.51cto.com/u_15127651/4527950 最后完美解决字体问题!!

    2024年02月20日
    浏览(33)
  • nodejs根据pdf模板填入中文数据并生成新的pdf文件

    首先 const templateBytes = await fs.promises.readFile(templatePath);   const pdfDoc = await PDFDocument.load(templateBytes);   const form = pdfDoc.getForm(); 这三行表示读文件,并且读取pdf表单,然后注册fontkit,将你要的字体嵌入pdf中,之后在pdf的表单中寻找字段,填入字段,并把字体样式更新为你嵌入pd

    2024年02月10日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包