Explorar o código

构建脚本参考

wangrui hai 3 meses
pai
achega
bc526be9b6
Modificáronse 2 ficheiros con 173 adicións e 1 borrados
  1. 54 0
      server/pom.xml
  2. 119 1
      server/src/test/java/org/springblade/ServerMainTests.java

+ 54 - 0
server/pom.xml

@@ -328,6 +328,60 @@
             </exclusions>
         </dependency>
         <!-- test dependency - end -->
+
+        <!--转pdf-->
+        <dependency>
+            <groupId>com.documents4j</groupId>
+            <artifactId>documents4j-local</artifactId>
+            <version>1.0.3</version>
+        </dependency>
+        <dependency>
+            <groupId>com.documents4j</groupId>
+            <artifactId>documents4j-transformer-msoffice-word</artifactId>
+            <version>1.0.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+            <version>5.5.10</version>
+        </dependency>
+        <!-- test   - end -->
+        <dependency>
+            <groupId>org.apache.directory.studio</groupId>
+            <artifactId>org.apache.commons.io</artifactId>
+            <version>2.4</version>
+        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>com.artofsolving</groupId>-->
+<!--            <artifactId>jodconverter</artifactId>-->
+<!--            <version>2.2.1</version>-->
+<!--        </dependency>-->
+        <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local -->
+        <dependency>
+            <groupId>org.jodconverter</groupId>
+            <artifactId>jodconverter-local</artifactId>
+            <version>4.4.8</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.openoffice</groupId>
+            <artifactId>juh</artifactId>
+            <version>3.1.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.openoffice</groupId>
+            <artifactId>unoil</artifactId>
+            <version>3.0.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+
     </dependencies>
 
     <build>

+ 119 - 1
server/src/test/java/org/springblade/ServerMainTests.java

@@ -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;
 	}
 }