PdfPostProcessorImpl.java
20.4 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.aemfd.pdfdocmanager.SharedPdfDocumentContext
* com.adobe.internal.io.ByteReader
* com.adobe.internal.io.RandomAccessFileByteReader
* com.adobe.internal.pdftoolkit.core.securityframework.SecurityLock
* com.adobe.internal.pdftoolkit.core.types.ASString
* com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog
* com.adobe.internal.pdftoolkit.pdf.document.PDFDocument
* com.adobe.internal.pdftoolkit.pdf.document.PDFDocumentInfo
* com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFile
* com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFileInfo
* com.adobe.internal.pdftoolkit.pdf.document.PDFFileSpecification
* com.adobe.internal.pdftoolkit.pdf.document.PDFNameDictionary
* com.adobe.internal.pdftoolkit.pdf.document.PDFNamedEmbeddedFiles
* com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions
* com.adobe.internal.pdftoolkit.pdf.document.PDFSaveFullOptions
* com.adobe.internal.pdftoolkit.pdf.document.PDFSaveIncrementalOptions
* com.adobe.internal.pdftoolkit.pdf.document.PDFSaveLinearOptions
* com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions
* com.adobe.internal.pdftoolkit.pdf.document.PDFVersion
* com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpace
* com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceCMYK
* com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceGray
* com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceRGB
* com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImage
* com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocation
* com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocationMap
* com.adobe.internal.pdftoolkit.pdf.page.PDFPage
* com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree
* com.adobe.internal.pdftoolkit.services.imageconversion.ImageManager
* com.adobe.internal.pdftoolkit.services.security.SecurityLockPassword
* com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata
* com.adobe.internal.pdftoolkit.services.xmp.XMPService
* com.adobe.native2pdf.xml.InitialView
* com.adobe.native2pdf.xml.SecuritySettings
* com.adobe.native2pdf.xml.SecuritySettings$Settings
* com.adobe.pdfg.common.Utils
* com.adobe.pdfg.exception.ErrorCode
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.pdfg.postprocess;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.aemfd.pdfdocmanager.SharedPdfDocumentContext;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.RandomAccessFileByteReader;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityLock;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocumentInfo;
import com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFile;
import com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFileInfo;
import com.adobe.internal.pdftoolkit.pdf.document.PDFFileSpecification;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNameDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNamedEmbeddedFiles;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveFullOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveIncrementalOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveLinearOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFVersion;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpace;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceCMYK;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceGray;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceRGB;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImage;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocation;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocationMap;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree;
import com.adobe.internal.pdftoolkit.services.imageconversion.ImageManager;
import com.adobe.internal.pdftoolkit.services.security.SecurityLockPassword;
import com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata;
import com.adobe.internal.pdftoolkit.services.xmp.XMPService;
import com.adobe.native2pdf.xml.InitialView;
import com.adobe.native2pdf.xml.SecuritySettings;
import com.adobe.pdfg.common.Utils;
import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.postprocess.PdfPostProcessorUtilities;
import com.adobe.pdfg.postprocess.PostProcessFileInfo;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
public final class PdfPostProcessorImpl {
private static final Logger logger = LoggerFactory.getLogger(PdfPostProcessorImpl.class);
private Document fileToDispose = null;
public void setFileToDispose(Document fileToDispose) {
this.fileToDispose = fileToDispose;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public int doPostProcess(PostProcessFileInfo filePaths, String attachmentFileName, InitialView initialView, SecuritySettings.Settings encryptionSettings, boolean doWebOptimization, boolean doPdfAProcessing, boolean doImagePostProcessing, String targetPdfVersion, boolean forcePdfVersion, String pdfProducerString, int pdfColorSpace, boolean applyWatermark) {
int errorCode;
block49 : {
errorCode = 0;
String jobIdentityId = (String)Utils.threadLocalValue.get();
StringBuilder debugMsgs = new StringBuilder();
applyWatermark = false;
try {
debugMsgs.append("\nInside PdfPostProcessorImpl.doPostProcess for job=" + jobIdentityId);
if ("".equals(filePaths.pdfFilePath)) {
errorCode = 16001;
break block49;
}
if ("".equals(filePaths.xmpFilePath) && "".equals(filePaths.attachmentFilePath) && initialView == null && encryptionSettings == null && !doWebOptimization && !doPdfAProcessing && !doImagePostProcessing && "".equals(targetPdfVersion) && (pdfProducerString == null || "".equals(pdfProducerString)) && !applyWatermark) {
errorCode = 0;
break block49;
}
File duplicatePdf = null;
Object pdfReader = null;
RandomAccessFileByteReader attachmentReader = null;
InputStream xmpInput = null;
PDFDocument pdfDocument = null;
errorCode = 16003;
Object pdfDocumentContext = null;
try {
boolean bProducerUpdated;
PDFSaveIncrementalOptions docOptions;
duplicatePdf = filePaths.tempPdfFilePath != null && !filePaths.tempPdfFilePath.equals("") ? new File(filePaths.tempPdfFilePath) : new File(filePaths.pdfFilePath);
Document pdfInput = new Document(duplicatePdf, false);
debugMsgs.append("\nbefore creating pdfDocument for job=" + jobIdentityId);
PDFOpenOptions pdfOpts = PDFOpenOptions.newInstance();
pdfOpts.setSaveInPlace(false);
pdfDocument = SharedPdfDocumentContext.toPDFDocument((Document)pdfInput, (PDFOpenOptions)pdfOpts);
debugMsgs.append("\nbefore creating originalPdfVersion for job=" + jobIdentityId);
String originalPdfVersion = pdfDocument.getOriginalVersion().asString();
if ("".equals(targetPdfVersion) || !forcePdfVersion && targetPdfVersion.compareTo(originalPdfVersion) < 0) {
targetPdfVersion = originalPdfVersion;
}
debugMsgs.append("\nbefore getting pdfVersion for job=" + jobIdentityId);
PDFVersion pdfVersion = PDFVersion.getSupportedInstance((String)targetPdfVersion);
if (pdfVersion == null) {
pdfVersion = PDFVersion.vLatest;
}
pdfDocument.setToSaveVersion(pdfVersion);
debugMsgs.append("\nafter pdfDocument.setToSaveVersion for job=" + jobIdentityId);
if (!"".equals(filePaths.attachmentFilePath)) {
if (attachmentFileName.length() == 0 && !(attachmentFileName = new File(filePaths.attachmentFilePath).getName()).endsWith(".joboptions")) {
attachmentFileName = attachmentFileName + ".joboptions";
}
debugMsgs.append("\nbefore creating attachmentReader for job=" + jobIdentityId);
attachmentReader = new RandomAccessFileByteReader(new RandomAccessFile(filePaths.attachmentFilePath, "r"));
ASString name = new ASString(attachmentFileName.getBytes("UTF-8"));
debugMsgs.append("\nafter name = new ASString for job=" + jobIdentityId);
PDFEmbeddedFile pdfEmbeddedFile = PDFEmbeddedFile.newInstance((PDFDocument)pdfDocument, (PDFEmbeddedFileInfo)null, (ByteReader)attachmentReader);
debugMsgs.append("\nafter creating pdfEmbeddedFile for job=" + jobIdentityId);
PDFFileSpecification fileSpecification = PDFFileSpecification.newInstance((PDFDocument)pdfDocument, (ASString)name, (PDFEmbeddedFile)pdfEmbeddedFile);
debugMsgs.append("\nafter creating fileSpecification for job=" + jobIdentityId);
PDFNamedEmbeddedFiles embeddedFiles = pdfDocument.requireCatalog().procureNameDictionary().procureNamedEmbeddedFiles();
embeddedFiles.addEntry((Object)name, (Object)fileSpecification);
}
boolean bl = bProducerUpdated = pdfProducerString != null && !"".equals(pdfProducerString);
if (bProducerUpdated) {
debugMsgs.append("\nproducer needs to be updated for job=" + jobIdentityId);
if (pdfDocument.getDocumentInfo() == null) {
PDFDocumentInfo info = PDFDocumentInfo.newInstance((PDFDocument)pdfDocument);
pdfDocument.setDocumentInfo(info);
}
pdfDocument.getDocumentInfo().setProducer(pdfProducerString);
}
if (pdfColorSpace == 1 && (doImagePostProcessing || doPdfAProcessing)) {
try {
PDFPageTree pageTree = pdfDocument.requirePages();
PDFPage pdfPage = pageTree.getPage(0);
PDFXObjectImageWithLocationMap pdfXObjectImageMap = ImageManager.getPDFXObjectMap((PDFPage)pdfPage);
PDFColorSpace colorSpace = null;
for (Object obj : pdfXObjectImageMap.entrySet()) {
Map.Entry entry = (Map.Entry)obj;
PDFXObjectImageWithLocation imageWithLocation = (PDFXObjectImageWithLocation)entry.getValue();
PDFXObjectImage image = imageWithLocation.getXImage();
colorSpace = image.getColorSpace();
}
pdfColorSpace = colorSpace instanceof PDFColorSpaceDeviceGray ? 2 : (colorSpace instanceof PDFColorSpaceDeviceCMYK ? 4 : (colorSpace instanceof PDFColorSpaceDeviceRGB ? 3 : 1));
}
catch (Exception e) {
logger.info("Exception while obtaining the color space information from the pdf:" + e.getMessage());
logger.trace(e.getMessage(), (Throwable)e);
}
}
if (doImagePostProcessing || doPdfAProcessing) {
debugMsgs.append("\ndoImagePostProcessing is true for job=" + jobIdentityId);
PdfPostProcessorUtilities.addPdfAOutputIntent(pdfDocument, pdfColorSpace);
}
if (!"".equals(filePaths.xmpFilePath) || doImagePostProcessing || doPdfAProcessing || bProducerUpdated) {
DocumentMetadata md;
debugMsgs.append("\nDo the Post-Processing Related to XMP Import and PDF/A for job=" + jobIdentityId);
errorCode = ErrorCode.CorruptXMPFile;
boolean bNeedImport = false;
boolean bNeedSynchronization = false;
if (!"".equals(filePaths.xmpFilePath)) {
debugMsgs.append("\nRead XMP from the External File for job=" + jobIdentityId);
xmpInput = new BufferedInputStream(new FileInputStream(filePaths.xmpFilePath));
bNeedImport = true;
if (doPdfAProcessing) {
bNeedSynchronization = true;
}
} else {
debugMsgs.append("\nExtract the XMP Metadata from the PDF Document for job=" + jobIdentityId);
md = XMPService.getDocumentMetadata((PDFDocument)pdfDocument);
md.commit();
ByteArrayOutputStream xmpOutput = new ByteArrayOutputStream();
pdfDocument.exportXMP((OutputStream)xmpOutput);
xmpInput = new ByteArrayInputStream(xmpOutput.toByteArray());
}
debugMsgs.append("\nafter creating variable xmpInput for job=" + jobIdentityId);
if (bProducerUpdated || doPdfAProcessing) {
debugMsgs.append("\nbProducerUpdated || doPdfAProcessing is true for job=" + jobIdentityId);
org.w3c.dom.Document document = PdfPostProcessorUtilities.parseXmpMetadata(xmpInput);
debugMsgs.append("\nafter PdfPostProcessorUtilities.parseXmpMetadata for job=" + jobIdentityId);
Element root = document.getDocumentElement();
Element parent = PdfPostProcessorUtilities.findPdfAEntryParent(root);
boolean bNeedSerialization = false;
if (doPdfAProcessing) {
debugMsgs.append("\ndo doPdfAProcessing for job=" + jobIdentityId);
if (!PdfPostProcessorUtilities.doesPdfAEntryExist(parent)) {
debugMsgs.append("\nbefore addPdfAEntryToDOM for job=" + jobIdentityId);
PdfPostProcessorUtilities.addPdfAEntryToDOM(document, parent);
bNeedSerialization = true;
bNeedImport = true;
}
}
if (bProducerUpdated) {
debugMsgs.append("\nbProducerUpdated is true for job=" + jobIdentityId);
PdfPostProcessorUtilities.updateProducerEntryInDOM(parent, pdfProducerString);
bNeedSerialization = true;
bNeedImport = true;
}
if (bNeedSerialization) {
debugMsgs.append("\nbNeedSerialization is true for job=" + jobIdentityId);
xmpInput = PdfPostProcessorUtilities.serializeXmpMetadata(document);
}
}
if (bNeedImport) {
try {
debugMsgs.append("\nbNeedImport is true for job=" + jobIdentityId);
md = XMPService.getDocumentMetadata((PDFDocument)pdfDocument);
md.importXMP(xmpInput);
}
catch (Exception e) {
logger.debug("Import XMP failed due to exception: " + e);
logger.trace(e.getMessage(), (Throwable)e);
}
}
if (bNeedSynchronization) {
debugMsgs.append("\nbNeedSynchronization is true for job=" + jobIdentityId);
md = XMPService.getDocumentMetadata((PDFDocument)pdfDocument);
md.commit();
}
}
errorCode = 16003;
PdfPostProcessorUtilities.applyInitialView(initialView, pdfDocument);
debugMsgs.append("\nafter PdfPostProcessorUtilities.applyInitialView for job=" + jobIdentityId);
SecurityLockPassword lock = PdfPostProcessorUtilities.getLockPassword(encryptionSettings);
debugMsgs.append("\nafter PdfPostProcessorUtilities.getLockPassword for job=" + jobIdentityId);
if (lock != null || forcePdfVersion) {
debugMsgs.append("\nlock != null so securityLock is applied for job=" + jobIdentityId);
docOptions = !doWebOptimization ? (lock == null ? PDFSaveFullOptions.newInstance() : PDFSaveFullOptions.newInstance((SecurityLock)lock)) : (lock == null ? PDFSaveLinearOptions.newInstance() : PDFSaveLinearOptions.newInstance((SecurityLock)lock));
} else {
docOptions = doWebOptimization && !doImagePostProcessing && !doPdfAProcessing ? PDFSaveLinearOptions.newInstance() : PDFSaveIncrementalOptions.newInstance();
}
docOptions.setCloseAfterSave(true);
debugMsgs.append("\nafter creating SharedPdfDocumentOptions for job=" + jobIdentityId);
filePaths.postProcessedDoc = SharedPdfDocumentContext.toDocument((PDFDocument)pdfDocument, (PDFSaveOptions)docOptions);
debugMsgs.append("\nafter SharedPdfDocumentContext.toDocument for job=" + jobIdentityId);
filePaths.postProcessedDoc.setAttribute("file", (Object)filePaths.pdfFilePath);
errorCode = 0;
}
catch (IOException e) {
logger.debug("PdfPostProcessorConstants.doPostProcess() exception: " + e.getMessage());
logger.trace(e.getMessage(), (Throwable)e);
errorCode = 16002;
}
catch (Exception e) {
logger.debug("PdfPostProcessorConstants.doPostProcess() exception: " + e.getMessage());
logger.trace(e.getMessage(), (Throwable)e);
}
finally {
if (xmpInput != null) {
try {
xmpInput.close();
}
catch (IOException e) {
logger.debug("PdfPostProcessor: IO error while closing XMP stream", (Throwable)e);
}
}
}
}
catch (Exception e) {
logger.debug(e.getMessage(), (Object)null, (Object)e);
}
finally {
debugMsgs.append("\nerror code =" + errorCode + " for job=" + jobIdentityId);
logger.debug(debugMsgs.toString());
}
}
return errorCode;
}
private void disposeReturnedDoc() {
if (this.fileToDispose != null) {
this.fileToDispose.dispose();
this.fileToDispose = null;
}
}
}