2021-04-30 20:12:10 +02:00
|
|
|
// Copyright (C) 2020-2021 Jakub Melka
|
2020-03-22 15:30:34 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// This file is part of PDF4QT.
|
2020-03-22 15:30:34 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// PDF4QT is free software: you can redistribute it and/or modify
|
2020-03-22 15:30:34 +01:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
2021-04-30 20:12:10 +02:00
|
|
|
// with the written consent of the copyright owner, any later version.
|
2020-03-22 15:30:34 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// PDF4QT is distributed in the hope that it will be useful,
|
2020-03-22 15:30:34 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2021-08-10 19:22:56 +02:00
|
|
|
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
|
2020-03-22 15:30:34 +01:00
|
|
|
|
|
|
|
#ifndef PDFDOCUMENTWRITER_H
|
|
|
|
#define PDFDOCUMENTWRITER_H
|
|
|
|
|
|
|
|
#include "pdfdocument.h"
|
|
|
|
#include "pdfprogress.h"
|
|
|
|
#include "pdfutils.h"
|
|
|
|
|
|
|
|
#include <QIODevice>
|
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Class used for writing PDF documents to the desired target device (or file,
|
|
|
|
/// buffer, etc.). If writing is not successful, then error message is returned.
|
2021-08-10 19:22:56 +02:00
|
|
|
class PDF4QTLIBSHARED_EXPORT PDFDocumentWriter
|
2020-03-22 15:30:34 +01:00
|
|
|
{
|
|
|
|
Q_DECLARE_TR_FUNCTIONS(pdf::PDFDocumentWriter)
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit inline PDFDocumentWriter(PDFProgress* progress) :
|
|
|
|
m_progress(progress)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-07 17:46:49 +02:00
|
|
|
/// Writes document to the file. If \p safeWrite is true, then document is first
|
|
|
|
/// written to the temporary file, and then renamed to original file name atomically,
|
|
|
|
/// so no data can be lost on, for example, power failure. If it is not possible to
|
|
|
|
/// create temporary file, then writing operation will attempt to write to the file
|
|
|
|
/// directly.
|
|
|
|
/// \param fileName File name
|
|
|
|
/// \param document Document
|
|
|
|
/// \param safeWrite Write document to the temporary file and then rename
|
|
|
|
PDFOperationResult write(const QString& fileName, const PDFDocument* document, bool safeWrite);
|
|
|
|
|
|
|
|
/// Write document to the output device. Device must be writable (i.e. opened
|
|
|
|
/// for writing).
|
|
|
|
/// \param device Output device
|
|
|
|
/// \param document Document
|
2020-03-22 15:30:34 +01:00
|
|
|
PDFOperationResult write(QIODevice* device, const PDFDocument* document);
|
|
|
|
|
2020-05-31 18:31:59 +02:00
|
|
|
/// Calculates document file size, as if it is written to the disk.
|
|
|
|
/// No file is accessed by this function; document is written
|
|
|
|
/// to fake stream, which counts operations. If error occurs, and
|
|
|
|
/// size can't be determined, then -1 is returned.
|
|
|
|
/// \param document Document
|
|
|
|
static qint64 getDocumentFileSize(const PDFDocument* document);
|
|
|
|
|
2021-06-18 19:06:11 +02:00
|
|
|
/// Calculates size estimate of an object. If object is null, then zero is returned.
|
|
|
|
/// \param document Document
|
|
|
|
/// \param reference Reference
|
|
|
|
static qint64 getObjectSize(const PDFDocument* document, PDFObjectReference reference);
|
|
|
|
|
2021-06-13 18:54:47 +02:00
|
|
|
/// Writes an object to byte array, without object header/footer
|
|
|
|
/// \param object Object to be written
|
|
|
|
static QByteArray getSerializedObject(const PDFObject& object);
|
|
|
|
|
2020-03-22 15:30:34 +01:00
|
|
|
private:
|
2021-06-18 19:06:11 +02:00
|
|
|
static void writeCRLF(QIODevice* device);
|
|
|
|
static void writeObjectHeader(QIODevice* device, PDFObjectReference reference);
|
|
|
|
static void writeObjectFooter(QIODevice* device);
|
2020-03-22 15:30:34 +01:00
|
|
|
|
|
|
|
/// Progress indicator
|
|
|
|
PDFProgress* m_progress;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace pdf
|
|
|
|
|
|
|
|
#endif // PDFDOCUMENTWRITER_H
|