Debian Codegraph @

qt6/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, 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-converter.h"
29 #include "poppler-qt6.h"
30
31 #include "poppler-annotation-helper.h"
32 #include "poppler-annotation-private.h"
33 #include "poppler-private.h"
34 #include "poppler-converter-private.h"
35 #include "poppler-qiodeviceoutstream-private.h"
36
37 #include <QFile>
38 #include <QUuid>
39
40 #include "Form.h"
41 #include <ErrorCodes.h>
42
43 namespace Poppler {
44
45 class PDFConverterPrivate : public BaseConverterPrivate
46 {
47 public:
48 PDFConverterPrivate();
49 ~PDFConverterPrivate() override;
50
51 PDFConverter::PDFOptions opts;
52 };
53
54 PDFConverterPrivate::PDFConverterPrivate() = default;
55
56 PDFConverterPrivate::~PDFConverterPrivate() = default;
57
58 PDFConverter::PDFConverter(DocumentData *document) : BaseConverter(*new PDFConverterPrivate())
59 {
60 Q_D(PDFConverter);
61 d->document = document;
62 }
63
64 PDFConverter::~PDFConverter() = default;
65
66 void PDFConverter::setPDFOptions(PDFConverter::PDFOptions options)
67 {
68 Q_D(PDFConverter);
69 d->opts = options;
70 }
71
72 PDFConverter::PDFOptions PDFConverter::pdfOptions() const
73 {
74 Q_D(const PDFConverter);
75 return d->opts;
76 }
77
78 bool PDFConverter::convert()
79 {
80 Q_D(PDFConverter);
81 d->lastError = NoError;
82
83 if (d->document->locked) {
84 d->lastError = FileLockedError;
85 return false;
86 }
87
88 QIODevice *dev = d->openDevice();
89 if (!dev) {
90 d->lastError = OpenOutputError;
91 return false;
92 }
93
94 bool deleteFile = false;
95 if (QFile *file = qobject_cast<QFile *>(dev)) {
96 deleteFile = !file->exists();
97 }
98
99 int errorCode = errNone;
100 QIODeviceOutStream stream(dev);
101 if (d->opts & WithChanges) {
102 errorCode = d->document->doc->saveAs(&stream);
103 } else {
104 errorCode = d->document->doc->saveWithoutChangesAs(&stream);
105 }
106 d->closeDevice();
107 if (errorCode != errNone) {
108 if (deleteFile) {
109 qobject_cast<QFile *>(dev)->remove();
110 }
111 if (errorCode == errOpenFile) {
112 d->lastError = OpenOutputError;
113 } else {
114 d->lastError = NotSupportedInputFileError;
115 }
116 }
117
118 return (errorCode == errNone);
119 }
120
121 bool PDFConverter::sign(const NewSignatureData &data)
122 {
123 Q_D(PDFConverter);
124 d->lastError = NoError;
125
126 if (d->document->locked) {
127 d->lastError = FileLockedError;
128 return false;
129 }
130
131 if (data.signatureText().isEmpty()) {
132 qWarning() << "No signature text given";
133 return false;
134 }
135
136 ::PDFDoc *doc = d->document->doc;
137 ::Page *destPage = doc->getPage(data.page() + 1);
138 std::unique_ptr<GooString> gSignatureText = std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureText()));
139 std::unique_ptr<GooString> gSignatureLeftText = std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureLeftText()));
140 const auto reason = std::unique_ptr<GooString>(data.reason().isEmpty() ? nullptr : QStringToUnicodeGooString(data.reason()));
141 const auto location = std::unique_ptr<GooString>(data.location().isEmpty() ? nullptr : QStringToUnicodeGooString(data.location()));
142 const auto ownerPwd = std::optional<GooString>(data.documentOwnerPassword().constData());
143 const auto userPwd = std::optional<GooString>(data.documentUserPassword().constData());
144 auto failure = doc->sign(d->outputFileName.toUtf8().constData(), data.certNickname().toUtf8().constData(), data.password().toUtf8().constData(), QStringToGooString(data.fieldPartialName()), data.page() + 1,
145 boundaryToPdfRectangle(destPage, data.boundingRectangle(), Annotation::FixedRotation), *gSignatureText, *gSignatureLeftText, data.fontSize(), data.leftFontSize(), convertQColor(data.fontColor()),
146 data.borderWidth(), convertQColor(data.borderColor()), convertQColor(data.backgroundColor()), reason.get(), location.get(), data.imagePath().toStdString(), ownerPwd, userPwd);
147 if (failure) {
148 d->lastSigningErrorDetails = fromPopplerCore(failure.value().message);
149 d->lastSigningResult = GenericSigningError; // catch all
150 switch (failure.value().type) {
151 case CryptoSign::SigningError::GenericError:
152 d->lastSigningResult = GenericSigningError;
153 break;
154 case CryptoSign::SigningError::InternalError:
155 d->lastSigningResult = InternalError;
156 break;
157 case CryptoSign::SigningError::KeyMissing:
158 d->lastSigningResult = KeyMissing;
159 break;
160 case CryptoSign::SigningError::UserCancelled:
161 d->lastSigningResult = UserCancelled;
162
163 break;
164 case CryptoSign::SigningError::WriteFailed:
165 d->lastSigningResult = WriteFailed;
166 break;
167 case CryptoSign::SigningError::BadPassphrase:
168
169 d->lastSigningResult = BadPassphrase;
170 break;
171 }
172 return false;
173 } else {
174 d->lastSigningErrorDetails = {};
175 d->lastSigningResult = SigningSuccess;
176 return true;
177 }
178 }
179
180 PDFConverter::SigningResult PDFConverter::lastSigningResult() const
181 {
182 Q_D(const PDFConverter);
183 return d->lastSigningResult;
184 }
185
186 Poppler::ErrorString PDFConverter::lastSigningErrorDetails() const
187 {
188 Q_D(const PDFConverter);
189 return d->lastSigningErrorDetails;
190 }
191
192 struct PDFConverter::NewSignatureData::NewSignatureDataPrivate
193 {
194 NewSignatureDataPrivate() = default;
195
196 QString certNickname;
197 QString password;
198 int page;
199 QRectF boundingRectangle;
200 QString signatureText;
201 QString signatureLeftText;
202 QString reason;
203 QString location;
204 double fontSize = 10.0;
205 double leftFontSize = 20.0;
206 QColor fontColor = Qt::red;
207 QColor borderColor = Qt::red;
208 double borderWidth = 1.5;
209 QColor backgroundColor = QColor(240, 240, 240);
210
211 QString partialName = QUuid::createUuid().toString();
212
213 QByteArray documentOwnerPassword;
214 QByteArray documentUserPassword;
215
216 QString imagePath;
217 };
218
219 PDFConverter::NewSignatureData::NewSignatureData() : d(new NewSignatureDataPrivate()) { }
220
221 PDFConverter::NewSignatureData::~NewSignatureData()
222 {
223 delete d;
224 }
225
226 QString PDFConverter::NewSignatureData::certNickname() const
227 {
228 return d->certNickname;
229 }
230
231 void PDFConverter::NewSignatureData::setCertNickname(const QString &certNickname)
232 {
233 d->certNickname = certNickname;
234 }
235
236 QString PDFConverter::NewSignatureData::password() const
237 {
238 return d->password;
239 }
240
241 void PDFConverter::NewSignatureData::setPassword(const QString &password)
242 {
243 d->password = password;
244 }
245
246 int PDFConverter::NewSignatureData::page() const
247 {
248 return d->page;
249 }
250
251 void PDFConverter::NewSignatureData::setPage(int page)
252 {
253 d->page = page;
254 }
255
256 QRectF PDFConverter::NewSignatureData::boundingRectangle() const
257 {
258 return d->boundingRectangle;
259 }
260
261 void PDFConverter::NewSignatureData::setBoundingRectangle(const QRectF &rect)
262 {
263 d->boundingRectangle = rect;
264 }
265
266 QString PDFConverter::NewSignatureData::signatureText() const
267 {
268 return d->signatureText;
269 }
270
271 void PDFConverter::NewSignatureData::setSignatureText(const QString &text)
272 {
273 d->signatureText = text;
274 }
275
276 QString PDFConverter::NewSignatureData::signatureLeftText() const
277 {
278 return d->signatureLeftText;
279 }
280
281 void PDFConverter::NewSignatureData::setSignatureLeftText(const QString &text)
282 {
283 d->signatureLeftText = text;
284 }
285
286 QString PDFConverter::NewSignatureData::reason() const
287 {
288 return d->reason;
289 }
290
291 void PDFConverter::NewSignatureData::setReason(const QString &reason)
292 {
293 d->reason = reason;
294 }
295
296 QString PDFConverter::NewSignatureData::location() const
297 {
298 return d->location;
299 }
300
301 void PDFConverter::NewSignatureData::setLocation(const QString &location)
302 {
303 d->location = location;
304 }
305
306 double PDFConverter::NewSignatureData::fontSize() const
307 {
308 return d->fontSize;
309 }
310
311 void PDFConverter::NewSignatureData::setFontSize(double fontSize)
312 {
313 d->fontSize = fontSize;
314 }
315
316 double PDFConverter::NewSignatureData::leftFontSize() const
317 {
318 return d->leftFontSize;
319 }
320
321 void PDFConverter::NewSignatureData::setLeftFontSize(double fontSize)
322 {
323 d->leftFontSize = fontSize;
324 }
325
326 QColor PDFConverter::NewSignatureData::fontColor() const
327 {
328 return d->fontColor;
329 }
330
331 void PDFConverter::NewSignatureData::setFontColor(const QColor &color)
332 {
333 d->fontColor = color;
334 }
335
336 QColor PDFConverter::NewSignatureData::borderColor() const
337 {
338 return d->borderColor;
339 }
340
341 void PDFConverter::NewSignatureData::setBorderColor(const QColor &color)
342 {
343 d->borderColor = color;
344 }
345
346 QColor PDFConverter::NewSignatureData::backgroundColor() const
347 {
348 return d->backgroundColor;
349 }
350
351 double PDFConverter::NewSignatureData::borderWidth() const
352 {
353 return d->borderWidth;
354 }
355
356 void PDFConverter::NewSignatureData::setBorderWidth(double width)
357 {
358 d->borderWidth = width;
359 }
360
361 void PDFConverter::NewSignatureData::setBackgroundColor(const QColor &color)
362 {
363 d->backgroundColor = color;
364 }
365
366 QString PDFConverter::NewSignatureData::fieldPartialName() const
367 {
368 return d->partialName;
369 }
370
371 void PDFConverter::NewSignatureData::setFieldPartialName(const QString &name)
372 {
373 d->partialName = name;
374 }
375
376 QByteArray PDFConverter::NewSignatureData::documentOwnerPassword() const
377 {
378 return d->documentOwnerPassword;
379 }
380
381 void PDFConverter::NewSignatureData::setDocumentOwnerPassword(const QByteArray &password)
382 {
383 d->documentOwnerPassword = password;
384 }
385
386 QByteArray PDFConverter::NewSignatureData::documentUserPassword() const
387 {
388 return d->documentUserPassword;
389 }
390
391 void PDFConverter::NewSignatureData::setDocumentUserPassword(const QByteArray &password)
392 {
393 d->documentUserPassword = password;
394 }
395
396 QString PDFConverter::NewSignatureData::imagePath() const
397 {
398 return d->imagePath;
399 }
400
401 void PDFConverter::NewSignatureData::setImagePath(const QString &path)
402 {
403 d->imagePath = path;
404 }
405 }