File indexing completed on 2024-06-02 05:30:37

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "webengineexportpdfpagejob.h"
0008 #include "webengineviewer_debug.h"
0009 #include <QWebEngineView>
0010 
0011 using namespace WebEngineViewer;
0012 WebEngineExportPdfPageJob::WebEngineExportPdfPageJob(QObject *parent)
0013     : QObject(parent)
0014 {
0015 }
0016 
0017 WebEngineExportPdfPageJob::~WebEngineExportPdfPageJob() = default;
0018 
0019 void WebEngineExportPdfPageJob::start()
0020 {
0021     if (!canStart()) {
0022         qCWarning(WEBENGINEVIEWER_LOG) << "webengineview not defined or path is not defined.! It's a bug";
0023         Q_EMIT exportPdfFailed();
0024         deleteLater();
0025         return;
0026     }
0027     connect(mWebEngineView->page(), &QWebEnginePage::pdfPrintingFinished, this, &WebEngineExportPdfPageJob::slotPdfPrintingFinished);
0028     mWebEngineView->page()->printToPdf(mPdfPath);
0029 }
0030 
0031 void WebEngineExportPdfPageJob::slotPdfPrintingFinished(const QString &filePath, bool success)
0032 {
0033     if (success) {
0034         Q_EMIT exportToPdfSuccess();
0035     } else {
0036         qCWarning(WEBENGINEVIEWER_LOG) << "Failed to export to pdf to " << filePath;
0037         Q_EMIT exportPdfFailed();
0038     }
0039     deleteLater();
0040 }
0041 
0042 QWebEngineView *WebEngineExportPdfPageJob::engineView() const
0043 {
0044     return mWebEngineView;
0045 }
0046 
0047 void WebEngineExportPdfPageJob::setEngineView(QWebEngineView *engineView)
0048 {
0049     mWebEngineView = engineView;
0050 }
0051 
0052 QString WebEngineExportPdfPageJob::pdfPath() const
0053 {
0054     return mPdfPath;
0055 }
0056 
0057 void WebEngineExportPdfPageJob::setPdfPath(const QString &pdfPath)
0058 {
0059     mPdfPath = pdfPath;
0060 }
0061 
0062 bool WebEngineExportPdfPageJob::canStart() const
0063 {
0064     return mWebEngineView && !mPdfPath.isEmpty();
0065 }
0066 
0067 #include "moc_webengineexportpdfpagejob.cpp"