Переглянути джерело

Merge branch 'main' of http://gogs.yaoyi.net/yyc3/reports

# Conflicts:
#	src/main/java/org/example/mybatisplus/Controller/IndexController.java
aQiu6 3 місяців тому
батько
коміт
77160f5612

+ 26 - 0
dynamic_template.ftl

@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <style>
+        table { border-collapse: collapse; width: 50%; }
+        th, td { border: 1px solid black; padding: 8px; }
+    </style>
+</head>
+<body>
+<h1>员工信息表</h1>
+<table>
+    <tr>
+        <th>姓名</th>
+        <td>${name}</td>
+    </tr>
+    <tr>
+        <th>年龄</th>
+        <td>${age}</td>
+    </tr>
+    <tr>
+        <th>职位</th>
+        <td>${title}</td>
+    </tr>
+</table>
+</body>
+</html>


BIN
jijinhui1.docx


+ 59 - 51
pom.xml

@@ -20,34 +20,34 @@
     </properties>
 
     <dependencies>
-            <!-- Docx4J core -->
-            <dependency>
-                <groupId>org.docx4j</groupId>
-                <artifactId>docx4j-JAXB-Internal</artifactId>
-                <version>8.3.14</version>
-                <exclusions>
-                    <exclusion>
-                        <groupId>org.apache.commons</groupId>
-                        <artifactId>commons-compress</artifactId>
-                    </exclusion>
-                </exclusions>
-            </dependency>
-            <dependency>
-                <groupId>org.docx4j</groupId>
-                <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
-                <version>11.4.9</version>
-            </dependency>
-            <dependency>
-                <groupId>org.docx4j</groupId>
-                <artifactId>docx4j-export-fo</artifactId>
-                <version>11.4.9</version>
-            </dependency>
-            <!-- iText for PDF conversion -->
-            <dependency>
-                <groupId>com.itextpdf</groupId>
-                <artifactId>itextpdf</artifactId>
-                <version>5.5.13.3</version>
-            </dependency>
+        <!-- Docx4J core -->
+        <dependency>
+            <groupId>org.docx4j</groupId>
+            <artifactId>docx4j-JAXB-Internal</artifactId>
+            <version>8.3.14</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.commons</groupId>
+                    <artifactId>commons-compress</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.docx4j</groupId>
+            <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
+            <version>11.4.9</version>
+        </dependency>
+        <dependency>
+            <groupId>org.docx4j</groupId>
+            <artifactId>docx4j-export-fo</artifactId>
+            <version>11.4.9</version>
+        </dependency>
+        <!-- iText for PDF conversion -->
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+            <version>5.5.13.3</version>
+        </dependency>
         <!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
         <dependency>
             <groupId>jakarta.xml.bind</groupId>
@@ -110,10 +110,11 @@
 
 
 
+        <!-- Apache POI -->
         <dependency>
             <groupId>org.apache.poi</groupId>
-            <artifactId>poi-ooxml</artifactId>
-            <version>3.15</version>
+            <artifactId>poi</artifactId>
+            <version>5.2.3</version>
             <exclusions>
                 <exclusion>
                     <groupId>org.apache.commons</groupId>
@@ -121,22 +122,6 @@
                 </exclusion>
             </exclusions>
         </dependency>
-        <dependency>
-            <groupId>org.apache.poi</groupId>
-            <artifactId>poi-ooxml-schemas</artifactId>
-            <version>3.15</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.poi</groupId>
-            <artifactId>poi</artifactId>
-            <version>3.15</version>
-        </dependency>
-        <!-- Apache POI -->
-        <dependency>
-            <groupId>org.apache.poi</groupId>
-            <artifactId>poi</artifactId>
-            <version>5.2.3</version>
-        </dependency>
         <!-- poi-scratchpad for PDF conversion -->
         <dependency>
             <groupId>org.apache.poi</groupId>
@@ -147,6 +132,12 @@
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
             <version>5.2.3</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.commons</groupId>
+                    <artifactId>commons-compress</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <!-- 图像处理支持 -->
         <dependency>
@@ -177,11 +168,6 @@
             <artifactId>jackson-databind</artifactId>
             <version>2.18.3</version>
         </dependency>
-        <dependency>
-            <groupId>org.apache.pdfbox</groupId>
-            <artifactId>pdfbox</artifactId>
-            <version>2.0.27</version>
-        </dependency>
         <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
         <dependency>
             <groupId>com.fasterxml.jackson.datatype</groupId>
@@ -353,6 +339,28 @@
                     </excludes>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-enforcer-plugin</artifactId>
+                <version>3.3.0</version>
+                <executions>
+                    <execution>
+                        <id>enforce-versions</id>
+                        <goals>
+                            <goal>enforce</goal>
+                        </goals>
+                        <configuration>
+                            <rules>
+                                <bannedDependencies>
+                                    <excludes>
+                                        <exclude>org.apache.commons:commons-compress:[1.21]</exclude>
+                                    </excludes>
+                                </bannedDependencies>
+                            </rules>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
 </project>

+ 3 - 2
src/main/java/org/example/mybatisplus/Controller/IndexController.java

@@ -26,6 +26,7 @@ import java.io.*;
 @RestController
 public class IndexController {
     public static void main(String[] args) {
+
         String wordFilePath = System.getProperty("user.dir")+File.separator+ "jijinhui.docx";
         String wordExportpath = System.getProperty("user.dir")+File.separator+ "jijinhui1.docx";
         String pdfFilePath =System.getProperty("user.dir")+File.separator+  "download.pdf";
@@ -56,10 +57,10 @@ public class IndexController {
         try {
             JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(report));
             // 生成Word文档
-            //WordPoiExporter.init(wordFilePath,wordExportpath,jsonObject);
+            WordPoiExporter.init(wordFilePath,wordExportpath,jsonObject);
             // 将Word文档转换为PDF
 
-            WordPoiExporter.convert("G:\\Word\\房屋租赁合同书.docx",pdfFilePath);
+           // WordPoiExporter.saveAsPdf("G:\\Word\\房屋租赁合同书.docx",pdfFilePath);
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 5 - 1
src/main/java/org/example/mybatisplus/module/FinancialInfo.java

@@ -13,7 +13,11 @@ import java.util.List;
 @Data
 public class FinancialInfo {
     private Boolean showable;  // 是否显示区域
-    private List<FinancialData> financialData;    // 重要财务数据(4.1)
+    private FinancialData financialData1;    // 重要财务数据(4.1)
+    private FinancialData financialData2;    // 重要财务数据(4.1)
+    private FinancialData financialData3;    // 重要财务数据(4.1)
+    private FinancialData financialData4;    // 重要财务数据(4.1)
+    private String remark;    // 备注
     private List<FinancialIndicator> indicators;  // 财务指标(4.2)
     private FinancialScore financialScore;        // 财务信息评分及建议(4.3)
 }

+ 33 - 16
src/main/java/org/example/mybatisplus/tools/WordPoiExporter.java

@@ -4,6 +4,10 @@ import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import org.apache.commons.lang.StringUtils;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
 import org.apache.poi.xwpf.usermodel.*;
 import org.docx4j.Docx4J;
 import org.docx4j.convert.out.FOSettings;
@@ -21,13 +25,14 @@ public class WordPoiExporter {
 //        String json = inputStreamToString(stream);
 //        JSONObject jsonObject = JSON.parseObject(json);
 
+        String pdfFilePath =System.getProperty("user.dir")+File.separator+  "download.pdf";
         try {
             XWPFDocument doc = processTemplate(source, data);
             FileOutputStream out = new FileOutputStream(path);
 
             doc.write(out);
+            saveAsPdf(doc, pdfFilePath);
             out.close();
-//            saveAsPdf(doc, FxyTool.class.getResource("/output.pdf").getPath());
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -69,7 +74,7 @@ public class WordPoiExporter {
                 }else if(keys.length==3){
                     JSONObject data1=data.getJSONObject(keys[1]);
                     String placeholder = "${" +key + "}";
-                    if (runText.contains(placeholder)) {
+                    if (data1!=null && runText.contains(placeholder)) {
                         // 直接修改Run的文本(保留原格式)
                         String value=data1.getString(keys[2]);
                         if(value==null)value="";
@@ -189,17 +194,26 @@ public class WordPoiExporter {
         }
     }
     public static void convertWordToPdf(String inputFilePath, String outputFilePath)  {
-//        try (FileInputStream fis = new FileInputStream(inputFilePath);
-//             XWPFDocument document = new XWPFDocument(fis);
-//             FileOutputStream fos = new FileOutputStream(outputFilePath)) {
-//
-//            PdfOptions options = PdfOptions.create();
-//            PdfConverter.getInstance().convert(document, fos, options);
-//        } catch (FileNotFoundException e) {
-//            throw new RuntimeException(e);
-//        } catch (IOException e) {
-//            throw new RuntimeException(e);
-//        }
+        try (FileInputStream fis = new FileInputStream(inputFilePath);
+             XWPFDocument wordDoc = new XWPFDocument(fis);
+             PDDocument pdfDoc = new PDDocument()) {
+
+            for (XWPFParagraph paragraph : wordDoc.getParagraphs()) {
+                PDPage page = new PDPage();
+                try (                     PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page)) {
+                    contentStream.beginText();
+                    contentStream.setFont(PDType1Font.HELVETICA, 12);
+                    contentStream.newLineAtOffset(100, 700);
+                    contentStream.showText(paragraph.getText());
+                    contentStream.endText();
+                }
+            }
+
+            pdfDoc.save(outputFilePath);
+            System.out.println("转换完成!");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
     }
     public static void convert(String inputPath, String outputPath) {
         try {
@@ -248,12 +262,13 @@ public class WordPoiExporter {
     }
     private static void saveAsPdf(XWPFDocument doc, String pdfPath) throws Exception {
         // 创建PDF文档
-        /*try (PDDocument pdfDoc = new PDDocument()) {
+        try (PDDocument pdfDoc = new PDDocument()) {
             PDPage page = new PDPage();
             pdfDoc.addPage(page);
 
             // 写入内容
-            try ( PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page)) {
+            try {
+                PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page);
                 contentStream.setFont(PDType1Font.HELVETICA, 12);
                 float margin = 50;
                 float yStart = page.getMediaBox().getHeight() - margin;
@@ -285,7 +300,9 @@ public class WordPoiExporter {
                 // 保存PDF
                 contentStream.close();
                 pdfDoc.save(pdfPath);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
             }
-        }*/
+        }
     }
 }

+ 2 - 0
src/main/resources/docx4j.properties

@@ -0,0 +1,2 @@
+# ??????????????
+docx4j.Convert.Out.FO.Processor=XSLT_FO

+ 0 - 13
src/test/java/org/example/mybatisplus/MybatisplusApplicationTests.java

@@ -1,13 +0,0 @@
-package org.example.mybatisplus;
-
-import org.junit.jupiter.api.Test;
-import org.springframework.boot.test.context.SpringBootTest;
-
-@SpringBootTest
-class MybatisplusApplicationTests {
-
-    @Test
-    void contextLoads() {
-    }
-
-}