|
|
|
@ -7,10 +7,12 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
@ -74,18 +76,21 @@ public class Jpg2PdfUtil {
|
|
|
|
|
List<PdfReader> pdfReaders = new ArrayList<>();
|
|
|
|
|
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
|
|
|
|
|
Iterator<String> iterator = files.iterator();
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
PdfReader pdfReader = check(iterator.next());
|
|
|
|
|
pdfReader = check(iterator.next());
|
|
|
|
|
if(null != pdfReader){
|
|
|
|
|
pdfReaders.add(pdfReader);
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!CollectionUtils.isEmpty(pdfReaders)){
|
|
|
|
|
OutputStream outputStream = null;
|
|
|
|
|
//创建文档容器
|
|
|
|
|
Document document = new Document(PageSize.A4,0,0,0,0);
|
|
|
|
|
PdfCopy copy = null;
|
|
|
|
|
try {
|
|
|
|
|
outputStream = response.getOutputStream();
|
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
|
document.open();
|
|
|
|
|
for (PdfReader reader : pdfReaders) {
|
|
|
|
@ -101,6 +106,14 @@ public class Jpg2PdfUtil {
|
|
|
|
|
if(null != document){
|
|
|
|
|
document.close();
|
|
|
|
|
}
|
|
|
|
|
if(null != outputStream){
|
|
|
|
|
try {
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
outputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -115,8 +128,9 @@ public class Jpg2PdfUtil {
|
|
|
|
|
private static void copy2Pdf(PdfCopy copy, PdfReader reader) throws IOException, BadPdfFormatException {
|
|
|
|
|
try {
|
|
|
|
|
int n = reader.getNumberOfPages();
|
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
PdfImportedPage page = copy.getImportedPage(reader, j);
|
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
|
if(null != page) {
|
|
|
|
|
copy.addPage(page);
|
|
|
|
|
}
|
|
|
|
|