Messagelib

webengineexportpdfpagejob.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "webengineexportpdfpagejob.h"
8#include "webengineviewer_debug.h"
9#include <KLocalizedString>
10#include <QPageSetupDialog>
11#include <QPrinter>
12#include <QWebEngineView>
13
14using namespace WebEngineViewer;
15WebEngineExportPdfPageJob::WebEngineExportPdfPageJob(QObject *parent)
16 : QObject(parent)
17{
18}
19
20WebEngineExportPdfPageJob::~WebEngineExportPdfPageJob() = default;
21
22void WebEngineExportPdfPageJob::start()
23{
24 if (!canStart()) {
25 qCWarning(WEBENGINEVIEWER_LOG) << "webengineview not defined or path is not defined.! It's a bug";
26 Q_EMIT exportPdfFailed();
28 return;
29 }
30 auto printer = new QPrinter();
31 printer->setOutputFormat(QPrinter::PdfFormat);
32 printer->setOutputFileName(mPdfPath);
33
34 auto dialog = new QPageSetupDialog(printer, mWebEngineView);
35 connect(mWebEngineView->page(), &QWebEnginePage::pdfPrintingFinished, this, &WebEngineExportPdfPageJob::slotExportPdfFinished);
36 if (dialog->exec() == QDialog::Accepted) {
37 if (dialog->printer()->outputFormat() == QPrinter::PdfFormat) {
38 mWebEngineView->page()->printToPdf(dialog->printer()->outputFileName(), dialog->printer()->pageLayout());
39 delete dialog;
40 delete printer;
41 }
42 }
43}
44
45void WebEngineExportPdfPageJob::slotExportPdfFinished(const QString &filePath, bool success)
46{
47 if (success) {
48 Q_EMIT exportToPdfSuccess();
49 } else {
50 qCWarning(WEBENGINEVIEWER_LOG) << "Failed to export to pdf to " << filePath;
51 Q_EMIT exportPdfFailed();
52 }
54}
55
56QWebEngineView *WebEngineExportPdfPageJob::engineView() const
57{
58 return mWebEngineView;
59}
60
61void WebEngineExportPdfPageJob::setEngineView(QWebEngineView *engineView)
62{
63 mWebEngineView = engineView;
64}
65
66QString WebEngineExportPdfPageJob::pdfPath() const
67{
68 return mPdfPath;
69}
70
71void WebEngineExportPdfPageJob::setPdfPath(const QString &pdfPath)
72{
73 mPdfPath = pdfPath;
74}
75
76bool WebEngineExportPdfPageJob::canStart() const
77{
78 return mWebEngineView && !mPdfPath.isEmpty();
79}
80
81#include "moc_webengineexportpdfpagejob.cpp"
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.