File indexing completed on 2024-12-22 04:48:19

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2023 Louis Schul <schul9louis@gmail.com>
0003 
0004 #include "printingHelper.h"
0005 
0006 #include <QFile>
0007 #include <QPrinter>
0008 #include <QTextDocument>
0009 #include <klocalizedstring.h>
0010 
0011 PrintingUtility::PrintingUtility(QObject *parent)
0012     : QObject(parent)
0013 {
0014 }
0015 
0016 void PrintingUtility::writePdf(const QString &path) const
0017 {
0018     if (QFile::exists(path)) {
0019         return;
0020     }
0021 
0022     QTextDocument document;
0023 
0024     QPrinter printer(QPrinter::PrinterResolution);
0025     printer.setOutputFormat(QPrinter::PdfFormat);
0026     printer.setOutputFileName(path);
0027     printer.setPageMargins(QMarginsF(15, 15, 15, 15));
0028 
0029     document.print(&printer);
0030 }
0031 
0032 void PrintingUtility::copy(const QString &fromPath, const QString &toPath) const
0033 {
0034     static const QString errorMessage = i18n("An error occurred while trying to copy this pdf.");
0035     if (QFile::exists(toPath)) {
0036         const bool previousRemoved = QFile::remove(toPath);
0037         if (!previousRemoved) {
0038             Q_EMIT pdfCopyDone(false, errorMessage);
0039             return;
0040         }
0041     }
0042 
0043     const bool isCopied = QFile::copy(fromPath, toPath);
0044     if (!isCopied) {
0045         Q_EMIT pdfCopyDone(false, errorMessage);
0046         return;
0047     }
0048 
0049     Q_EMIT pdfCopyDone(true, {});
0050 }