|
|
|
@ -14,8 +14,8 @@ import com.aspose.cad.*;
|
|
|
|
|
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
|
|
|
|
|
import com.aspose.cad.fileformats.tiff.enums.TiffExpectedFormat;
|
|
|
|
|
import com.aspose.cad.imageoptions.*;
|
|
|
|
|
import com.itextpdf.text.pdf.PdfReader;
|
|
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
|
import org.apache.pdfbox.Loader;
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
|
import org.apache.pdfbox.rendering.ImageType;
|
|
|
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
|
@ -37,7 +37,10 @@ import java.io.*;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
@ -236,9 +239,9 @@ public class FileHandlerService implements InitializingBean {
|
|
|
|
|
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
|
|
|
|
boolean usePasswordCache = fileAttribute.getUsePasswordCache();
|
|
|
|
|
String filePassword = fileAttribute.getFilePassword();
|
|
|
|
|
String pdfPassword = null;
|
|
|
|
|
PDDocument doc = null;
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
PDDocument doc;
|
|
|
|
|
final String[] pdfPassword = {null};
|
|
|
|
|
final int[] pageCount = new int[1];
|
|
|
|
|
if (!forceUpdatedCache) {
|
|
|
|
|
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath);
|
|
|
|
|
if (!CollectionUtils.isEmpty(cacheResult)) {
|
|
|
|
@ -246,64 +249,77 @@ public class FileHandlerService implements InitializingBean {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<String> imageUrls = new ArrayList<>();
|
|
|
|
|
File pdfFile = new File(fileNameFilePath);
|
|
|
|
|
if (!pdfFile.exists()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
int index = pdfFilePath.lastIndexOf(".");
|
|
|
|
|
String folder = pdfFilePath.substring(0, index);
|
|
|
|
|
File path = new File(folder);
|
|
|
|
|
if (!path.exists() && !path.mkdirs()) {
|
|
|
|
|
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
File pdfFile = new File(fileNameFilePath);
|
|
|
|
|
if (!pdfFile.exists()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
doc = PDDocument.load(pdfFile, filePassword);
|
|
|
|
|
doc = Loader.loadPDF(pdfFile, filePassword);
|
|
|
|
|
doc.setResourceCache(new NotResourceCache());
|
|
|
|
|
int pageCount = doc.getNumberOfPages();
|
|
|
|
|
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
|
|
|
|
int index = pdfFilePath.lastIndexOf(".");
|
|
|
|
|
String folder = pdfFilePath.substring(0, index);
|
|
|
|
|
File path = new File(folder);
|
|
|
|
|
if (!path.exists() && !path.mkdirs()) {
|
|
|
|
|
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
|
|
|
|
|
pageCount[0] = doc.getNumberOfPages();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
|
|
|
|
for (Throwable throwable : throwableArray) {
|
|
|
|
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
|
|
|
|
if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
|
|
|
|
|
pdfPassword[0] = PDF_PASSWORD_MSG; //查询到该文件是密码文件 输出带密码的值
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String imageFilePath;
|
|
|
|
|
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
|
|
|
|
|
imageFilePath = folder + File.separator + pageIndex + PDF2JPG_IMAGE_FORMAT;
|
|
|
|
|
BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
|
|
|
|
|
ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getPdf2JpgDpi());
|
|
|
|
|
String imageUrl = this.getPdf2jpgUrl(pdfFilePath, pageIndex);
|
|
|
|
|
imageUrls.add(imageUrl);
|
|
|
|
|
if (!PDF_PASSWORD_MSG.equals(pdfPassword[0])) { //该文件异常 错误原因非密码原因输出错误
|
|
|
|
|
logger.error("Convert pdf exception, pdfFilePath:{}", pdfFilePath, e);
|
|
|
|
|
}
|
|
|
|
|
throw new Exception(e);
|
|
|
|
|
}
|
|
|
|
|
Callable <List<String>> call = () -> {
|
|
|
|
|
try {
|
|
|
|
|
if (!ObjectUtils.isEmpty(filePassword)) { //获取到密码 判断是否是加密文件
|
|
|
|
|
pdfReader = new PdfReader(fileNameFilePath); //读取PDF文件 通过异常获取该文件是否有密码字符
|
|
|
|
|
String imageFilePath;
|
|
|
|
|
BufferedImage image = null;
|
|
|
|
|
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
|
|
|
|
pdfRenderer.setSubsamplingAllowed(true);
|
|
|
|
|
for (int pageIndex = 0; pageIndex < pageCount[0]; pageIndex++) {
|
|
|
|
|
imageFilePath = folder + File.separator + pageIndex + PDF2JPG_IMAGE_FORMAT;
|
|
|
|
|
image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
|
|
|
|
|
ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getPdf2JpgDpi());
|
|
|
|
|
String imageUrl = this.getPdf2jpgUrl(pdfFilePath, pageIndex);
|
|
|
|
|
imageUrls.add(imageUrl);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) { //获取异常方法 判断是否有加密字符串
|
|
|
|
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
|
|
|
|
for (Throwable throwable : throwableArray) {
|
|
|
|
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
|
|
|
|
if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
|
|
|
|
|
pdfPassword = PDF_PASSWORD_MSG; //查询到该文件是密码文件 输出带密码的值
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!PDF_PASSWORD_MSG.equals(pdfPassword)) { //该文件异常 错误原因非密码原因输出错误
|
|
|
|
|
logger.error("Convert pdf exception, pdfFilePath:{}", pdfFilePath, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
image.flush();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new Exception(e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (pdfReader != null) { //关闭
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (usePasswordCache || !PDF_PASSWORD_MSG.equals(pdfPassword)) { //加密文件 判断是否启用缓存命令
|
|
|
|
|
this.addPdf2jpgCache(pdfFilePath, pageCount);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
if (!e.getMessage().contains(PDF_PASSWORD_MSG)) {
|
|
|
|
|
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
|
|
|
|
|
doc.close();
|
|
|
|
|
}
|
|
|
|
|
return imageUrls;
|
|
|
|
|
};
|
|
|
|
|
Future<List<String>> result = pool.submit(call);
|
|
|
|
|
int pdftimeout;
|
|
|
|
|
if(pageCount[0] <=50){
|
|
|
|
|
pdftimeout = ConfigConstants.getPdfTimeout();
|
|
|
|
|
}else if(pageCount[0] <=200){
|
|
|
|
|
pdftimeout = ConfigConstants.getPdfTimeout80();
|
|
|
|
|
}else {
|
|
|
|
|
pdftimeout = ConfigConstants.getPdfTimeout200();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
result.get(pdftimeout, TimeUnit.SECONDS);
|
|
|
|
|
// 如果在超时时间内,没有数据返回:则抛出TimeoutException异常
|
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
|
throw new Exception(e);
|
|
|
|
|
} catch (TimeoutException e) {
|
|
|
|
|
throw new Exception("overtime");
|
|
|
|
|
} finally {
|
|
|
|
|
if (doc != null) { //关闭
|
|
|
|
|
doc.close();
|
|
|
|
|
}
|
|
|
|
|
//关闭
|
|
|
|
|
doc.close();
|
|
|
|
|
}
|
|
|
|
|
if (usePasswordCache || ObjectUtils.isEmpty(filePassword)) { //加密文件 判断是否启用缓存命令
|
|
|
|
|
this.addPdf2jpgCache(pdfFilePath, pageCount[0]);
|
|
|
|
|
}
|
|
|
|
|
return imageUrls;
|
|
|
|
|
}
|
|
|
|
|