Html2PDFWebkit.java
13.9 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.html2pdf.Html2PdfFontPaths
* com.adobe.native2pdf.xml.FiletypeSettings
* com.adobe.native2pdf.xml.FiletypeSettings$Settings
* com.adobe.native2pdf.xml.FiletypeSettings$Settings$Html2Pdf
* com.adobe.native2pdf.xml.InitialView
* com.adobe.native2pdf.xml.SecuritySettings
* com.adobe.native2pdf.xml.SecuritySettings$Settings
* com.adobe.pdfg.common.AESProperties
* com.adobe.pdfg.common.JobConfiguration
* com.adobe.pdfg.common.PDFGGlobalCache
* com.adobe.pdfg.common.SettingValidator
* com.adobe.pdfg.common.Utils
* com.adobe.pdfg.exception.ConversionException
* com.adobe.pdfg.exception.FileFormatNotSupportedException
* com.adobe.pdfg.exception.InvalidParameterException
* com.adobe.pdfg.logging.PDFGLogger
* com.adobe.pdfg.service.api.PDFGConfigService
* com.adobe.service.ConnectionFactory
* com.day.cq.dam.handler.gibson.fontmanager.FontManagerService
*/
package com.adobe.pdfg.impl;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.html2pdf.Html2PdfFontPaths;
import com.adobe.native2pdf.xml.FiletypeSettings;
import com.adobe.native2pdf.xml.InitialView;
import com.adobe.native2pdf.xml.SecuritySettings;
import com.adobe.pdfg.callbacks.HTMLToPDFTransactionCallback;
import com.adobe.pdfg.common.AESProperties;
import com.adobe.pdfg.common.JobConfiguration;
import com.adobe.pdfg.common.PDFGGlobalCache;
import com.adobe.pdfg.common.SettingValidator;
import com.adobe.pdfg.common.Utils;
import com.adobe.pdfg.exception.ConversionException;
import com.adobe.pdfg.exception.FileFormatNotSupportedException;
import com.adobe.pdfg.exception.InvalidParameterException;
import com.adobe.pdfg.impl.GeneratePDFServiceImpl;
import com.adobe.pdfg.impl.GeneratePDFUtil;
import com.adobe.pdfg.impl.Html2PDFConvertorHelper;
import com.adobe.pdfg.impl.utils.AdjustableSemaphore;
import com.adobe.pdfg.logging.PDFGLogger;
import com.adobe.pdfg.postprocess.PostProcessFileInfo;
import com.adobe.pdfg.service.api.PDFGConfigService;
import com.adobe.pdfg.transaction.TransactionCallback;
import com.adobe.pdfg.transaction.TransactionTemplate;
import com.adobe.service.ConnectionFactory;
import com.day.cq.dam.handler.gibson.fontmanager.FontManagerService;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Html2PDFWebkit
implements Html2PDFConvertorHelper.IHtml2PDFConvertor {
protected PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(Html2PDFWebkit.class);
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
* Converted monitor instructions to comments
* Lifted jumps to return sites
*/
public Map createPDF(GeneratePDFServiceImpl coreSvcImpl, String inputUrl, String fileTypeSettingsName, String securitySettingsName, Document settingsDoc, Document xmpDoc, String configString, File pdfgTmpDir, int spideringLevel, boolean isURL) throws ConversionException, FileFormatNotSupportedException, InvalidParameterException, Exception {
String jobIdentityId = (String)Utils.threadLocalValue.get();
long waitTime = 0;
StringBuilder debugMsgs = new StringBuilder();
long startTime = System.currentTimeMillis();
if (coreSvcImpl.getHtmlToPdfConversionLock() == null) {
debugMsgs.append("\nbefore synchronized(Html2PDFWebkit.class) for job=" + jobIdentityId);
Class<Html2PDFWebkit> class_ = Html2PDFWebkit.class;
// MONITORENTER : com.adobe.pdfg.impl.Html2PDFWebkit.class
if (coreSvcImpl.getHtmlToPdfConversionLock() == null) {
debugMsgs.append("\nbefore initializeHTMLConversionLock for job=" + jobIdentityId);
coreSvcImpl.initializeHtmlToPdfConversionLock();
debugMsgs.append("\nafter initializeHTMLConversionLock for job=" + jobIdentityId);
}
// MONITOREXIT : class_
}
this.pdfgLogger.info("001-024", new Object[]{inputUrl, new Date(startTime), jobIdentityId});
this.pdfgLogger.info("001-016", new Object[]{inputUrl, "HTML to PDF"});
boolean success = true;
debugMsgs.append("\nbefore htmlConversionLock.acquire() for job=" + jobIdentityId);
long waitStartTime = System.currentTimeMillis();
coreSvcImpl.getHtmlToPdfConversionLock().acquire();
long waitEndTime = System.currentTimeMillis();
debugMsgs.append("\nafter htmlConversionLock.acquire() for job=" + jobIdentityId);
waitTime -= waitStartTime;
try {
Map xmpFile2;
try {
File tempPdf = new File(pdfgTmpDir, "TempHtmlToPdf");
File pdfFile = new File(pdfgTmpDir, "HtmlToPdfResult");
JobConfiguration config = null;
try {
config = PDFGGlobalCache.getJobConfiguration((String)configString);
}
catch (Exception e) {
throw new InvalidParameterException(1001, (Throwable)e);
}
if (config == null) {
throw new InvalidParameterException(80001);
}
int timeoutSeconds = AESProperties.getMaximumTimeout();
timeoutSeconds = Math.max(10, timeoutSeconds);
timeoutSeconds = config.getConversionTimeoutSetting(10, timeoutSeconds, timeoutSeconds);
HTMLToPDFTransactionCallback transactionCallback = new HTMLToPDFTransactionCallback(coreSvcImpl.getHtmlToPdfFactory());
transactionCallback.setSourcePath(inputUrl);
transactionCallback.setDestinationPath(tempPdf.getAbsolutePath());
transactionCallback.setTimeout(timeoutSeconds);
transactionCallback.setHtml2PDFparams(config);
transactionCallback.setIsURL(isURL);
transactionCallback.setFontsPath(this.retrieveHtml2PdfFontPaths(coreSvcImpl));
transactionCallback.setHTMLFont(coreSvcImpl.getFallbackFontForHTMLConversions());
TransactionTemplate _txTemplate = coreSvcImpl.getTransactionTemplate();
_txTemplate.setPropagationBehavior(0);
_txTemplate.setTimeout(timeoutSeconds + 200);
this.pdfgLogger.debug("before calling _txTemplate.execute for job=" + jobIdentityId);
_txTemplate.execute(transactionCallback);
FiletypeSettings.Settings.Html2Pdf html2pdf = config.getFiletypeSettings().getHtml2Pdf();
InitialView initialView = html2pdf == null ? null : html2pdf.getInitialView();
SecuritySettings.Settings securitySettings = config.getSecuritySettings();
boolean shouldApplySecurity = Utils.shouldApplySecurity((SecuritySettings.Settings)securitySettings);
String targetPdfVersion = null;
if (shouldApplySecurity) {
SettingValidator validator = new SettingValidator(null, securitySettings);
if (validator.areEncryptionPasswordsIdentical()) {
throw new InvalidParameterException(80009);
}
int acrobatVersion = securitySettings.getAcrobatVersion();
if (acrobatVersion > 5) {
targetPdfVersion = "1." + (acrobatVersion - 1);
}
GeneratePDFUtil.updateSecuritySettings(coreSvcImpl.getConfigService(), securitySettings);
} else {
securitySettings = null;
}
PostProcessFileInfo pp = new PostProcessFileInfo();
pp.attachmentFilePath = "";
pp.logFilePath = "";
pp.tempPdfFilePath = tempPdf.getCanonicalPath();
pp.pdfFilePath = pdfFile.getCanonicalPath();
pp.xmpFilePath = "";
if (xmpDoc != null) {
File xmpFile2 = new File(pdfgTmpDir, "HtmlToPdfXmp");
xmpDoc.copyToFile(xmpFile2);
this.pdfgLogger.info("001-020", new Object[]{inputUrl, xmpFile2.getName()});
pp.xmpFilePath = xmpFile2.getAbsolutePath();
}
coreSvcImpl.pdfPostProcess(pp, null, initialView, securitySettings, true, targetPdfVersion, "PDF generator", GeneratePDFUtil.applyWaterMark());
xmpFile2 = this.getResponse(pp.pdfFilePath, null, null, pp.postProcessedDoc);
Object var36_36 = null;
debugMsgs.append("\nbefore htmlConversionLock.release() for job=" + jobIdentityId);
}
catch (Exception e) {
this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
success = false;
throw e;
}
coreSvcImpl.getHtmlToPdfConversionLock().release();
debugMsgs.append("\nafter htmlConversionLock.release() for job=" + jobIdentityId);
this.pdfgLogger.debug(debugMsgs.toString());
long endTime = System.currentTimeMillis();
if (success) {
this.pdfgLogger.info("001-027", inputUrl);
} else {
this.pdfgLogger.info("001-028", inputUrl);
}
this.pdfgLogger.info("001-025", new Object[]{inputUrl, new Date(), jobIdentityId});
this.pdfgLogger.info("001-030", new Object[]{inputUrl, waitTime, jobIdentityId});
this.pdfgLogger.info("001-026", new Object[]{inputUrl, endTime - startTime - waitTime, jobIdentityId});
return xmpFile2;
}
catch (Throwable var35_40) {
Object var36_37 = null;
debugMsgs.append("\nbefore htmlConversionLock.release() for job=" + jobIdentityId);
coreSvcImpl.getHtmlToPdfConversionLock().release();
debugMsgs.append("\nafter htmlConversionLock.release() for job=" + jobIdentityId);
this.pdfgLogger.debug(debugMsgs.toString());
long endTime = System.currentTimeMillis();
if (success) {
this.pdfgLogger.info("001-027", inputUrl);
} else {
this.pdfgLogger.info("001-028", inputUrl);
}
this.pdfgLogger.info("001-025", new Object[]{inputUrl, new Date(), jobIdentityId});
this.pdfgLogger.info("001-030", new Object[]{inputUrl, waitTime, jobIdentityId});
this.pdfgLogger.info("001-026", new Object[]{inputUrl, endTime - startTime - waitTime, jobIdentityId});
throw var35_40;
}
}
private Html2PdfFontPaths retrieveHtml2PdfFontPaths(GeneratePDFServiceImpl coreSvcImpl) {
Html2PdfFontPaths fontPaths = new Html2PdfFontPaths();
fontPaths.customerFontPath = "";
fontPaths.systemFontPath = "";
fontPaths.adobeFontPath = "";
try {
fontPaths.customerFontPath = this.getNormalizedFontPath(coreSvcImpl.getFontManagerService().getCustomerFontDirectory());
}
catch (Exception ex2) {
this.pdfgLogger.warning(ex2.getLocalizedMessage(), "");
this.pdfgLogger.trace(ex2.getMessage(), null, (Throwable)ex2);
}
try {
fontPaths.systemFontPath = this.getNormalizedFontPath(coreSvcImpl.getFontManagerService().getSystemFontDirectory());
}
catch (Exception ex2) {
this.pdfgLogger.warning(ex2.getLocalizedMessage(), "");
this.pdfgLogger.trace(ex2.getMessage(), null, (Throwable)ex2);
}
try {
fontPaths.adobeFontPath = this.getNormalizedFontPath(coreSvcImpl.getFontManagerService().getAdobeServerFontDirectory());
}
catch (Exception ex2) {
this.pdfgLogger.warning(ex2.getLocalizedMessage(), "");
this.pdfgLogger.trace(ex2.getMessage(), null, (Throwable)ex2);
}
return fontPaths;
}
private String getNormalizedFontPath(String path) {
String fileSeparator = System.getProperty("file.separator");
String pathSeparator = ";";
String modifiedPath = "";
if (path == null) {
return modifiedPath;
}
StringTokenizer tokens = new StringTokenizer(path, ";");
while (tokens.hasMoreTokens()) {
path = tokens.nextToken();
if (path != null && !"".equals(path)) {
modifiedPath = !path.endsWith(fileSeparator) ? modifiedPath + path + fileSeparator : modifiedPath + path;
}
if (!tokens.hasMoreTokens()) continue;
modifiedPath = modifiedPath + ";";
}
return modifiedPath;
}
private Map getResponse(String pdfFilePath, String jdfFilePath, String logFilePath, Document postProcessedDoc) {
File logFile;
File pdfFile;
HashMap<String, Document> response = new HashMap<String, Document>();
Document convertedDoc = postProcessedDoc;
if (convertedDoc == null && pdfFilePath != null && (pdfFile = new File(pdfFilePath)).exists() && pdfFile.length() > 0) {
convertedDoc = new Document(pdfFile, true);
}
if (pdfFilePath != null && pdfFilePath.indexOf(".pdf") < 0) {
convertedDoc.setAttribute("file", (Object)(pdfFilePath + ".pdf"));
convertedDoc.setContentType("application/pdf");
}
Document logDoc = null;
if (logFilePath != null && (logFile = new File(logFilePath)).exists() && logFile.length() > 0) {
logDoc = new Document(logFile, true);
if (logFilePath.indexOf(".log") < 0) {
logDoc.setAttribute("file", (Object)(logFilePath + ".log"));
logDoc.setContentType("text/plain");
}
}
if (convertedDoc != null) {
response.put("ConvertedDoc", convertedDoc);
}
if (logDoc != null) {
response.put("LogDoc", logDoc);
}
return response;
}
}