|
@@ -4,6 +4,7 @@ import com.izouma.awesomeAdmin.exception.BusinessException;
|
|
|
import com.izouma.awesomeAdmin.utils.FileUtils;
|
|
import com.izouma.awesomeAdmin.utils.FileUtils;
|
|
|
import com.jacob.activeX.ActiveXComponent;
|
|
import com.jacob.activeX.ActiveXComponent;
|
|
|
import com.jacob.com.Dispatch;
|
|
import com.jacob.com.Dispatch;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.util.TempFile;
|
|
import org.apache.poi.util.TempFile;
|
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
@@ -17,14 +18,16 @@ import javax.annotation.PreDestroy;
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
|
|
|
|
@Controller
|
|
@Controller
|
|
|
|
|
+@Slf4j
|
|
|
public class Word2PDFController {
|
|
public class Word2PDFController {
|
|
|
- private ActiveXComponent app = null;
|
|
|
|
|
|
|
+ private ActiveXComponent app = null;
|
|
|
|
|
+ private Dispatch documents = null;
|
|
|
|
|
|
|
|
@PostMapping(value = "/word2pdf", produces = "application/pdf")
|
|
@PostMapping(value = "/word2pdf", produces = "application/pdf")
|
|
|
@ResponseBody
|
|
@ResponseBody
|
|
|
public byte[] word2pdf(@RequestParam("file") MultipartFile file) {
|
|
public byte[] word2pdf(@RequestParam("file") MultipartFile file) {
|
|
|
|
|
|
|
|
- System.out.println("开始转换...");
|
|
|
|
|
|
|
+ log.info("开始转换");
|
|
|
// 开始时间
|
|
// 开始时间
|
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
|
try {
|
|
try {
|
|
@@ -32,32 +35,20 @@ public class Word2PDFController {
|
|
|
File target = TempFile.createTempFile("word2pdf", ".pdf");
|
|
File target = TempFile.createTempFile("word2pdf", ".pdf");
|
|
|
FileUtils.write(file.getInputStream(), word);
|
|
FileUtils.write(file.getInputStream(), word);
|
|
|
|
|
|
|
|
- // 打开word
|
|
|
|
|
-
|
|
|
|
|
- // 设置word不可见,很多博客下面这里都写了这一句话,其实是没有必要的,因为默认就是不可见的,如果设置可见就是会打开一个word文档,对于转化为pdf明显是没有必要的
|
|
|
|
|
- //app.setProperty("Visible", false);
|
|
|
|
|
- // 获得word中所有打开的文档
|
|
|
|
|
- Dispatch documents = app.getProperty("Documents").toDispatch();
|
|
|
|
|
System.out.println("打开文件: " + word.getPath());
|
|
System.out.println("打开文件: " + word.getPath());
|
|
|
- // 打开文档
|
|
|
|
|
Dispatch document = Dispatch.call(documents, "Open", word.getPath(), false, true).toDispatch();
|
|
Dispatch document = Dispatch.call(documents, "Open", word.getPath(), false, true).toDispatch();
|
|
|
- // 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
|
|
|
|
|
if (target.exists()) {
|
|
if (target.exists()) {
|
|
|
target.delete();
|
|
target.delete();
|
|
|
}
|
|
}
|
|
|
- System.out.println("另存为: " + target.getPath());
|
|
|
|
|
- // 另存为,将文档报错为pdf,其中word保存为pdf的格式宏的值是17
|
|
|
|
|
|
|
+ log.info("另存为: " + target.getPath());
|
|
|
Dispatch.call(document, "SaveAs", target.getPath(), 17);
|
|
Dispatch.call(document, "SaveAs", target.getPath(), 17);
|
|
|
- // 关闭文档
|
|
|
|
|
Dispatch.call(document, "Close", false);
|
|
Dispatch.call(document, "Close", false);
|
|
|
- // 结束时间
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
long end = System.currentTimeMillis();
|
|
|
- System.out.println("转换成功,用时:" + (end - start) + "ms");
|
|
|
|
|
|
|
+ log.info("转换成功,用时:" + (end - start) + "ms");
|
|
|
|
|
|
|
|
return org.apache.commons.io.FileUtils.readFileToByteArray(target);
|
|
return org.apache.commons.io.FileUtils.readFileToByteArray(target);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- e.getMessage();
|
|
|
|
|
- System.out.println("转换失败" + e.getMessage());
|
|
|
|
|
|
|
+ log.error("转换失败", e);
|
|
|
throw new BusinessException("转换失败" + e.getMessage());
|
|
throw new BusinessException("转换失败" + e.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -66,6 +57,7 @@ public class Word2PDFController {
|
|
|
@PostConstruct
|
|
@PostConstruct
|
|
|
public void init() {
|
|
public void init() {
|
|
|
app = new ActiveXComponent("Word.Application");
|
|
app = new ActiveXComponent("Word.Application");
|
|
|
|
|
+ documents = app.getProperty("Documents").toDispatch();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PreDestroy
|
|
@PreDestroy
|