QT生成PDF文档

时间:2024-05-18 19:09:39
//生成PDF文档,str为传入的学号
CreatePDF(QVector<QString> str)
{
    //保存程序当前路径
    QString currentDir = QDir::currentPath();
    //生成pdf保存的路径
    QString file_path = QFileDialog::getExistingDirectory(this,"请选择保存路径","C:\\Users\\Administrator\\Desktop");
    if(file_path.isEmpty())
    {
        return;
        QMessageBox::warning(this,"提示","路径不能为空");
    }

    //将程序的执行路径设置到filePath下
    QDir tempDir;
    tempDir.setCurrent(file_path);

    //qDebug()<<tempDir.currentPath();

    //进度条
    QProgressBar *m_pProgressBar = new QProgressBar(this);
    m_pProgressBar->setOrientation(Qt::Horizontal);  // 水平方向
    m_pProgressBar->setMinimum(1);  // 最小值
    m_pProgressBar->setMaximum(str.size());  // 最大值
    m_pProgressBar->setMaximumHeight(15);
    m_pProgressBar->setMinimumWidth(60);
    m_pProgressBar->setVisible(true);

    for(int i=0;i<str.size();i++)
    {
        m_pProgressBar->setValue(i);

        QCoreApplication::processEvents();
        QPixmap qrimage=GenerateQRcode(str.at(i));

        //Qt5 pdfwriter生成pdf
        QString fileName=str.at(i)+".pdf";
        //qDebug()<<str.at(i);

        QFile pdfFile(fileName);
        //判断文件是否存在
        if(QFile::exists(fileName))
        {

            QMessageBox::StandardButton reply;
            reply = QMessageBox::question(this, "提示", "文件已经存在,点击是将覆盖原文件", QMessageBox::Yes | QMessageBox::No);
            if(reply == QMessageBox::Yes)
            {
                continue;
            }
            else
            {
                return;
            }
        }
        else
        {
            // 打开要写入的pdf文件
            pdfFile.open(QIODevice::WriteOnly);

            QPdfWriter* pPdfWriter = new QPdfWriter(&pdfFile);  // 创建pdf写入器
            pPdfWriter->setPageSize(QPagedPaintDevice::A4);     // 设置纸张为A4
            pPdfWriter->setResolution(300);                     // 设置纸张的分辨率为300,因此其像素为3508X2479

            int iMargin = 60;                   // 页边距
            pPdfWriter->setPageMargins(QMarginsF(iMargin, iMargin, iMargin, iMargin));

            QPainter* pPdfPainter = new QPainter(pPdfWriter);   // qt绘制工具
            // 标题,居中
            QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter);
            option.setWrapMode(QTextOption::WordWrap);

            //二维码
            pPdfPainter->drawPixmap(1600,70,qrimage);
            //字体
            QFont font;
            font.setFamily("simhei.ttf");
            //标题,字号
            int fontSize = 22;
            font.setPointSize(fontSize);
            pPdfPainter->setFont(font);                    // 为绘制工具设置字体
            pPdfPainter->drawText(QRect(0, 0, 1980, 100),Qt::AlignHCenter|Qt::AlignBottom,
                                  "青少年儿童健康体检表");
            //option.setWrapMode(QTextOption::WordWrap);
            //画表格

            pPdfPainter->setFont(QFont("", 10));
            pPdfPainter->drawText(20,180,"姓名:");
            pPdfPainter->drawText(500,180,"性别:");
            pPdfPainter->drawText(1000,180,"年龄:");
            pPdfPainter->drawText(20,250,"班级:");

            QDate date=QDate::currentDate();
            QString create_time=date.toString("yyyy/MM/dd");
            pPdfPainter->drawText(500,250,QString("体检日期:%1").arg(create_time));
            pPdfPainter->drawText(20,320,QString("编号:%1").arg(str.at(i)));

            // 标题下边留白
            int iTop = 350;
            // 左侧缩进
            int iLeft = 20;
            // 设置画笔颜色、宽度
            //pPdfPainter.setPen(QPen(QColor(0, 160, 230), 2));
            pPdfPainter->setPen(2);
            // 设置画刷颜色
            //pPdfPainter->setBrush(QColor(255, 160, 90));
            pPdfPainter->drawRect(iLeft, iTop, 1980, 2750);//表格矩形方框
            pPdfPainter->drawLine(iLeft,iTop+100,iLeft+1980,iTop+100);
            pPdfPainter->drawLine(iLeft,iTop+500,iLeft+1980,iTop+500);
            pPdfPainter->drawLine(iLeft,iTop+1000,iLeft+1980,iTop+1000);
            pPdfPainter->drawLine(300,iTop,300,iTop+2750);
            pPdfPainter->drawText(iLeft+50,iTop+60,"检查项目");
            pPdfPainter->drawText(iLeft+900,iTop+60,"内容");
            pPdfPainter->drawText(iLeft+50,iTop+260,"体成分");
            pPdfPainter->drawText(iLeft+50,iTop+760,"脊柱");
            pPdfPainter->drawText(iLeft+50,iTop+1260,"骨龄");

            pPdfWriter->newPage();
            pPdfPainter->drawText(50,1260,"骨龄");

            delete pPdfPainter;
            delete pPdfWriter;
            pdfFile.close();
            //将程序当前路径设置为原来的路径
        }
    }
    tempDir.setCurrent(currentDir);
    delete m_pProgressBar;
    QMessageBox::warning(this,"提示","导出完成");
}

实现功能,根据传入ID批量生成A4PDF文档并带有二维码,

效果图:

QT生成PDF文档