乐于分享
好东西不私藏

使用Qt把文件内容合并成pdf

使用Qt把文件内容合并成pdf

main.cpp
#include<QApplication>#include<QTextDocument>#include<QTextCursor>#include<QPrinter>#include<QFileDialog>#include<QPainter>#include<QDir>#include<QDebug>#include<QStack>#include<QTextCursor>voidgenerateSimplePdf(){    // 创建文本文档    QTextDocument document;    // 设置HTML内容    QString html = R"(        <h1>Qt PDF生成示例</h1>        <p>这是一个使用<strong>QTextDocument</strong>生成的PDF文档。</p>        <li>项目1</li>        <li>项目2</li>        <li>项目3</li>        <p style='color: blue;'>蓝色文本</p>    )";    document.setHtml(html);    // 创建打印机(PDF输出)    QPrinter printer(QPrinter::HighResolution);    printer.setOutputFormat(QPrinter::PdfFormat);    printer.setOutputFileName("output.pdf");    printer.setPageSize(QPageSize(QPageSize::A4));    // 打印到PDF    document.print(&printer);}voidprint_pdf(QString &&name,QStringList &ss){    // 创建文本文档    QTextDocument document;    // document.setHtml(ss);    // document.setPlainText(ss);    QPrinter printer(QPrinter::HighResolution);    printer.setOutputFormat(QPrinter::PdfFormat);    printer.setOutputFileName(name);    printer.setPageSize(QPageSize(QPageSize::A4));    QTextCursor cursor(&document);    for (auto &&p:ss) {        cursor.insertText(p);    }    // 打印到PDF    document.print(&printer);}voidoutput_all_cpp_pdf(){    QDir d("/home/admin1/桌面/work");    if(!d.exists()){        qDebug() << "不存在" << d;        return;    }    auto &&files = d.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries);    QStack<QFileInfo> stack;    QStringList cpps;    for(auto &&f:files){        if(f.isDir()){            stack.push(f);            while (!stack.isEmpty()) {                auto &&s = stack.pop();                QDir ds(s.absoluteFilePath());                for(auto &&dd:ds.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries)){                    if(dd.isDir()){                        stack.push(dd);                    }                    if(dd.isFile()){                        QFile fl(dd.absoluteFilePath());                        if (!fl.open(QIODevice::ReadOnly | QIODevice::Text)) {                            qDebug() << "无法打开文件:" << fl.errorString();                            return;                        }                        cpps << fl.readAll();                    }                }            }        }        if(f.isFile()){            QFile fl(f.absoluteFilePath());            if (!fl.open(QIODevice::ReadOnly | QIODevice::Text)) {                qDebug() << "无法打开文件:" << fl.errorString();                return;            }            cpps << fl.readAll();        }    }    print_pdf("cpp.pdf",cpps);    // QApplication::quit();    exit(EXIT_SUCCESS);}intmain(int argc, char *argv[]){    QApplication a(argc, argv);    // Set up code that uses the Qt event loop here.    // Call a.quit() or a.exit() to quit the application.    // A not very useful example would be including    // #include <QTimer>    // near the top of the file and calling    // QTimer::singleShot(5000, &a, &QCoreApplication::quit);    // which quits the application after 5 seconds.    // If you do not need a running Qt event loop, remove the call    // to a.exec() or use the Non-Qt Plain C++ Application template.    // generateSimplePdf();    output_all_cpp_pdf();    return a.exec();}

pdf.pro

QT       += core gui printsupportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.# In order to do so, uncomment the following line.#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \        main.cpp# Default rules for deployment.qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += target
本站文章均为手工撰写未经允许谢绝转载:夜雨聆风 » 使用Qt把文件内容合并成pdf

评论 抢沙发

4 + 6 =
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
×
订阅图标按钮