* tif图片预览,改为支持转换为jpg后预览(JAI支持);加入tif->jpg->pdf转换后,以pdf格式预览,此种模式可完美支持打印纸张自适应。

This commit is contained in:
zhangzhen1979 2021-12-03 19:05:31 +08:00 committed by kl
parent 14151a6c46
commit b7760ab42a
7 changed files with 191 additions and 15 deletions

Binary file not shown.

Binary file not shown.

View File

@ -250,6 +250,26 @@
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai_core</artifactId>
<version>1.1.2-beta</version>
<systemPath>${basedir}/lib/jai_core-1.1.2-beta.jar</systemPath>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai_codec</artifactId>
<version>1.1.3</version>
<systemPath>${basedir}/lib/jai_codec-1.1.3.jar</systemPath>
</dependency>
</dependencies>
<build>

View File

@ -67,7 +67,7 @@ media.convert.disable = ${KK_MEDIA_CONVERT_DISABLE:false}
#支持转换的视频类型
convertMedias = ${KK_CONVERTMEDIAS:avi,mov,wmv,mkv,3gp,rm}
#office类型文档(word ppt)样式,默认为图片(image)可配置为pdf预览时也有按钮切换
office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:image}
office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:pdf}
#是否关闭office预览切换开关默认为false可配置为true关闭
office.preview.switch.disabled = ${KK_OFFICE_PREVIEW_SWITCH_DISABLED:false}

View File

@ -15,7 +15,6 @@ public interface FilePreview {
String COMPRESS_FILE_PREVIEW_PAGE = "compress";
String MEDIA_FILE_PREVIEW_PAGE = "media";
String PICTURE_FILE_PREVIEW_PAGE = "picture";
String TIFF_FILE_PREVIEW_PAGE = "tiff";
String OFD_FILE_PREVIEW_PAGE = "ofd";
String OFFICE_PICTURE_FILE_PREVIEW_PAGE = "officePicture";
String TXT_FILE_PREVIEW_PAGE = "txt";

View File

@ -1,11 +1,20 @@
package cn.keking.service.impl;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
import cn.keking.model.FileType;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.WebUtils;
import cn.keking.utils.ConvertPicUtil;
import cn.keking.utils.DownloadUtils;
import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* tiff 图片文件处理
@ -16,9 +25,8 @@ import org.springframework.util.StringUtils;
public class TiffFilePreviewImpl implements FilePreview {
private final PictureFilePreviewImpl pictureFilePreview;
private static final String INITIALIZE_MEMORY_SIZE = "initializeMemorySize";
//默认初始化 50MB 内存
private static final long INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT = 1024L * 1024 * 50;
private final String fileDir = ConfigConstants.getFileDir();
public TiffFilePreviewImpl(PictureFilePreviewImpl pictureFilePreview) {
this.pictureFilePreview = pictureFilePreview;
@ -26,13 +34,55 @@ public class TiffFilePreviewImpl implements FilePreview {
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
pictureFilePreview.filePreviewHandle(url,model,fileAttribute);
String fileSize = WebUtils.getUrlParameterReg(url,INITIALIZE_MEMORY_SIZE);
if(StringUtils.hasText(fileSize)){
model.addAttribute(INITIALIZE_MEMORY_SIZE,fileSize);
}else {
model.addAttribute(INITIALIZE_MEMORY_SIZE,Long.toString(INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT));
}
return TIFF_FILE_PREVIEW_PAGE;
String inputFileName = url.substring(url.lastIndexOf("/") + 1);
String inputFileExt = inputFileName.substring(inputFileName.lastIndexOf(".") + 1);
String uuid = UUID.randomUUID().toString().replaceAll("-","");
String tiffFileName = uuid + "." + inputFileExt;
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, tiffFileName);
if (response.isFailure()) {
return NOT_SUPPORTED_FILE_PAGE;
}
String strTiffPath = response.getContent();
File fileTiff = new File(strTiffPath);
File fileJpg = ConvertPicUtil.convertPic2Jpg(strTiffPath, fileDir + uuid + ".jpg");
if(fileJpg.exists()){
// 转换后的tif没用了可以删掉了
fileTiff.delete();
String baseUrl = BaseUrlFilter.getBaseUrl();
if("pdf".equalsIgnoreCase(fileAttribute.getOfficePreviewType())){
File filePdf = ConvertPicUtil.convertJpg2Pdf(fileDir + uuid + ".jpg", fileDir + uuid + ".pdf");
if(filePdf.exists()){
// 转换后的jpg没用了可以删掉了
fileJpg.delete();
String pdfUrl = baseUrl + uuid + ".pdf";
model.addAttribute("pdfUrl", pdfUrl);
return PDF_FILE_PREVIEW_PAGE;
}
}else{
String jpgUrl = baseUrl + uuid + ".jpg";
fileAttribute.setName(uuid + ".jpg");
fileAttribute.setType(FileType.PICTURE);
fileAttribute.setSuffix("jpg");
fileAttribute.setUrl(jpgUrl);
List<String> imgUrls = new ArrayList<>();
imgUrls.add(jpgUrl);
model.addAttribute("imgUrls", imgUrls);
model.addAttribute("currentUrl", jpgUrl);
}
}
return PICTURE_FILE_PREVIEW_PAGE;
}
}

View File

@ -0,0 +1,107 @@
package cn.keking.utils;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;
import org.springframework.stereotype.Component;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import java.io.*;
@Component
public class ConvertPicUtil {
/**
* 图片 JPG
* 支持输入格式如下BMPGIFFlashPixJPEGPNGPMNTIFFWBMP
* @param strInputFile 输入文件的路径和文件名
* @param strOutputFile 输出文件的路径和文件名
* @return
*/
public static File convertPic2Jpg(String strInputFile, String strOutputFile) {
// 读取源图片文件
RenderedOp roInput = JAI.create("fileload", strInputFile);
try {
strOutputFile = strOutputFile.replaceAll("\\\\", "/");
File fileJpgPath = new File(strOutputFile.substring(0, strOutputFile.lastIndexOf("/")));
if(!fileJpgPath.exists()){
fileJpgPath.mkdirs();
}
File fileJpg=new File(strOutputFile);
OutputStream ops = new FileOutputStream(fileJpg);
// 文件存储输出流
JPEGEncodeParam param = new JPEGEncodeParam();
ImageEncoder image = ImageCodec.createImageEncoder("JPEG", ops,
param); // 指定输出格式
// 解析输出流进行输出
image.encode(roInput);
// 关闭流
ops.close();
return fileJpg;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将Jpg图片转换为Pdf文件
* @param strJpgFile 输入的jpg的路径和文件名
* @param strPdfFile 输出的pdf的路径和文件名
* @return
*/
public static File convertJpg2Pdf(String strJpgFile, String strPdfFile) {
Document document = new Document();
// 设置文档页边距
document.setMargins(0,0,0,0);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strPdfFile);
PdfWriter.getInstance(document, fos);
// 打开文档
document.open();
// 获取图片的宽高
Image image = Image.getInstance(strJpgFile);
float floatImageHeight=image.getScaledHeight();
float floatImageWidth=image.getScaledWidth();
// 设置页面宽高与图片一致
Rectangle rectangle = new Rectangle(floatImageWidth, floatImageHeight);
document.setPageSize(rectangle);
// 图片居中
image.setAlignment(Image.ALIGN_CENTER);
//新建一页添加图片
document.newPage();
document.add(image);
} catch (Exception ioe) {
ioe.printStackTrace();
return null;
} finally {
//关闭文档
document.close();
try {
fos.flush();
fos.close();
File filePDF = new File(strPdfFile);
return filePDF;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}