需求

qt 需要打印 pdf

解决

简单的打印 pdf,可以使用 QPrinter.

void Printer::Print(QPrinter *printer)
{
    qDebug() << "Printer::Print";
    QPainter *pPainter = new QPainter(printer);

    int page_border = 30 * line_ratio_;
    int nPdfWidth = pPainter->viewport().width();
    int nPdfHeight = pPainter->viewport().height();
    qDebug() << "pdf width: " << nPdfWidth << ", height: " << nPdfHeight;
    //绘制标题
    int y = 10 * line_ratio_;
    int nLineHeight = 60 * line_ratio_;

    y = PrintInfo(pPainter, nPdfWidth, page_border, y, nLineHeight);
    qDebug() << "after PrintInfo y: " << y;

    y += 10 * line_ratio_;
    pPainter->drawLine(QLineF(page_border, y + 15 * line_ratio_, nPdfWidth - page_border, y + 15 * line_ratio_));
    y += 30 * line_ratio_;

    y = PrintImage(pPainter, nPdfWidth, page_border, y, nPdfHeight * image_ratio_);
    qDebug() << "after PrintImage y: " << y;

    y = PrintTable(pPainter, nPdfWidth, page_border, y, nLineHeight);
    qDebug() << "after PrintTable y: " << y;

    delete pPainter;
}

int Printer::PrintInfo(QPainter *pPainter, int nPdfWidth, int page_border,
		     int y, int nLineHeight)
{
    pPainter->setFont(QFont("宋体", 18, QFont::Bold));
    pPainter->drawText(QRect(0, y, nPdfWidth, 100), Qt::AlignCenter, tr(organization_name_.toStdString().c_str()));
    y += 60 * line_ratio_;

    pPainter->setFont(QFont("宋体", 18, QFont::Normal));
    pPainter->drawText(QRect(0, y, nPdfWidth, 100), Qt::AlignCenter, tr("功能分析报告单"));
    y += 80 * line_ratio_;

    int line_width = nPdfWidth - page_border * 2;
    int column_width = line_width / 4;
    int column_x_1 = page_border;
    int column_x_2 = page_border + column_width;
    int column_x_3 = page_border + column_width * 2;
    int column_x_4 = page_border + column_width * 3;

    pPainter->setFont(QFont("宋体", 12, QFont::Normal));
    pPainter->drawText(QRect(column_x_1, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("姓名: %1").arg(info_->name));
    pPainter->drawText(QRect(column_x_2, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("性别: %1").arg(info_->sex));
    pPainter->drawText(QRect(column_x_3, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("年龄: %1").arg(info_->age));
    pPainter->drawText(QRect(column_x_4, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("样本号: %1").arg(data_->strip_id));
    y += 30 * line_ratio_;

    pPainter->setFont(QFont("宋体", 12, QFont::Normal));
    pPainter->drawText(QRect(column_x_1, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("住院号: %1").arg(info_->patient_id));
    pPainter->drawText(QRect(column_x_2, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("科室: %1").arg(info_->department));
    pPainter->drawText(QRect(column_x_3, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("床号: %1").arg(info_->bed_no));
   pPainter->drawText(QRect(column_x_4, y, column_width, nLineHeight), Qt::AlignLeft,
		    QString("试剂类型: %1").arg("gbACT"));

    y += 30 * line_ratio_;

    pPainter->setFont(QFont("宋体", 12, QFont::Normal));
    pPainter->drawText(QRect(column_x_1, y, column_width, nLineHeight), Qt::AlignLeft,
		     QString("送检医生: %1").arg(info_->submit_doctor));
    pPainter->drawText(QRect(column_x_2, y, column_width * 3, nLineHeight), Qt::AlignLeft,
		     QString("备注: %1").arg(info_->note));

    return y;
}

参考