WebEngine Widgets Html2Pdf Beispiel
Konvertiert Webseiten in PDF-Dokumente mit Qt WebEngine.
Html2Pdf demonstriert die Verwendung von Qt WebEngine zur Implementierung einer Befehlszeilenanwendung für die Konvertierung von Webseiten in PDF-Dokumente.
Ausführen des Beispiels
Zum Ausführen des Beispiels von Qt Creatorauszuführen, öffnen Sie den Modus Welcome und wählen Sie das Beispiel aus Examples aus. Weitere Informationen finden Sie unter Erstellen und Ausführen eines Beispiels.
Der Konvertierungsprozess
Um eine Webseite in ein PDF-Dokument zu konvertieren, müssen wir:
- Erstellen Sie eine QWebEngineView.
- Weisen Sie QWebEngineView an, mit dem Laden der Ziel-URL zu beginnen und zu warten, bis es beendet ist.
- Weisen Sie QWebEngineView an, mit der Konvertierung der geladenen Seite in eine PDF-Datei zu beginnen, und warten Sie erneut, bis der Vorgang abgeschlossen ist.
- Sobald die Konvertierung abgeschlossen ist, beenden Sie das Programm.
Dieser Prozess ist in der Klasse Html2PdfConverter gekapselt:
#include <QApplication> #include <QCommandLineParser> #include <QFile> #include <QTextStream> #include <QWebEngineView> #include <functional> #include <utility> class Html2PdfConverter : public QObject { Q_OBJECT public: explicit Html2PdfConverter(QString inputPath, QString outputPath); int run(); private slots: void loadFinished(bool ok); void pdfPrintingFinished(const QString &filePath, bool success); private: QString m_inputPath; QString m_outputPath; QScopedPointer<QWebEngineView> m_view; };
Im Konstruktor erstellen wir die QWebEngineView und verbinden uns mit ihren Signalen QWebEngineView::loadFinished und QWebEngineView::pdfPrintingFinished:
Html2PdfConverter::Html2PdfConverter(QString inputPath, QString outputPath) : m_inputPath(std::move(inputPath)) , m_outputPath(std::move(outputPath)) , m_view(new QWebEngineView) { connect(m_view.data(), &QWebEngineView::loadFinished, this, &Html2PdfConverter::loadFinished); connect(m_view.data(), &QWebEngineView::pdfPrintingFinished, this, &Html2PdfConverter::pdfPrintingFinished); }
Die Methode run()
wird den Konvertierungsprozess auslösen, indem sie QWebEnginePage auffordert, mit dem Laden der Ziel-URL zu beginnen. Dann treten wir in die Hauptereignisschleife ein:
int Html2PdfConverter::run() { m_view->load(QUrl::fromUserInput(m_inputPath)); return QApplication::exec(); }
Nachdem das Laden beendet ist, beginnen wir mit der PDF-Erzeugung. Wir fordern die Methode QWebEnginePage::printToPdf auf, die Ausgabe direkt auf die Festplatte zu schreiben:
void Html2PdfConverter::loadFinished(bool ok) { if (!ok) { QTextStream(stderr) << tr("failed to load URL '%1'").arg(m_inputPath) << "\n"; QCoreApplication::exit(1); return; } m_view->printToPdf(m_outputPath); }
Sobald wir das Signal erhalten, dass die PDF-Konvertierung abgeschlossen ist, müssen wir nur noch mögliche Fehler melden und das Programm beenden:
void Html2PdfConverter::pdfPrintingFinished(const QString &filePath, bool success) { if (!success) { QTextStream(stderr) << tr("failed to print to output file '%1'").arg(filePath) << "\n"; QCoreApplication::exit(1); } else { QCoreApplication::quit(); } }
Die Hauptfunktion
Unsere Funktion main
ist für das Einrichten einer QApplication und das Parsen von Kommandozeilenargumenten zuständig:
int main(int argc, char *argv[]) { QApplication app(argc, argv); QCoreApplication::setOrganizationName("QtExamples"); QCoreApplication::setApplicationName("html2pdf"); QCoreApplication::setApplicationVersion(QT_VERSION_STR); QCommandLineParser parser; parser.setApplicationDescription( QCoreApplication::translate("main", "Converts the web page INPUT into the PDF file OUTPUT.")); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument( QCoreApplication::translate("main", "INPUT"), QCoreApplication::translate("main", "Input URL for PDF conversion.")); parser.addPositionalArgument( QCoreApplication::translate("main", "OUTPUT"), QCoreApplication::translate("main", "Output file name for PDF conversion.")); parser.process(QCoreApplication::arguments()); const QStringList requiredArguments = parser.positionalArguments(); if (requiredArguments.size() != 2) parser.showHelp(1); Html2PdfConverter converter(requiredArguments.at(0), requiredArguments.at(1)); return converter.run(); }
Beachten Sie, dass wir für die Verwendung von Qt WebEngine Widgets eine QApplication und nicht eine QCoreApplication erstellen müssen, auch wenn es sich um eine Kommandozeilenanwendung handelt.
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.