Repository navigation
Expand file tree
/
Copy pathUnlockPDFFormsController.java
More file actions
135 lines (120 loc) · 6.36 KB
/
Copy pathUnlockPDFFormsController.java
File metadata and controls
135 lines (120 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package stirling.software.SPDF.controller.api.misc;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.pdfbox.cos.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import io.github.pixee.security.Filenames;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.config.swagger.StandardPdfResponse;
import stirling.software.common.annotations.AutoJobPostMapping;
import stirling.software.common.annotations.api.MiscApi;
import stirling.software.common.enumeration.ResourceWeight;
import stirling.software.common.model.api.PDFFile;
import stirling.software.common.model.tool.ToolFormat;
import stirling.software.common.model.tool.ToolIO;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.GeneralUtils;
import stirling.software.common.util.RegexPatternUtils;
import stirling.software.common.util.TempFileManager;
import stirling.software.common.util.WebResponseUtils;
@MiscApi
@Slf4j
public class UnlockPDFFormsController {
private final CustomPDFDocumentFactory pdfDocumentFactory;
private final TempFileManager tempFileManager;
public UnlockPDFFormsController(
CustomPDFDocumentFactory pdfDocumentFactory, TempFileManager tempFileManager) {
this.pdfDocumentFactory = pdfDocumentFactory;
this.tempFileManager = tempFileManager;
}
@AutoJobPostMapping(
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
value = "/unlock-pdf-forms",
resourceWeight = ResourceWeight.SMALL_WEIGHT)
@StandardPdfResponse
@ToolIO(produces = ToolFormat.PDF)
@Operation(
summary = "Remove read-only property from form fields",
description = "Removing read-only property from form fields making them fillable")
public ResponseEntity<Resource> unlockPDFForms(@ModelAttribute PDFFile file) {
try (PDDocument document = pdfDocumentFactory.load(file)) {
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
if (acroForm != null) {
acroForm.setNeedAppearances(true);
for (PDField field : acroForm.getFieldTree()) {
COSDictionary dict = field.getCOSObject();
if (dict.containsKey(COSName.getPDFName("Lock"))) {
dict.removeItem(COSName.getPDFName("Lock"));
}
int currentFlags = field.getFieldFlags();
if ((currentFlags & 1) == 1) {
int newFlags = currentFlags & ~1;
field.setFieldFlags(newFlags);
}
}
COSBase xfaBase = acroForm.getCOSObject().getDictionaryObject(COSName.XFA);
if (xfaBase != null) {
try {
var accessReadOnlyPattern =
RegexPatternUtils.getInstance().getAccessReadOnlyPattern();
if (xfaBase instanceof COSStream xfaStream) {
InputStream is = xfaStream.createInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
is.transferTo(baos);
String xml = baos.toString(StandardCharsets.UTF_8);
xml = accessReadOnlyPattern.matcher(xml).replaceAll("access=\"open\"");
PDStream newStream =
new PDStream(
document,
new ByteArrayInputStream(
xml.getBytes(StandardCharsets.UTF_8)));
acroForm.getCOSObject().setItem(COSName.XFA, newStream.getCOSObject());
} else if (xfaBase instanceof COSArray xfaArray) {
for (int i = 0; i < xfaArray.size(); i += 2) {
COSBase namePart = xfaArray.getObject(i);
COSBase streamPart = xfaArray.getObject(i + 1);
if (namePart instanceof COSString
&& streamPart instanceof COSStream stream) {
InputStream is = stream.createInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
is.transferTo(baos);
String xml = baos.toString(StandardCharsets.UTF_8);
xml =
accessReadOnlyPattern
.matcher(xml)
.replaceAll("access=\"open\"");
PDStream newStream =
new PDStream(
document,
new ByteArrayInputStream(
xml.getBytes(StandardCharsets.UTF_8)));
xfaArray.set(i + 1, newStream.getCOSObject());
}
}
}
} catch (Exception e) {
log.error("exception", e);
}
}
}
String mergedFileName =
GeneralUtils.generateFilename(
file.getFileInput().getOriginalFilename(), "_unlocked_forms.pdf");
return WebResponseUtils.pdfDocToWebResponse(
document, Filenames.toSimpleFileName(mergedFileName), tempFileManager);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
}