|
@@ -1,11 +1,129 @@
|
|
|
package org.springblade;
|
|
|
|
|
|
+import com.aspose.cad.internal.gl.S;
|
|
|
+import com.documents4j.api.DocumentType;
|
|
|
+import com.documents4j.api.IConverter;
|
|
|
+import com.documents4j.job.LocalConverter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jodconverter.core.DocumentConverter;
|
|
|
+import org.jodconverter.core.office.OfficeManager;
|
|
|
+import org.jodconverter.core.office.OfficeUtils;
|
|
|
+import org.jodconverter.local.JodConverter;
|
|
|
+import org.jodconverter.local.office.LocalOfficeManager;
|
|
|
+import org.jodconverter.local.office.OfficeConnection;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.net.ConnectException;
|
|
|
+@Slf4j
|
|
|
@SpringBootTest
|
|
|
class ServerMainTests {
|
|
|
+ public static void main(String[] args) throws ConnectException {
|
|
|
+ String docPath="/Users/wangrui/Downloads/aaa.doc";
|
|
|
+// String docPath="https://bi.platomix.net/uploads/aaa.doc";
|
|
|
+ String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
|
|
|
+
|
|
|
+// docTopdf(docPath,pdfPath);
|
|
|
+// wordToPdf(docPath,pdfPath);
|
|
|
+ office2PDF(docPath,pdfPath);
|
|
|
+ }
|
|
|
@Test
|
|
|
- void contextLoads() {
|
|
|
+ void contextLoads() throws ConnectException {
|
|
|
+ String docPath="/Users/wangrui/Downloads/aaa.doc";
|
|
|
+// String docPath="https://bi.platomix.net/uploads/aaa.doc";
|
|
|
+ String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
|
|
|
+
|
|
|
+// docTopdf(docPath,pdfPath);
|
|
|
+// wordToPdf(docPath,pdfPath);
|
|
|
+// office2PDF(docPath,pdfPath);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * docx、xlsx、转pdf
|
|
|
+ * @param docPath
|
|
|
+ * @param pdfPath
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean docTopdf(String docPath, String pdfPath) {
|
|
|
+// public static boolean docTopdf() {
|
|
|
+
|
|
|
+
|
|
|
+ File inputWord = new File(docPath);
|
|
|
+ File outputFile = new File(pdfPath);
|
|
|
+ try {
|
|
|
+ InputStream docxInputStream = new FileInputStream(inputWord);
|
|
|
+ OutputStream outputStream = new FileOutputStream(outputFile);
|
|
|
+ IConverter converter = LocalConverter.builder().build();
|
|
|
+ String fileTyle=docPath.substring(docPath.lastIndexOf("."),docPath.length());//获取文件类型
|
|
|
+ if(".docx".equals(fileTyle)){
|
|
|
+ converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
+ }else if(".doc".equals(fileTyle)){
|
|
|
+ converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
+ }else if(".xls".equals(fileTyle)){
|
|
|
+ converter.convert(docxInputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
+ }else if(".xlsx".equals(fileTyle)){
|
|
|
+ converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
+ }
|
|
|
+ docxInputStream.close();
|
|
|
+ outputStream.close();
|
|
|
+ inputWord.delete();
|
|
|
+ System.out.println("pdf转换成功");
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+// public static void wordToPdf(String docFile,String pdfFile) throws ConnectException {
|
|
|
+// // 源文件目录
|
|
|
+// File inputFile = new File(docFile);
|
|
|
+// // 输出文件目录
|
|
|
+// File outputFile = new File(pdfFile);
|
|
|
+// if (!outputFile.getParentFile().exists()) {
|
|
|
+// outputFile.getParentFile().exists();
|
|
|
+// }
|
|
|
+// // 连接openoffice服务
|
|
|
+// OpenOfficeConnection connection = new SocketOpenOfficeConnection("10.211.55.4", 8100);
|
|
|
+//// OpenOfficeConnection connection = new SocketOpenOfficeConnection("localhost", 8100);
|
|
|
+//// OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
|
|
|
+// connection.connect();
|
|
|
+// // 转换word到pdf
|
|
|
+// System.out.println(1);
|
|
|
+//// DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
|
|
|
+// OpenOfficeDocumentConverter converter = new OpenOfficeDocumentConverter(connection);
|
|
|
+// converter.convert(inputFile, outputFile);
|
|
|
+// System.out.println(2);
|
|
|
+// // 关闭连接
|
|
|
+// connection.disconnect();
|
|
|
+// }
|
|
|
+ public static boolean office2PDF(String sourceFile, String destFile) {
|
|
|
+// OfficeUrl officeUrl = new OfficeUrl("10.211.55.4:8100");
|
|
|
+// OfficeConnection connection = new OfficeConnection("10.211.55.4", 8100);
|
|
|
+
|
|
|
+ OfficeManager officeManager
|
|
|
+ = LocalOfficeManager.builder().install()
|
|
|
+// .officeHome("C:\\Program Files (x86)\\OpenOffice 4")
|
|
|
+// .officeHome("/Applications/OpenOffice.app/Contents")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ try {
|
|
|
+ File inputFile = new File(sourceFile);
|
|
|
+ if (!inputFile.exists()) {
|
|
|
+ log.info("找不到源文件");
|
|
|
+ return false;// 找不到源文件, 则返回-1
|
|
|
+ }
|
|
|
+ // 如果目标路径不存在, 则新建该路径
|
|
|
+ File outputFile = new File(destFile);
|
|
|
+ if (!outputFile.getParentFile().exists()) {
|
|
|
+ outputFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ officeManager.start(); // Start the office process
|
|
|
+ JodConverter.convert(new File(sourceFile)).to(outputFile).execute();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+ OfficeUtils.stopQuietly(officeManager); // Stop the office process
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|