wangrui 3 ay önce
ebeveyn
işleme
f02192021c

+ 7 - 3
docker/kkfileview-jdk/docker build.txt

@@ -4,10 +4,14 @@ docker build --tag keking/kkfileview-jdk:4.3.0 .
 # arm64架构执行如下命令
 docker build -f Dockerfile_arm64 --tag keking/kkfileview-jdk:4.3.0 .
 
-#项目推送 当前版本 4.3.6-2024.07.24
+#项目推送 当前版本 4.3.7-2024.10.12
 
 cd /Users/wangrui/boyuan/beijing_boyuan/bi/file-online-preview
 mvn clean package -Dmaven.test.skip=true
-docker build -t docker.platomix.net:10000/module/fileview-app:4.3.5-2024.07.22 .
+docker build -t docker.platomix.net:10000/module/fileview-app:4.3.7-2024.10.12 .
 
-docker push docker.platomix.net:10000/module/fileview-app:4.3.5-2024.07.22
+docker push docker.platomix.net:10000/module/fileview-app:4.3.7-2024.10.12
+
+docker run -p 8012:8012 docker.platomix.net:10000/module/fileview-app:4.3.7-2024.10.12
+
+docker run -p 8012:8012 -v /Users/wangrui/Downloads/aa:/dockerDir docker.platomix.net:10000/module/fileview-app:4.3.7-2024.10.12

+ 0 - 46
server/pom.xml

@@ -329,52 +329,6 @@
         </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>

+ 32 - 3
server/src/main/java/org/springblade/controller/OnlinePreviewController.java

@@ -4,6 +4,14 @@ import cn.hutool.core.date.DateUtil;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson2.JSON;
+import com.google.common.collect.Maps;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.jodconverter.core.office.OfficeException;
+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.springblade.core.constant.BladeConstant;
 import org.springblade.core.secure.BladeUser;
 import org.springblade.core.secure.utils.AuthUtil;
@@ -14,8 +22,10 @@ import org.springblade.model.FileAttribute;
 import org.springblade.service.FileHandlerService;
 import org.springblade.service.FilePreview;
 import org.springblade.service.FilePreviewFactory;
+import org.springblade.service.OfficeToPdfService;
 import org.springblade.service.cache.CacheService;
 import org.springblade.service.impl.OtherFilePreviewImpl;
+import org.springblade.utils.AddWatermarkUtil;
 import org.springblade.utils.KkFileUtils;
 import org.springblade.utils.WebUtils;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -41,14 +51,14 @@ import org.springframework.web.client.RestTemplate;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.xml.crypto.Data;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.URL;
 import java.util.*;
 
 /**
  * @author yudian-it
  */
+@Slf4j
 @Controller
 public class OnlinePreviewController {
 
@@ -59,15 +69,17 @@ public class OnlinePreviewController {
     private final CacheService cacheService;
     private final FileHandlerService fileHandlerService;
     private final OtherFilePreviewImpl otherFilePreview;
+    private final OfficeToPdfService officeToPdfService;
     private static final RestTemplate restTemplate = new RestTemplate();
     private static final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
     private static final ObjectMapper mapper = new ObjectMapper();
 
-    public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, OtherFilePreviewImpl otherFilePreview) {
+    public OnlinePreviewController(OfficeToPdfService officeToPdfService,FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, OtherFilePreviewImpl otherFilePreview) {
         this.previewFactory = filePreviewFactory;
         this.fileHandlerService = fileHandlerService;
         this.cacheService = cacheService;
         this.otherFilePreview = otherFilePreview;
+        this.officeToPdfService = officeToPdfService;
     }
 
     /**
@@ -120,6 +132,23 @@ public class OnlinePreviewController {
         logger.info("onlinePreview预览文件map=" + JSONUtil.toJsonStr(map));
         return filePreview.filePreviewHandle(fileUrl, model, fileAttribute);
     }
+    @SneakyThrows
+    @GetMapping("/office2PDF2")
+    public Map office2PDF2(String sourceFile, String destFile, HttpServletRequest req)  {
+
+        HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
+
+        FileAttribute fileAttribute = fileHandlerService.getFileAttribute(sourceFile, req);
+        officeToPdfService.openOfficeToPDF(sourceFile, destFile, fileAttribute);
+
+        File outputFile = new File(destFile);
+        InputStream docxInputStream = new FileInputStream(outputFile);
+        byte[] watermarks = AddWatermarkUtil.addWatermark(docxInputStream, "watermark");
+        AddWatermarkUtil.writePdfToFile(watermarks, destFile);
+        objectObjectHashMap.put("destFile",destFile);
+        return objectObjectHashMap;
+    }
+
     /**
      * @param url
      * @param model

+ 69 - 0
server/src/main/java/org/springblade/utils/AddWatermarkUtil.java

@@ -0,0 +1,69 @@
+package org.springblade.utils;
+
+import com.itextpdf.text.BaseColor;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Element;
+import com.itextpdf.text.pdf.BaseFont;
+import com.itextpdf.text.pdf.PdfContentByte;
+import com.itextpdf.text.pdf.PdfReader;
+import com.itextpdf.text.pdf.PdfStamper;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author 喜欢番茄火锅
+ * @date 2024/10/12 11:30
+ */
+public class AddWatermarkUtil {
+    public static byte[] addWatermark(InputStream inputStream, String watermark) throws IOException, DocumentException {
+
+        PdfReader reader = new PdfReader(inputStream);
+        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
+            PdfStamper stamper = new PdfStamper(reader, os);
+            int total = reader.getNumberOfPages() + 1;
+            PdfContentByte content;
+            // 设置字体
+//			BaseFont baseFont = BaseFont.createFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
+            BaseFont baseFont = BaseFont.createFont();
+            // 循环对每页插入水印
+            for (int i = 1; i < total; i++) {
+                // 水印的起始
+                content = stamper.getUnderContent(i);
+                // 开始
+                content.beginText();
+                // 设置颜色
+                content.setColorFill(new BaseColor(244, 244, 244));
+                // 设置字体及字号
+                content.setFontAndSize(baseFont, 50);
+                // 设置起始位置
+                content.setTextMatrix(400, 780);
+                for (int x = 0; x < 5; x++) {
+                    for (int y = 0; y < 5; y++) {
+                        content.showTextAlignedKerned(Element.ALIGN_CENTER,
+                                watermark,
+                                (100f + x * 350),
+                                (40.0f + y * 150),
+                                30);
+                    }
+                }
+                content.endText();
+            }
+            stamper.close();
+            return os.toByteArray();
+        } finally {
+            reader.close();
+        }
+    }
+    public static void writePdfToFile(byte[] pdfBytes, String filePath) {
+        try (FileOutputStream fos = new FileOutputStream(filePath)) {
+            fos.write(pdfBytes);
+            System.out.println("PDF 文件已成功写入: " + filePath);
+        } catch (IOException e) {
+            e.printStackTrace();
+            System.err.println("无法写入 PDF 文件: " + e.getMessage());
+        }
+    }
+}

+ 69 - 62
server/src/test/java/org/springblade/ServerMainTests.java

@@ -1,9 +1,13 @@
 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 com.itextpdf.text.BaseColor;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Element;
+import com.itextpdf.text.pdf.BaseFont;
+import com.itextpdf.text.pdf.PdfContentByte;
+import com.itextpdf.text.pdf.PdfReader;
+import com.itextpdf.text.pdf.PdfStamper;
 import lombok.extern.slf4j.Slf4j;
 import org.jodconverter.core.DocumentConverter;
 import org.jodconverter.core.office.OfficeManager;
@@ -19,83 +23,42 @@ import java.net.ConnectException;
 @Slf4j
 @SpringBootTest
 class ServerMainTests {
-	public static void main(String[] args) throws ConnectException {
+	public static void main(String[] args) throws IOException, DocumentException {
 		String docPath="/Users/wangrui/Downloads/aaa.doc";
 //		String docPath="https://bi.platomix.net/uploads/aaa.doc";
 		String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
-
+		String pdfPath2="/Users/wangrui/Downloads/aaa2.pdf";
+		File outputFile = new File(pdfPath);
+		InputStream docxInputStream = new FileInputStream(outputFile);
 //		docTopdf(docPath,pdfPath);
 //		wordToPdf(docPath,pdfPath);
-		office2PDF(docPath,pdfPath);
+//		office2PDF(pdfPath,"zhangsan");
+		byte[] zhangs123s = addWatermark(docxInputStream, "zhangs123");
+		writePdfToFile(zhangs123s,pdfPath2);
 	}
 	@Test
-	void contextLoads() throws ConnectException {
+	void contextLoads() throws IOException, DocumentException {
 		String docPath="/Users/wangrui/Downloads/aaa.doc";
 //		String docPath="https://bi.platomix.net/uploads/aaa.doc";
 		String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
-
+		File outputFile = new File(pdfPath);
+		InputStream docxInputStream = new FileInputStream(outputFile);
 //		docTopdf(docPath,pdfPath);
 //		wordToPdf(docPath,pdfPath);
 //		office2PDF(docPath,pdfPath);
+		addWatermark(docxInputStream,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) {
+	public static void writePdfToFile(byte[] pdfBytes, String filePath) {
+		try (FileOutputStream fos = new FileOutputStream(filePath)) {
+			fos.write(pdfBytes);
+			System.out.println("PDF 文件已成功写入: " + filePath);
+		} catch (IOException e) {
 			e.printStackTrace();
-			return false;
+			System.err.println("无法写入 PDF 文件: " + e.getMessage());
 		}
 	}
-//	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);
@@ -126,4 +89,48 @@ class ServerMainTests {
 		}
 		return true;
 	}
+
+	public static byte[] addWatermark(InputStream inputStream, String watermark) throws IOException, DocumentException {
+
+		PdfReader reader = new PdfReader(inputStream);
+		try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
+			PdfStamper stamper = new PdfStamper(reader, os);
+			int total = reader.getNumberOfPages() + 1;
+			PdfContentByte content;
+			// 设置字体
+//			BaseFont baseFont = BaseFont.createFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
+			BaseFont baseFont = BaseFont.createFont();
+			// 循环对每页插入水印
+			for (int i = 1; i < total; i++) {
+				// 水印的起始
+				content = stamper.getUnderContent(i);
+				// 开始
+				content.beginText();
+				// 设置颜色
+				content.setColorFill(new BaseColor(244, 244, 244));
+				// 设置字体及字号
+				content.setFontAndSize(baseFont, 50);
+				// 设置起始位置
+				content.setTextMatrix(400, 780);
+				for (int x = 0; x < 5; x++) {
+					for (int y = 0; y < 5; y++) {
+						content.showTextAlignedKerned(Element.ALIGN_CENTER,
+								watermark,
+								(100f + x * 350),
+								(40.0f + y * 150),
+								30);
+					}
+				}
+				content.endText();
+			}
+			stamper.close();
+			return os.toByteArray();
+		}finally {
+			reader.close();
+		}
+
+	}
+
+
+
 }

+ 143 - 0
server/src/test/java/org/springblade/ServerMainTests2.java

@@ -0,0 +1,143 @@
+package org.springblade;
+
+import com.aspose.cad.internal.gl.S;
+import com.itextpdf.text.BaseColor;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Element;
+import com.itextpdf.text.pdf.BaseFont;
+import com.itextpdf.text.pdf.PdfContentByte;
+import com.itextpdf.text.pdf.PdfReader;
+import com.itextpdf.text.pdf.PdfStamper;
+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 ServerMainTests2 {
+	public static void main(String[] args) throws ConnectException {
+//		String docPath="C:\\Users\\wangrui\\aaa.doc";
+		//		String pdfPath="C:\\Users\\wangrui\\aaa.pdf";
+
+//		String docPath="https://bi.platomix.net/uploads/aaa.doc";
+		String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
+		String docPath="/Users/wangrui/Downloads/aaa.doc";
+
+//		docTopdf(docPath,pdfPath);
+//		wordToPdf(docPath,pdfPath);
+		office2PDF(docPath,pdfPath);
+	}
+	@Test
+	void contextLoads() throws ConnectException {
+		String docPath="C:\\Program Files (x86)\\aaa.doc";
+//		String docPath="https://bi.platomix.net/uploads/aaa.doc";
+		String pdfPath="C:\\Program Files (x86)\\aaa.pdf";
+
+//		docTopdf(docPath,pdfPath);
+//		wordToPdf(docPath,pdfPath);
+//		office2PDF(docPath,pdfPath);
+	}
+
+//	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;
+	}
+	public byte[] addWatermark(InputStream inputStream,String watermark) throws IOException, DocumentException {
+
+		PdfReader reader = new PdfReader(inputStream);
+		try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
+			PdfStamper stamper = new PdfStamper(reader, os);
+			int total = reader.getNumberOfPages() + 1;
+			PdfContentByte content;
+			// 设置字体
+			BaseFont baseFont = BaseFont.createFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
+			// 循环对每页插入水印
+			for (int i = 1; i < total; i++) {
+				// 水印的起始
+				content = stamper.getUnderContent(i);
+				// 开始
+				content.beginText();
+				// 设置颜色
+				content.setColorFill(new BaseColor(244, 244, 244));
+				// 设置字体及字号
+				content.setFontAndSize(baseFont, 50);
+				// 设置起始位置
+				content.setTextMatrix(400, 780);
+				for (int x = 0; x < 5; x++) {
+					for (int y = 0; y < 5; y++) {
+						content.showTextAlignedKerned(Element.ALIGN_CENTER,
+								watermark,
+								(100f + x * 350),
+								(40.0f + y * 150),
+								30);
+					}
+				}
+				content.endText();
+			}
+			stamper.close();
+			return os.toByteArray();
+		}finally {
+			reader.close();
+		}
+
+	}
+
+
+
+}