qt如何导出pdf格式的文件

来源:互联网 发布:thinkphp会员系统源码 编辑:程序博客网 时间:2024/06/05 07:27

本人在最近的项目开发中,客户要求系统运行日志导出pdf格式的文件,经过仔细研究QT Creator的帮助文件,发现qt本身是支持pdf打印格式的。通过设置QPrinter的输出文本格为                               

    QPrinter::PdfFormat即可。具体实现代码如下所示:

    QPrinter printer(QPrinter::HighResolution);

    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(filename);//filename表示导出文件的文件名(包含路径)
    QFile file(filename);
    if (!file.open(QFile::WriteOnly)){
        qDebug() << "error to open file";
        return;
    }
    QTextDocument *test = new QTextDocument(this);
    QFont Font;
    Font.setFamily("宋体");
    test->setDefaultFont(Font);
    test->setHtml("ceshiwenzi");
    test->print(&printer);
    file.close();