Commit 4a2e0097 authored by wangqiang's avatar wangqiang

卷内目录生成pdf文件后,将临时目录及文件删除

parent a2a8e48a
......@@ -325,12 +325,13 @@ public class ExtFilesServiceImpl<V extends ExtFilesVO> implements ExtFilesServic
public String convertPdf(String modelName, Map<String, Object> dataMap) {
String targetPath = System.getProperty("user.dir") + "/TEMP/";
File wordFile = new File(targetPath + modelName);
//File wordFile = new File(targetPath + modelName);
String tempFullPath = targetPath + modelName;
File pdfFile2 = new File("TEMP/" + dataMap.get("contextName") + "案卷目录" +"("+dataMap.get("mj") +")"+ ".pdf");
try {
InputStream tempStream2 = Files.newInputStream(wordFile.toPath());
//InputStream tempStream2 = Files.newInputStream(wordFile.toPath());
OutputStream outputStream = Files.newOutputStream(pdfFile2.toPath());
byte[] bytes2 = WordUtils.fillWord(dataMap, tempStream2);
byte[] bytes2 = WordUtils.fillWord(dataMap, tempFullPath);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes2);
WordUtils.wordToPDF(byteArrayInputStream, outputStream);
byteArrayInputStream.close();
......@@ -342,29 +343,25 @@ public class ExtFilesServiceImpl<V extends ExtFilesVO> implements ExtFilesServic
public String fillWordAndConvertPdf(String modelName, Map<String, Object> dataMap, String pdfFileName,String tempDir) {
String targetPath = System.getProperty("user.dir") + "/TEMP/";
File wordFile = new File(targetPath + modelName);
File pdfFile2 = new File(tempDir + File.separator+pdfFileName + ".pdf");
InputStream tempStream = null;
OutputStream outputStream = null;
String tempFullPath = targetPath + modelName;
File pdfFile = new File(tempDir + File.separator+pdfFileName + ".pdf");
ByteArrayInputStream byteArrayInputStream = null;
try {
tempStream = Files.newInputStream(wordFile.toPath());
outputStream = Files.newOutputStream(pdfFile2.toPath());
byte[] bytes2 = WordUtils.fillWord(dataMap, tempStream);
byteArrayInputStream = new ByteArrayInputStream(bytes2);
try(OutputStream outputStream = new FileOutputStream(pdfFile)) {
byte[] wordBytes = WordUtils.fillWord(dataMap, tempFullPath);
byteArrayInputStream = new ByteArrayInputStream(wordBytes);
WordUtils.wordToPDF(byteArrayInputStream, outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
byteArrayInputStream.close();
tempStream.close();
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
if(byteArrayInputStream != null){
try {
byteArrayInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return pdfFile2.getAbsolutePath();
return pdfFile.getAbsolutePath();
}
......
......@@ -308,11 +308,11 @@ public class WordUtils {
* 填充word模板,获取填充后的word文件流:
*
* @param datas: 填充数据
* @param fileStream: 文件模板流 -- 可从OSS/FastDFS上获取流,直接传入;
* @param tempPath: 模板地址
* @return
* @throws IOException
*/
public static byte[] fillWord(Map<String, Object> datas, InputStream fileStream) throws IOException {
public static byte[] fillWord(Map<String, Object> datas, String tempPath) throws IOException {
XWPFTemplate template = null;
ByteArrayOutputStream wordOut = new ByteArrayOutputStream(1024);
try {
......@@ -324,7 +324,7 @@ public class WordUtils {
.build();
//填充文本域:
template = XWPFTemplate.compile(fileStream, config).render(datas);
template = XWPFTemplate.compile(tempPath, config).render(datas);
template.write(wordOut);
} catch (RenderException ex) {
log.error("模板填充失败,请保证模板文本域与参数保持一致。错误信息::{}", ex);
......
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