首页 > 代码库 > 使用QPrinter生成pdf或用打印机打印数据

使用QPrinter生成pdf或用打印机打印数据

1.使用html生成表格

QString wesiOperateWidget::MakeDataToHtml(){    //表头    //html = "<table width=100% border=1 cellspacing=0 text-align=center style=border-collapse:collapse>";    //html += "<tr>";    //html += "<th>编号</th>";    //html += "<th>姓名</th>";    //html += "<th>头部成绩</th>";    //html += "<th>胸部成绩</th>";    //html += "</tr>";    ////数据    //html += "<tr>";    //html += "<td align=center>1</td>";    //html += "<td align=center>赵六</td>";    //html += "<td align=center>100</td>";    //html += "<td align=center>400</td>";    //html += "</tr>";    //html += "</table>";    QString html;    html = "<table width=100% border=1 cellspacing=0 text-align=center style=border-collapse:collapse>";    html += "<tr>";    html += "<th>编号</th>";    html += "<th>姓名</th>";    html += "<th>头部成绩</th>";    html += "<th>胸部成绩</th>";    html += "</tr>";    QStringList dataStringList;    QStringList rowInfoList;    html += "<tr>";    foreach(QString str, dataStringList)    {        rowInfoList = str.split(",");        for(int i = 0; i < 8; i += 2)        {            if(rowInfoList[i] == "number")            {                html = html + "<td align=center>" + rowInfoList[i + i] + "</td>";            }            else if(rowInfoList[i] == "name")            {                html = html + "<td align=center>" + rowInfoList[i + i] + "</td>";            }            else if(rowInfoList[i] == "headscore")            {                html = html + "<td align=center>" + rowInfoList[i + i] + "</td>";            }            else if(rowInfoList[i] == "bodyscore")            {                html = html + "<td align=center>" + rowInfoList[i + i] + "</td>";            }        }    }    html += "</tr>";    html += "</table>";    return html;}
view code

2.生成pdf文档

    QPrinter printer;    printer.setPageSize(QPrinter::A4);    printer.setOutputFormat(QPrinter::PdfFormat);    QString filepath = QFileDialog::getSaveFileName(this, tr("保存为..."), tr("D://成绩.pdf"), tr("PDF格式(*.pdf)"));    printer.setOutputFileName(filepath);    QTextDocument textDocument;    textDocument.setHtml(MakeDataToHtml());    textDocument.print(&printer);    QMessageBox::about(this, tr("提示"), tr("保存成功"));
View Code

3.使用打印机打印

1     QPrinter printer;2     printer.setPageSize(QPrinter::A4);3     QPrintDialog printDialog(&printer, this);4     if (printDialog.exec()){5         QTextDocument textDocument;6         textDocument.setHtml(MakeDataToHtml());7         textDocument.print(&printer);8     }
View Code

 

使用QPrinter生成pdf或用打印机打印数据