QT中使用HTML格式导出PDF文件

QT中使用HTML格式原因

在开发时需要将放入的图片导出的PDF,而一般的方式只能将文字保存,保存不了图片格式的内容。HTML格式广泛运用于数据的存储、使用以及格式转换中,这里使用HTML是为了将QTextEdit中的富文本导出大PDF。通过QTextEdit中的toHtml()函数获取QTextEdit中的内容,如:ui->textEdit->toHtml().toUtf8()。获取的内容格式是HTML格式,通过html可以转换成PDF格式。

如何使用HTML导出PDF文件

  1. 使用QPrinter类定义对象,然后通过setOutputFormat定义转成的是PDF格式的文件,setOutputFileName定义转成PDF文件的文件名。
  2. 使用QTextDocument类定义对象,通过setHtml、print函数将html转换成PDF
    QString pdfname = "E:/htmltopdf.pdf";
    QPrinter printer;
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(pdfname);
    QTextDocument document;
    document.setHtml(html);		//注:html是QTextEdit控件中获取的html格式内容
    document.print(&printer);
    document.end();

HTML中的语法

主要的几个语法点:

  1. html是以 <html>开头,</html> 结尾。每组信息都是成对出现的
  2. <title> 中的内容不显示在PDF中,只有当鼠标在PDF文档上点击标题时才会显示<title>后面的内容
  3. h1~h6第不同的标题格式,主要是字体大小不同。hr是绘制一条横线
  4. 在开始是需要设置PDF格式颜色,好像不设置会导致不显示内容的情况
  5. 创建表格用table属性,绘制table表格时 border表示线宽,cellspacing表示单元格之间的空隙,如果不设置默认为1,就会用一只1px宽的笔绘制table边框,有点难看,所以建议设置cellspacing为0
  6. html中可以使用占位符,但是要注意添加\"。

void Widget::on_pushButton_clicked()
{
    QString textEdit_html = ui->textEdit->toHtml().toUtf8();
    QString textEdit_m = ui->textEdit->toPlainText();

    //html添加标题
    QString html = "";
    html += "<html>";
    html += "<head>";
    html += "<title> 这是PDF不显示内容直到鼠标靠近 </title>";   //title属性内容不显示,当鼠标放到上面时显示title中的内容
    html += "<head>";                                                         //head 中的内容不显示
    html += "<body bgcolor=\"#ffffff\">";       //PDF背景颜色需要设置,否则不能显示后面的字体内容

    html += "<h1 style=\" text-align:center\"> 测试HTML转成PDF文档</h1>";   //h1~h6 字体是限定的
    html += "<h1 style=\" text-align:center\"> 第二行标题</h1>";
    html += "<p style=\" text-align:right\"> 序号     </h1>";

    html += "<table border=\"0.2\" cellpadding=\" 3 \" cellspacing=\" 0 \" margin=\"auto\" >";      // cellspacing 单元格之间的空间,cellpadding 属性规定单元边沿与其内容之间的空白
        html += "<tr >";
            //html += "<td width=\"20%\" hight= \"20px\">调试任务</th>";
            QString str1 = QString(" %1 ").arg("调试任务");
            //html += "<td style= \" width=20%; height:30px;line-height:24px;color:#E74C3C \">" + str1 + "</th>";   //line-height行高,color字体颜色
            html += "<td width=\"20%\" style= \"color:#E74C3C; text-align:center \" >" + str1 + "</th>";
            html += "<td width=\"80%\">  </th>";
        html += "</tr>";
        html += "<tr>";
            html += "<td width=\"20%\" hight= \"20px\">治疗室序号</td>";
            html += "<td width=\"40%\">  </td>";
            QString str3 = QString(" %1 ").arg("日期日期");
            html += "<td width=\"20%\">" + str3 + "</td>";
            html += "<td width=\"40%\">  </td>";
        html += "</tr>";
    html += "</table>";

    html += "<table border=\" 0.2 \" cellspacing=\" 0 \" >";
        html += "<tr>";
            html += "<th width=\"20%\" hight= \"20px\">治疗室序号</th>";
            html += "<th width=\"40%\">  </th>";
            QString str2 = QString(" %1 ").arg("日期");
            html += "<th width=\"20%\">" + str2 + "</th>";
            html += "<th width=\"40%\">  </th>";
        html += "</tr>";
    html += "</table>";

    html += "<table border=\" 0.2 \" cellspacing=\" 0 \" >";
        html += "<tr>";
            html += "<td width=\"100%\">" +textEdit_html + "</th>";
        html += "</tr>";
    html += "</table>";


    html += "</html>";

    qDebug()<< textEdit_html << endl;
    qDebug()<< html << endl;
    qDebug()<< textEdit_m << endl;




    QString pdfname = "E:/testhtmlpdf.pdf";
    QPrinter printer;
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(pdfname);
    QTextDocument document;
    //QString html = saveHtmlToPDFgld();
    //document.setHtml(html);

    document.setHtml(html);
    document.print(&printer);
    document.end();
}

在这里插入图片描述

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值