Debian Codegraph @

qt5/src/poppler-pdf-converter.cc

raw
1 /* poppler-pdf-converter.cc: qt interface to poppler
2 * Copyright (C) 2008, Pino Toscano <[email protected]>
3 * Copyright (C) 2008, 2009, 2020-2022, 2024, 2025, Albert Astals Cid <[email protected]>
4 * Copyright (C) 2020, Thorsten Behrens <[email protected]>
5 * Copyright (C) 2020, Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by Technische Universität Dresden
6 * Copyright (C) 2021, Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>.
7 * Copyright (C) 2021, Zachary Travis <[email protected]>
8 * Copyright (C) 2021, Georgiy Sgibnev <[email protected]>. Work sponsored by lab50.net.
9 * Copyright (C) 2022, Martin <[email protected]>
10 * Copyright (C) 2022, Felix Jung <[email protected]>
11 * Copyright (C) 2024, 2025, g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27
28 #include "poppler-qt5.h"
29
30 #include "poppler-annotation-helper.h"
31 #include "poppler-annotation-private.h"
32 #include "poppler-private.h"
33 #include "poppler-converter-private.h"
34 #include "poppler-qiodeviceoutstream-private.h"
35
36 #include <QFile>
37 #include <QUuid>
38
39 #include "Form.h"
40 #include <ErrorCodes.h>
41
42 namespace Poppler {
43
44 class PDFConverterPrivate : public BaseConverterPrivate
45 {
46 public:
47 PDFConverterPrivate();
48 ~PDFConverterPrivate() override;
49
50 PDFConverter::PDFOptions opts;
51 };
52
53 PDFConverterPrivate::PDFConverterPrivate() : opts(nullptr) { }
54
55 PDFConverterPrivate::~PDFConverterPrivate() = default;
56
57 PDFConverter::PDFConverter(DocumentData *document) : BaseConverter(*new PDFConverterPrivate())
58 {
59 Q_D(PDFConverter);
60 d->document = document;
61 }
62
63 PDFConverter::~PDFConverter() = default;
64
65 void PDFConverter::setPDFOptions(PDFConverter::PDFOptions options)
66 {
67 Q_D(PDFConverter);
68 d->opts = options;
69 }
70
71 PDFConverter::PDFOptions PDFConverter::pdfOptions() const
72 {
73 Q_D(const PDFConverter);
74 return d->opts;
75 }
76
77 bool PDFConverter::convert()
78 {
79 Q_D(PDFConverter);
80 d->lastError = NoError;
81
82 if (d->document->locked) {
83 d->lastError = FileLockedError;
84 return false;
85 }
86
87 QIODevice *dev = d->openDevice();
88 if (!dev) {
89 d->lastError = OpenOutputError;
90 return false;
91 }
92
93 bool deleteFile = false;
94 if (QFile *file = qobject_cast<QFile *>(dev)) {
95 deleteFile = !file->exists();
96 }
97
98 int errorCode = errNone;
99 QIODeviceOutStream stream(dev);
100 if (d->opts & WithChanges) {
101 errorCode = d->document->doc->saveAs(&stream);
102 } else {
103 errorCode = d->document->doc->saveWithoutChangesAs(&stream);
104 }
105 d->closeDevice();
106 if (errorCode != errNone) {
107 if (deleteFile) {
108 qobject_cast<QFile *>(dev)->remove();
109 }
110 if (errorCode == errOpenFile) {
111 d->lastError = OpenOutputError;
112 } else {
113 d->lastError = NotSupportedInputFileError;
114 }
115 }
116
117 return (errorCode == errNone);
118 }
119
120 bool PDFConverter::sign(const NewSignatureData &data)
121 {
122 Q_D(PDFConverter);
123 d->lastError = NoError;
124
125 if (d->document->locked) {
126 d->lastError = FileLockedError;
127 return false;
128 }
129
130 if (data.signatureText().isEmpty()) {
131 qWarning() << "No signature text given";
132 return false;
133 }
134
135 ::PDFDoc *doc = d->document->doc;
136 ::Page *destPage = doc->getPage(data.page() + 1);
137 std::unique_ptr<GooString> gSignatureText = std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureText()));
138 std::unique_ptr<GooString> gSignatureLeftText = std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureLeftText()));
139 const auto reason = std::unique_ptr<GooString>(data.reason().isEmpty() ? nullptr : QStringToUnicodeGooString(data.reason()));
140 const auto location = std::unique_ptr<GooString>(data.location().isEmpty() ? nullptr : QStringToUnicodeGooString(data.location()));
141 const auto ownerPwd = std::optional<GooString>(data.documentOwnerPassword().constData());
142 const auto userPwd = std::optional<GooString>(data.documentUserPassword().constData());
143 auto failure = doc->sign(d->outputFileName.toUtf8().constData(), data.certNickname().toUtf8().constData(), data.password().toUtf8().constData(), QStringToGooString(data.fieldPartialName()), data.page() + 1,
144 boundaryToPdfRectangle(destPage, data.boundingRectangle(), Annotation::FixedRotation), *gSignatureText, *gSignatureLeftText, data.fontSize(), data.leftFontSize(), convertQColor(data.fontColor()),
145 data.borderWidth(), convertQColor(data.borderColor()), convertQColor(data.backgroundColor()), reason.get(), location.get(), data.imagePath().toStdString(), ownerPwd, userPwd);
146 if (failure) {
147 d->lastSigningErrorDetails = fromPopplerCore(failure.value().message);
148 d->lastSigningResult = GenericSigningError; // catch all
149 switch (failure.value().type) {
150 case CryptoSign::SigningError::GenericError:
151 d->lastSigningResult = GenericSigningError;
152 break;
153 case CryptoSign::SigningError::InternalError:
154 d->lastSigningResult = InternalError;
155 break;
156 case CryptoSign::SigningError::KeyMissing:
157 d->lastSigningResult = KeyMissing;
158 break;
159 case CryptoSign::SigningError::UserCancelled:
160 d->lastSigningResult = UserCancelled;
161
162 break;
163 case CryptoSign::SigningError::WriteFailed:
164 d->lastSigningResult = WriteFailed;
165 break;
166 case CryptoSign::SigningError::BadPassphrase:
167
168 d->lastSigningResult = BadPassphrase;
169 break;
170 }
171 return false;
172 } else {
173 d->lastSigningErrorDetails = {};
174 d->lastSigningResult = SigningSuccess;
175 return true;
176 }
177 }
178
179 ErrorString PDFConverter::lastSigningErrorDetails() const
180 {
181 Q_D(const PDFConverter);
182 return d->lastSigningErrorDetails;
183 }
184
185 PDFConverter::SigningResult PDFConverter::lastSigningResult() const
186 {
187 Q_D(const PDFConverter);
188 return d->lastSigningResult;
189 }
190
191 struct PDFConverter::NewSignatureData::NewSignatureDataPrivate
192 {
193 NewSignatureDataPrivate() = default;
194
195 QString certNickname;
196 QString password;
197 int page;
198 QRectF boundingRectangle;
199 QString signatureText;
200 QString signatureLeftText;
201 QString reason;
202 QString location;
203 double fontSize = 10.0;
204 double leftFontSize = 20.0;
205 QColor fontColor = Qt::red;
206 QColor borderColor = Qt::red;
207 double borderWidth = 1.5;
208 QColor backgroundColor = QColor(240, 240, 240);
209
210 QString partialName = QUuid::createUuid().toString();
211
212 QByteArray documentOwnerPassword;
213 QByteArray documentUserPassword;
214
215 QString imagePath;
216 };
217
218 PDFConverter::NewSignatureData::NewSignatureData() : d(new NewSignatureDataPrivate()) { }
219
220 PDFConverter::NewSignatureData::~NewSignatureData()
221 {
222 delete d;
223 }
224
225 QString PDFConverter::NewSignatureData::certNickname() const
226 {
227 return d->certNickname;
228 }
229
230 void PDFConverter::NewSignatureData::setCertNickname(const QString &certNickname)
231 {
232 d->certNickname = certNickname;
233 }
234
235 QString PDFConverter::NewSignatureData::password() const
236 {
237 return d->password;
238 }
239
240 void PDFConverter::NewSignatureData::setPassword(const QString &password)
241 {
242 d->password = password;
243 }
244
245 int PDFConverter::NewSignatureData::page() const
246 {
247 return d->page;
248 }
249
250 void PDFConverter::NewSignatureData::setPage(int page)
251 {
252 d->page = page;
253 }
254
255 QRectF PDFConverter::NewSignatureData::boundingRectangle() const
256 {
257 return d->boundingRectangle;
258 }
259
260 void PDFConverter::NewSignatureData::setBoundingRectangle(const QRectF &rect)
261 {
262 d->boundingRectangle = rect;
263 }
264
265 QString PDFConverter::NewSignatureData::signatureText() const
266 {
267 return d->signatureText;
268 }
269
270 void PDFConverter::NewSignatureData::setSignatureText(const QString &text)
271 {
272 d->signatureText = text;
273 }
274
275 QString PDFConverter::NewSignatureData::signatureLeftText() const
276 {
277 return d->signatureLeftText;
278 }
279
280 void PDFConverter::NewSignatureData::setSignatureLeftText(const QString &text)
281 {
282 d->signatureLeftText = text;
283 }
284
285 QString PDFConverter::NewSignatureData::reason() const
286 {
287 return d->reason;
288 }
289
290 void PDFConverter::NewSignatureData::setReason(const QString &reason)
291 {
292 d->reason = reason;
293 }
294
295 QString PDFConverter::NewSignatureData::location() const
296 {
297 return d->location;
298 }
299
300 void PDFConverter::NewSignatureData::setLocation(const QString &location)
301 {
302 d->location = location;
303 }
304
305 double PDFConverter::NewSignatureData::fontSize() const
306 {
307 return d->fontSize;
308 }
309
310 void PDFConverter::NewSignatureData::setFontSize(double fontSize)
311 {
312 d->fontSize = fontSize;
313 }
314
315 double PDFConverter::NewSignatureData::leftFontSize() const
316 {
317 return d->leftFontSize;
318 }
319
320 void PDFConverter::NewSignatureData::setLeftFontSize(double fontSize)
321 {
322 d->leftFontSize = fontSize;
323 }
324
325 QColor PDFConverter::NewSignatureData::fontColor() const
326 {
327 return d->fontColor;
328 }
329
330 void PDFConverter::NewSignatureData::setFontColor(const QColor &color)
331 {
332 d->fontColor = color;
333 }
334
335 QColor PDFConverter::NewSignatureData::borderColor() const
336 {
337 return d->borderColor;
338 }
339
340 void PDFConverter::NewSignatureData::setBorderColor(const QColor &color)
341 {
342 d->borderColor = color;
343 }
344
345 QColor PDFConverter::NewSignatureData::backgroundColor() const
346 {
347 return d->backgroundColor;
348 }
349
350 double PDFConverter::NewSignatureData::borderWidth() const
351 {
352 return d->borderWidth;
353 }
354
355 void PDFConverter::NewSignatureData::setBorderWidth(double width)
356 {
357 d->borderWidth = width;
358 }
359
360 void PDFConverter::NewSignatureData::setBackgroundColor(const QColor &color)
361 {
362 d->backgroundColor = color;
363 }
364
365 QString PDFConverter::NewSignatureData::fieldPartialName() const
366 {
367 return d->partialName;
368 }
369
370 void PDFConverter::NewSignatureData::setFieldPartialName(const QString &name)
371 {
372 d->partialName = name;
373 }
374
375 QByteArray PDFConverter::NewSignatureData::documentOwnerPassword() const
376 {
377 return d->documentOwnerPassword;
378 }
379
380 void PDFConverter::NewSignatureData::setDocumentOwnerPassword(const QByteArray &password)
381 {
382 d->documentOwnerPassword = password;
383 }
384
385 QByteArray PDFConverter::NewSignatureData::documentUserPassword() const
386 {
387 return d->documentUserPassword;
388 }
389
390 void PDFConverter::NewSignatureData::setDocumentUserPassword(const QByteArray &password)
391 {
392 d->documentUserPassword = password;
393 }
394
395 QString PDFConverter::NewSignatureData::imagePath() const
396 {
397 return d->imagePath;
398 }
399
400 void PDFConverter::NewSignatureData::setImagePath(const QString &path)
401 {
402 d->imagePath = path;
403 }
404 }