Skip to content

Commit 6f732e0

Browse files
committed
增强pdf转换错误处理
1 parent 5579a51 commit 6f732e0

4 files changed

Lines changed: 55 additions & 39 deletions

File tree

docviewer/src/com/log4ic/servlet/DocViewerServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void getDoc(HttpServletRequest request, HttpServletResponse response) {
133133
outp = response.getOutputStream();
134134
if (DocViewer.isSplitPage()) {
135135
if (DocViewer.isEncryption()) {
136-
LOGGER.info("解密文档...");
136+
LOGGER.info("加密文档...");
137137
if (DocViewer.isDynamicKey()) {
138138
byte[] page = DocViewer.encryptToBytes(docId, docPage, secretKey);
139139
in = new ByteArrayInputStream(page);
@@ -146,7 +146,7 @@ public void getDoc(HttpServletRequest request, HttpServletResponse response) {
146146
}
147147
} else {
148148
if (DocViewer.isEncryption()) {
149-
LOGGER.info("解密文档...");
149+
LOGGER.info("加密文档...");
150150
if (DocViewer.isDynamicKey()) {
151151
byte[] page = DocViewer.encryptToBytes(docId, secretKey);
152152
in = new ByteArrayInputStream(page);

docviewer/web/scripts/uploader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
return bp;
7474
},
7575
uploader = new plupload.Uploader($.extend({
76-
runtimes:'html5,gears,flash,silverlight,html4',
76+
runtimes:'gears,flash,silverlight,html5,html4',
7777
url:contextPath + 'upload',
7878
max_file_size:maxSize || '1024mb',
7979
chunk_size:(chunkSize / 1024) + 'mb',

docviewerapi/src/com/log4ic/utils/convert/pdf/PDFConverter.java

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void setCommandConfig(String command) throws Exception {
5151
setCommand(command);
5252
}
5353

54-
public int execConvertCommand(String command, final List<String> outResources) throws InterruptedException, IOException {
54+
public int execCommand(String command, final List<String> outResources) throws InterruptedException, IOException {
5555
LOGGER.debug(command);
5656
final Process convertProcess = Runtime.getRuntime().exec(command);
5757
final InputStream inputStream = convertProcess.getInputStream();
@@ -133,44 +133,61 @@ private PDFConverterDeploy deploy(File pdfFile, String outPath, boolean splitPag
133133
}
134134

135135
outPath = FileUtils.appendFileSeparator(outputDirectory.getPath());
136+
File pdf = new File(FileUtils.getFilePrefix(pdfFile) + "_decrypted.pdf");
137+
File info = new File(FileUtils.appendFileSeparator(outputDirectory.getPath()) + "info");
138+
int pageCount = 0;
139+
if (!pdf.exists() && !info.exists()) {
140+
141+
PdfReader reader = new PdfReader(pdfPath);
142+
pageCount = reader.getNumberOfPages();
143+
144+
// if pdf is a encrypted file unencrypted
145+
if (reader.isEncrypted()) {
146+
LOGGER.debug("encrypted pdf! 准备另存");
147+
pdfPath = PDFSecurer.decrypt(reader, FileUtils.getFilePrefix(pdfFile) + "_decrypted.pdf").getPath();
148+
LOGGER.debug("PDF解密完成");
149+
} else {
150+
LOGGER.debug("---文档未加密---");
151+
}
136152

137-
PdfReader reader = new PdfReader(pdfPath);
138-
int pageCount = reader.getNumberOfPages();
153+
reader.close();
139154

140-
// if pdf is a encrypted file unencrypted
141-
if (reader.isEncrypted()) {
142-
LOGGER.debug("encrypted pdf! 准备另存");
143-
pdfPath = PDFSecurer.decrypt(reader, FileUtils.getFilePrefix(pdfFile) + "_decrypted.pdf").getPath();
144-
LOGGER.debug("PDF解密完成");
145-
} else {
146-
LOGGER.debug("---文档未加密---");
147-
}
155+
if (pageCount != 0) {
156+
OutputStream out = null;
157+
try {
148158

149-
reader.close();
159+
info.createNewFile();
150160

151-
if (pageCount != 0) {
152-
OutputStream out = null;
153-
try {
154-
File info = new File(FileUtils.appendFileSeparator(outputDirectory.getPath()) + "info");
161+
out = new FileOutputStream(info);
162+
163+
out.write((pageCount + "").getBytes("UTF-8"));
164+
} finally {
165+
if (out != null) {
166+
out.flush();
167+
out.close();
168+
}
169+
}
155170

156-
info.createNewFile();
171+
}
172+
} else {
173+
pdfPath = pdf.getPath();
174+
FileInputStream in = new FileInputStream(info);
157175

158-
out = new FileOutputStream(info);
176+
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
159177

160-
out.write((pageCount + "").getBytes("UTF-8"));
178+
try {
179+
pageCount = Integer.parseInt(reader.readLine());
180+
} catch (Exception e) {
161181
} finally {
162-
if (out != null) {
163-
out.flush();
164-
out.close();
165-
}
182+
reader.close();
183+
in.close();
166184
}
167-
168185
}
169186

170187
return new PDFConverterDeploy(outputDirectory, pageCount, COMMAND.replace("${in}", pdfPath)
171188
.replace("${out}", outPath + (splitPage ? "page%.swf" : "page.swf")) +
172189
//是否将图片转换成点阵形式
173-
(poly2bitmap ? " -s poly2bitmap" : ""));
190+
(poly2bitmap ? " -s poly2bitmap -s multiply=4" : ""));
174191

175192
}
176193

@@ -205,11 +222,11 @@ public PDFConvertError checkError(List<String> outResources) {
205222
return error;
206223
}
207224

208-
private void errorHandler(List<String> outResources, final File pdfFile, final String outPath, boolean splitPage, boolean poly2bitmap) throws Exception {
225+
private void errorHandler(String command, final File pdfFile, final String outPath, boolean splitPage, boolean poly2bitmap, List<String> outResources) throws Exception {
209226
// 如果第一次没有将图片转换成点阵图报错,则进行第二次转换,并将图片转换成点阵
210227
final PDFConvertError error = checkError(outResources);
211228
if (error.getType() == PDFConvertErrorType.COMPLEX && poly2bitmap != true) {
212-
LOGGER.debug("---第一次转换失败,进行第二次转换---");
229+
LOGGER.debug("---第一次转换失败,进行第二次转换 poly2bitmap = true ---");
213230
if (splitPage) {
214231
new Thread() {
215232
public void run() {
@@ -222,21 +239,21 @@ public void run() {
222239
}.start();
223240
convertAsOnePageMode(pdfFile, outPath, error.getPosition() + 1);
224241
} else {
225-
convert(pdfFile, outPath, true);
242+
convert(pdfFile, outPath, splitPage, true);
226243
}
227244
} else {
228245
throw new Exception("Conversion failed:" + error.getType() + ";\n" + StringUtils.join(outResources, "\n"));
229246
}
230247
}
231248

232249
public void run(String command, File pdfFile, String outPath, boolean splitPage, boolean poly2bitmap) throws Exception {
233-
LOGGER.debug("---转换开始---");
250+
LOGGER.debug("---开始---");
234251
List<String> outResources = new ArrayList<String>();
235252

236-
if (execConvertCommand(command, outResources) != 0) {
237-
errorHandler(outResources, pdfFile, outPath, splitPage, poly2bitmap);
253+
if (execCommand(command, outResources) != 0) {
254+
errorHandler(command, pdfFile, outPath, splitPage, poly2bitmap, outResources);
238255
}
239-
LOGGER.debug("---转换结束---");
256+
LOGGER.debug("---结束---");
240257
}
241258

242259
public File convertAsOnePageMode(File pdfFile, String outPath) throws Exception {
@@ -272,18 +289,17 @@ public File convertAsOnePageMode(final File pdfFile, final String outPath, final
272289
public void run() {
273290
List<String> outResources = new ArrayList<String>();
274291
try {
275-
if (execConvertCommand(commandExec, outResources) != 0) {
292+
if (execCommand(commandExec, outResources) != 0) {
276293
PDFConvertError error = checkError(outResources);
277294
if (error.getType() == PDFConvertErrorType.COMPLEX && poly2bitmap != true) {
278295
LOGGER.debug("---第一次转换失败,进行第二次转换---");
279-
execConvertCommand(commandExec + " -s poly2bitmap", outResources);
296+
execCommand(commandExec + " -s poly2bitmap", outResources);
280297
} else if (error.getType() != PDFConvertErrorType.NONE) {
281298
throw new Exception("Conversion failed:" + error.getType() + ";\n" + StringUtils.join(outResources, "\n"));
282299
}
283300
}
284301
} catch (Exception e) {
285302
LOGGER.error(e);
286-
e.printStackTrace();
287303
}
288304

289305
}

docviewerapi/src/conf/docviewer.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ docviewer.converter.office.protocol=pipe
3030
#docviewer.converter.office.profile=
3131

3232
#swftools\u547D\u4EE4 pdf2swf windows\u4E0B\u52A0\u73AF\u5883\u53D8\u91CF\u6216\u8005\u524D\u9762\u8FFD\u52A0\u8DEF\u5F84 languagedir \u4E2D\u6587\u652F\u6301 \u9700\u8981\u66F4\u6539\u8DEF\u5F84 \uFF0C\u4F7F\u7528\u65F6\u9700\u66F4\u6539add-to-xpdfrc\u6587\u4EF6\u7684\u8DEF\u5F84\uFF0C\u8DEF\u5F84\u4E2D\u4E0D\u80FD\u7528\u7A7A\u683C\u548C\u4E2D\u6587\uFF0C\u914D\u7F6E\u8BF4\u660E\u8BF7\u53C2\u9605chinese-simplified/README
33-
docviewer.converter.pdf.command=pdf2swf ${in} -o ${out} -T 9 -z -s disablelinks -s languagedir=/home/icode/Workspace/personal/docviewer/etc/xpdf-chinese-simplified
33+
docviewer.converter.pdf.command=pdf2swf ${in} -o ${out} -T 9 -s ignoredraworder -s enablezlib -s disablelinks -s languagedir=/home/icode/Workspace/personal/docviewer/etc/xpdf-chinese-simplified
3434
#\u5355\u9875\u8F6C\u6362\u6A21\u5F0F\u6700\u5927\u7EBF\u7A0B\u6570
3535
docviewer.converter.pdf.mode.singlePage.maxThread=5

0 commit comments

Comments
 (0)