Browse Source

simhei字体

yuedefeng 7 years ago
parent
commit
3f719e1013
3 changed files with 17 additions and 5 deletions
  1. 1 0
      README.md
  2. 7 0
      pom.xml
  3. 9 5
      src/main/java/com/common/workflow/service/util/ExcelToPdf.java

+ 1 - 0
README.md

@@ -95,3 +95,4 @@ docker中中文字体乱码的问题(添加宋体):
 
 COPY simsun.ttf /usr/share/fonts
 
+mvn install:install-file -Dfile=aspose-cells-8.5.2.jar -DgroupId=com.aspose -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jar

+ 7 - 0
pom.xml

@@ -373,6 +373,13 @@
             <artifactId>activiti-spring-boot-starter-actuator</artifactId>
             <version>${activiti.version}</version>
         </dependency>
+        <!-- https://mvnrepository.com/artifact/com.aspose/aspose-cells -->
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-cells</artifactId>
+            <version>8.5.2</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 9 - 5
src/main/java/com/common/workflow/service/util/ExcelToPdf.java

@@ -6,11 +6,9 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.Locale;
 
-import com.aspose.cells.License;
-import com.aspose.cells.PdfSaveOptions;
-import com.aspose.cells.SaveFormat;
-import com.aspose.cells.Workbook;
+import com.aspose.cells.*;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -41,12 +39,18 @@ public class ExcelToPdf {
             HttpURLConnection conn = (HttpURLConnection)url.openConnection();
             //得到输入流
             InputStream inputStream = conn.getInputStream();
+
+            //TxtLoadOptions lo = new TxtLoadOptions();
+            //lo.setEncoding(Encoding.getUTF8());
             Workbook wb = new Workbook(inputStream);// 原始excel路径
 
-            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
+            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(SaveFormat.PDF);
             pdfSaveOptions.setOnePagePerSheet(true);
             pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
+            pdfSaveOptions.setDefaultFont("SimSun");
+            pdfSaveOptions.setEmbedStandardWindowsFonts(true);
             response.setContentType("application/pdf");
+            response.setCharacterEncoding("UTF-8");
             OutputStream output = response.getOutputStream();
             wb.save(output, pdfSaveOptions);
             output.flush();