Browse Source

fix 新增中文字体显示

DYH2020 2 years ago
parent
commit
574119ed8e

BIN
config_file/simsun.ttc


+ 8 - 14
src/main/java/com/ywt/mg/core/utils/GeneratePdf.java

@@ -10,9 +10,7 @@ package com.ywt.mg.core.utils;
 import com.itextpdf.text.*;
 import com.itextpdf.text.Font;
 import com.itextpdf.text.Image;
-import com.itextpdf.text.pdf.ColumnText;
-import com.itextpdf.text.pdf.PdfContentByte;
-import com.itextpdf.text.pdf.PdfWriter;
+import com.itextpdf.text.pdf.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -79,11 +77,7 @@ public class GeneratePdf {
             Phrase phrase = new Phrase(content);
             doc.add(phrase);
             doc.close();
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (DocumentException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
@@ -101,7 +95,11 @@ public class GeneratePdf {
             PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
             doc.open();
             doc.newPage();
-            Phrase phrase = new Phrase(content);
+            // 使用宋体显示中文
+            String songtiFontName = "./config_file/simsun.ttc";
+            songtiFontName += ",1";
+            Font songtiFont = new Font(BaseFont.createFont(songtiFontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));//中文简体
+            Phrase phrase = new Phrase(content, songtiFont);
             doc.add(phrase);
             for (String imagePath : imagePathList) {
                 doc.newPage();
@@ -115,11 +113,7 @@ public class GeneratePdf {
                 doc.add(png1);
             }
             doc.close();
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (DocumentException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }

+ 2 - 2
src/main/java/com/ywt/mg/services/enterprise/ApplyProjectService.java

@@ -287,8 +287,8 @@ public class ApplyProjectService {
             logger.error("ApplyProjectService#batchDownloadFiles(request:{}){}", JsonSerializer.toJson(request), e.getMessage(), e);
         } finally {
             // 5、删掉之前所有的文件;
-            FileUtil.deleteFolders(rootPrefix);
-            FileUtil.deleteFolders(filePrefix);
+//            FileUtil.deleteFolders(rootPrefix);
+//            FileUtil.deleteFolders(filePrefix);
         }
     }