@ -15,12 +15,13 @@ import com.aspose.cad.Image;
|
|||||||
import com.aspose.cad.LoadOptions;
|
import com.aspose.cad.LoadOptions;
|
||||||
import com.aspose.cad.imageoptions.CadRasterizationOptions;
|
import com.aspose.cad.imageoptions.CadRasterizationOptions;
|
||||||
import com.aspose.cad.imageoptions.PdfOptions;
|
import com.aspose.cad.imageoptions.PdfOptions;
|
||||||
import com.itextpdf.text.exceptions.BadPasswordException;
|
|
||||||
import com.itextpdf.text.pdf.PdfReader;
|
import com.itextpdf.text.pdf.PdfReader;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.rendering.ImageType;
|
import org.apache.pdfbox.rendering.ImageType;
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
||||||
|
import org.apache.poi.EncryptedDocumentException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -50,6 +51,7 @@ public class FileHandlerService {
|
|||||||
private final String fileDir = ConfigConstants.getFileDir();
|
private final String fileDir = ConfigConstants.getFileDir();
|
||||||
private final static String pdf2jpg_image_format = ".jpg";
|
private final static String pdf2jpg_image_format = ".jpg";
|
||||||
private final CacheService cacheService;
|
private final CacheService cacheService;
|
||||||
|
private static final String pdf_password_msg = "password";
|
||||||
|
|
||||||
@Value("${server.tomcat.uri-encoding:UTF-8}")
|
@Value("${server.tomcat.uri-encoding:UTF-8}")
|
||||||
private String uriEncoding;
|
private String uriEncoding;
|
||||||
@ -223,6 +225,7 @@ public class FileHandlerService {
|
|||||||
public List<String> pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception {
|
public List<String> pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception {
|
||||||
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
||||||
String filePassword = fileAttribute.getFilePassword();
|
String filePassword = fileAttribute.getFilePassword();
|
||||||
|
String pdfPassword = null;
|
||||||
PDDocument doc = null;
|
PDDocument doc = null;
|
||||||
PdfReader pdfReader = null;
|
PdfReader pdfReader = null;
|
||||||
if (!forceUpdatedCache) {
|
if (!forceUpdatedCache) {
|
||||||
@ -256,18 +259,25 @@ public class FileHandlerService {
|
|||||||
imageUrls.add(imageUrl);
|
imageUrls.add(imageUrl);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
pdfReader = new PdfReader(pdfFilePath); //判断pdf文件是否加密 缓存不加密文件
|
pdfReader = new PdfReader(pdfFilePath); //读取PDF文件
|
||||||
this.addPdf2jpgCache(pdfFilePath, pageCount);
|
} catch (Exception e) { //获取异常方法 判断是否有加密字符串
|
||||||
} catch (BadPasswordException e) {
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
||||||
|
for (Throwable throwable : throwableArray) {
|
||||||
} catch (Exception e) {
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
||||||
e.printStackTrace();
|
if (e.getMessage().toLowerCase().contains(pdf_password_msg)) {
|
||||||
|
pdfPassword = pdf_password_msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.error("Convert pdf exception, pdfFilePath:{}", pdfFilePath, e);
|
||||||
}finally {
|
}finally {
|
||||||
if (pdfReader != null) { //关闭
|
if (pdfReader != null) { //关闭
|
||||||
pdfReader.close();
|
pdfReader.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(!pdfPassword.equals(pdf_password_msg)){ //判断是否加密文件 加密文件不缓存
|
||||||
|
this.addPdf2jpgCache(pdfFilePath, pageCount);
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
|
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
|
||||||
throw new Exception(e);
|
throw new Exception(e);
|
||||||
@ -286,7 +296,7 @@ public class FileHandlerService {
|
|||||||
* @param outputFilePath pdf输出文件路径
|
* @param outputFilePath pdf输出文件路径
|
||||||
* @return 转换是否成功
|
* @return 转换是否成功
|
||||||
*/
|
*/
|
||||||
public boolean cadToPdf(String inputFilePath, String outputFilePath) {
|
public String cadToPdf(String inputFilePath, String outputFilePath) throws Exception {
|
||||||
File outputFile = new File(outputFilePath);
|
File outputFile = new File(outputFilePath);
|
||||||
LoadOptions opts = new LoadOptions();
|
LoadOptions opts = new LoadOptions();
|
||||||
opts.setSpecifiedEncoding(CodePages.SimpChinese);
|
opts.setSpecifiedEncoding(CodePages.SimpChinese);
|
||||||
@ -300,21 +310,22 @@ public class FileHandlerService {
|
|||||||
cadRasterizationOptions.setDrawType(1);
|
cadRasterizationOptions.setDrawType(1);
|
||||||
PdfOptions pdfOptions = new PdfOptions();
|
PdfOptions pdfOptions = new PdfOptions();
|
||||||
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
|
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
|
||||||
OutputStream stream;
|
OutputStream stream = null;
|
||||||
try {
|
try {
|
||||||
stream = new FileOutputStream(outputFile);
|
stream = new FileOutputStream(outputFile);
|
||||||
cadImage.save(stream, pdfOptions);
|
cadImage.save(stream, pdfOptions);
|
||||||
stream.close();
|
|
||||||
cadImage.close();
|
|
||||||
return true;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e);
|
logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e);
|
||||||
|
return "null";
|
||||||
} finally {
|
} finally {
|
||||||
|
if (stream != null) { //关闭
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
if (cadImage != null) { //关闭
|
if (cadImage != null) { //关闭
|
||||||
cadImage.close();
|
cadImage.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return "true";
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user