Qt生成PDF(图片生成,文本生成)

Qt做多了没有人逃得过导入导出pdf这个功能,有的话只是时候未到,注释都在代码里了,直接上代码了

图片生成:

QPrinter printer_pixmap(QPrinter::HighResolution);
    printer_pixmap.setPageSize(QPrinter::A4);  //设置纸张大小为A4
    printer_pixmap.setOutputFormat(QPrinter::PdfFormat);  //设置输出格式为pdf
    printer_pixmap.setOutputFileName(sFilePdf1);   //设置输出路径
    QPainter painter_pixmap;
    //sFilePix是自定义的照片
    QPixmap pixmap(sFilePix);
    painter_pixmap.begin(&printer_pixmap);
    QRect rect = painter_pixmap.viewport();
    int multiple = rect.width()/pixmap.width();
    painter_pixmap.scale(multiple, multiple); //将图像(所有要画的东西)在pdf上放大multiple-1倍
    painter_pixmap.drawPixmap(0, 0, pixmap);  //画图
    painter_pixmap.end();

文本生成:

QPrinter printer_text;

//    QPrinter printer_text(QPrinter::HighResolution);//加上QPrinter::HighResolution就会生成错误,他把pdf的分辨率默认为1200了
    printer_text.setOrientation(QPrinter::Landscape);
    printer_text.setPageSize(QPrinter::A4);  //设置纸张大小为A4
    printer_text.setOutputFormat(QPrinter::PdfFormat);  //设置输出格式为pdf
    printer_text.setOutputFileName(sFilePdf2);   //设置输出路径
    QPainter *painter = new QPainter(this);
    painter->begin(&printer_text);

    painter->setRenderHint(QPainter::Antialiasing, true);
    // 设置画笔颜色、宽度
    painter->setPen(QPen(QColor(255, 255, 255), 2));
    // 设置画刷颜色
    painter->setBrush(QColor(255, 255, 255));
    QRect rect(0,0,800,600);
    //整张图设置画刷白底
    painter->fillRect(rect,QColor(255, 255, 255));
    painter->drawRect(rect);
    //画数据部分的线条
    painter->setPen(QPen(QColor(0, 0, 0), 1));
    QVector lines;
    lines.append(QLine(QPoint(50,50),QPoint(750,50)));//上边
    lines.append(QLine(QPoint(750,50),QPoint(750,550)));//右边
    lines.append(QLine(QPoint(50,550),QPoint(750,550)));//下边
    lines.append(QLine(QPoint(50,50),QPoint(50,550)));//左边
    lines.append(QLine(QPoint(50,120),QPoint(750,120)));//名称下边
    lines.append(QLine(QPoint(50,180),QPoint(750,180)));//信息下边
    lines.append(QLine(QPoint(50,200),QPoint(750,200)));//条目下边
    lines.append(QLine(QPoint(50,490),QPoint(750,490)));//数据下边
    lines.append(QLine(QPoint(120,180),QPoint(120,200)));//类型分割边
    lines.append(QLine(QPoint(190,180),QPoint(190,200)));//类型分割边
    lines.append(QLine(QPoint(260,180),QPoint(260,200)));//类型分割边
    lines.append(QLine(QPoint(330,180),QPoint(330,200)));//类型分割边
    lines.append(QLine(QPoint(400,180),QPoint(400,490)));//结果和曲线分割边
    painter->drawLines(lines);
    QFont font;
    font.setPointSize(13);
    font.setFamily("黑体");

    font.setItalic(true);
    painter->setFont(font);
    //第一部分
    painter->drawText(50,50,700,40,Qt::AlignCenter,ui->lE_unit->text());//单位名称
    painter->drawText(QPoint(650,110),"No:");
    font.setPointSize(20);
    painter->setFont(font);
    painter->drawText(50,80,700,40,Qt::AlignCenter,ui->lE_report->text());//报告名称
    font.setPointSize(10);
    painter->setFont(font);
    //第二部分
    painter->drawText(50,120,700,60,Qt::AlignVCenter,QString("样本ID: ")+QString("11111111"));
    painter->drawText(210,120,700,60,Qt::AlignVCenter,QString("标本类型: ")+QString("ABC"));
    painter->drawText(370,120,700,60,Qt::AlignVCenter,QString("检测方法: ")+ui->lE_means->text());
    painter->drawText(600,120,700,60,Qt::AlignVCenter,QString("仪器: ")+ui->lE_instrument->text());
    //第三部分
    painter->drawText(50,180,70,20,Qt::AlignCenter,"序号");
    painter->drawText(120,180,70,20,Qt::AlignCenter,"检测项目");
    painter->drawText(190,180,70,20,Qt::AlignCenter,"Ct");
    painter->drawText(260,180,70,20,Qt::AlignCenter,"检测下限");
    painter->drawText(330,180,70,20,Qt::AlignCenter,"结果");
    //第四部分
    painter->drawText(50,210,70,20,Qt::AlignCenter,"1");
    painter->drawText(120,210,70,20,Qt::AlignCenter,"我");
    painter->drawText(190,210,70,20,Qt::AlignCenter,"真的");
    painter->drawText(260,210,70,20,Qt::AlignCenter,"不知道");
    painter->drawText(330,210,70,20,Qt::AlignCenter,"写啥");
    //第五部分
    painter->drawText(50,490,700,30,Qt::AlignVCenter,QString("送检者: ")+ui->lE_inspect->text());
    painter->drawText(230,490,700,30,Qt::AlignVCenter,QString("代码: ")+ui->lE_code->text());
    painter->drawText(410,490,700,30,Qt::AlignVCenter,QString("检验者: ")+ui->lE_detection->text());
    painter->drawText(590,490,700,30,Qt::AlignVCenter,QString("审核者: ")+ui->lE_audit->text());
    painter->drawText(50,520,700,30,Qt::AlignVCenter,QString("送检日期: ")+ui->lE_inspectDate->text());
    painter->drawText(410,520,700,30,Qt::AlignVCenter,QString("报告日期: ")+ui->lE_reportDate->text());

    painter->end();

 

你可能感兴趣的:(Qt)