Commit 652c438e authored by 赵啸非's avatar 赵啸非

添加材料数量统计

parent 9719dba9
...@@ -169,6 +169,20 @@ ...@@ -169,6 +169,20 @@
<version>1.12.0</version> <version>1.12.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
......
...@@ -10,6 +10,9 @@ import com.mortals.framework.exception.AppException; ...@@ -10,6 +10,9 @@ import com.mortals.framework.exception.AppException;
import com.mortals.framework.util.DateUtils; import com.mortals.framework.util.DateUtils;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
...@@ -20,7 +23,7 @@ import java.io.*; ...@@ -20,7 +23,7 @@ import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@CommonsLog
public class WordUtil { public class WordUtil {
private Configuration configure = null; private Configuration configure = null;
...@@ -137,6 +140,32 @@ public class WordUtil { ...@@ -137,6 +140,32 @@ public class WordUtil {
} }
public static void pdfToImages(String filePath, String jpegPath) {
log.info(String.format("文件转换开始:%s", DateUtils.getCurrStrDateTime()));
// 转换开始前时间
long old = System.currentTimeMillis();
File file = new File(jpegPath);
List<BufferedImage> imageInfoList = new ArrayList<>();
try (PDDocument doc = PDDocument.load(new File(filePath))) {
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImage(i);
imageInfoList.add(image);
}
BufferedImage mergeImage = mergeImage(false, imageInfoList);
ImageIO.write(mergeImage, "png", file);
// 转换结束后时间
long now = System.currentTimeMillis();
//os.close();
log.info("文件转换结束,共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (IOException e) {
log.error("生成预览图片异常:", e);
throw new AppException("生成预览图片异常");
}
}
/** /**
* 合并任数量的图片成一张图片 * 合并任数量的图片成一张图片
* *
......
...@@ -80,6 +80,11 @@ public class MatterDatumServiceImpl extends AbstractCRUDServiceImpl<MatterDatumD ...@@ -80,6 +80,11 @@ public class MatterDatumServiceImpl extends AbstractCRUDServiceImpl<MatterDatumD
@Autowired @Autowired
private PubdatumService pubdatumService; private PubdatumService pubdatumService;
public static String word[] = {"doc", "dot", "wps", "wpt", "docx", "dotx", "docm", "dotm"};
public static String imgs[] = {"jpg", "png", "jpeg", "gif"};
public static String pdf[] = {"pdf"};
@Override @Override
protected MatterDatumEntity findBefore(MatterDatumEntity params, PageInfo pageInfo, Context context) throws AppException { protected MatterDatumEntity findBefore(MatterDatumEntity params, PageInfo pageInfo, Context context) throws AppException {
if (!ObjectUtils.isEmpty(params.getMaterialName())) { if (!ObjectUtils.isEmpty(params.getMaterialName())) {
...@@ -151,20 +156,45 @@ public class MatterDatumServiceImpl extends AbstractCRUDServiceImpl<MatterDatumD ...@@ -151,20 +156,45 @@ public class MatterDatumServiceImpl extends AbstractCRUDServiceImpl<MatterDatumD
} }
//生成样表预览图片 //生成样表预览图片
String rootPath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/"; String rootPath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/";
String samplePath = rootPath + entity.getSamplePath();
String prePath = "file/preview/"; String prePath = "file/preview/";
String newName = genPreviewPath(rootPath, prePath, entity.getSamplePath());
entity.setPreViewPath(prePath + newName);
super.saveBefore(entity, context);
}
private String genPreviewPath(String rootPath, String prePath, String tempPath) {
//生成样表预览图片
String samplePath = rootPath + tempPath;
String filePath = rootPath + prePath; String filePath = rootPath + prePath;
File pathDir = new File(filePath); File pathDir = new File(filePath);
if (!pathDir.exists()) { if (!pathDir.exists()) {
pathDir.mkdirs(); pathDir.mkdirs();
} }
String newName = RandomUtil.randomNumbers(12) + ".jpg"; String newName = RandomUtil.randomNumbers(12) + ".jpg";
String filePathAll = filePath + newName; String filePathAll = filePath + newName;
WordUtil.convertWordToJPEG(samplePath, filePathAll); //判断文件类型是否为doc pdf jpg png 等
entity.setPreViewPath(prePath + newName); String extName = FileUtil.getSuffix(samplePath);
if (this.isExsitArry(extName, word)) {
WordUtil.convertWordToJPEG(samplePath, filePathAll);
}else if(this.isExsitArry(extName, pdf)){
WordUtil.pdfToImages(samplePath,filePathAll);
}else if(this.isExsitArry(extName,imgs)){
try {
com.mortals.framework.util.FileUtil.copyFile(samplePath,filePathAll);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
return newName;
}
super.saveBefore(entity, context); public boolean isExsitArry(String chex, String arry[]) {
for (String ex : arry) {
if (chex.equalsIgnoreCase(ex)) {
return true;
}
}
return false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment