由于系統之前使用了是itext進行轉換的,現在已經不是開源的工具了,需要收費,然后進行改造,具體處理如下。
<dependency><groupId>org.docx4j</groupId><artifactId>docx4j</artifactId><version>6.1.2</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>xalan</groupId><artifactId>xalan</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-export-fo</artifactId><version>6.0.0</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>xalan</groupId><artifactId>xalan</artifactId></exclusion></exclusions></dependency>
以上排查一下日志,因為和自己的有沖突,所以,自己項目看情況而定。
public static void writeWordModle(String inPath, String outPath, Map<String, String> params,String rootpath) throws Exception {FileInputStream is = null;FileOutputStream targetpdf = null;WordprocessingMLPackage wordMLPackage = null;CustomXWPFDocument doc = null;ByteArrayOutputStream baos = null;InputStream inputStream = null;try {// 加載 Word 文檔File file = new File(inPath);is = new FileInputStream(file);doc = new CustomXWPFDocument(is);// 替換文本和表格中的變量WordUtils wordUtil = new WordUtils();wordUtil.replaceInPara(doc, params); // 替換文本里面的變量wordUtil.replaceInTable(doc, params); // 替換表格里面的變量// 將 XWPFDocument 轉換為字節數組baos = new ByteArrayOutputStream();doc.write(baos);baos.flush();byte[] docBytes = baos.toByteArray();// 使用字節數組創建輸入流inputStream = new ByteArrayInputStream(docBytes);// 將輸入流轉換為 WordprocessingMLPackagewordMLPackage = WordprocessingMLPackage.load(inputStream);// 設置字體映射器IdentityPlusMapper fontMapper = new IdentityPlusMapper();//加載字體fontMapper = setFontMapper(fontMapper);//解決宋體(正文)和宋體(標題)的亂碼問題,以下是在linux的關鍵PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));wordMLPackage.setFontMapper(fontMapper); // 執行轉換targetpdf = new FileOutputStream(outPath);Docx4J.toPDF(wordMLPackage, targetpdf);} catch (Exception e) {e.printStackTrace();throw e;} finally {// 關閉所有流close(is);close(targetpdf);close(baos);close(inputStream);}}
因為我是通過word模版生成了流然后通過流轉換成pdf的,可以使用File docFile = new File(docxPath);替代代碼中的? ?inputStream = new ByteArrayInputStream(docBytes);
//解決宋體(正文)和宋體(標題)的亂碼問題,以下是在linux的關鍵PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));
這句很關鍵,我測試了很久找了很多資料,這個解決了linux環境亂碼的問題。
/** * 字體設置* @param mlPackage* @throws Exception*/private static IdentityPlusMapper setFontMapper( IdentityPlusMapper fontMapper) throws Exception {fontMapper.put("隸書", PhysicalFonts.get("LiSu"));fontMapper.put("宋體", PhysicalFonts.get("SimSun"));fontMapper.put("微軟雅黑", PhysicalFonts.get("Microsoft Yahei"));fontMapper.put("黑體", PhysicalFonts.get("SimHei"));fontMapper.put("楷體", PhysicalFonts.get("KaiTi"));fontMapper.put("新宋體", PhysicalFonts.get("NSimSun"));fontMapper.put("華文行楷", PhysicalFonts.get("STXingkai"));fontMapper.put("華文仿宋", PhysicalFonts.get("STFangsong"));fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));fontMapper.put("幼圓", PhysicalFonts.get("YouYuan"));fontMapper.put("華文宋體", PhysicalFonts.get("STSong"));fontMapper.put("華文中宋", PhysicalFonts.get("STZhongsong"));fontMapper.put("等線", PhysicalFonts.get("SimSun"));fontMapper.put("等線 Light", PhysicalFonts.get("SimSun"));fontMapper.put("華文琥珀", PhysicalFonts.get("STHupo"));fontMapper.put("華文隸書", PhysicalFonts.get("STLiti"));fontMapper.put("華文新魏", PhysicalFonts.get("STXinwei"));fontMapper.put("華文彩云", PhysicalFonts.get("STCaiyun"));fontMapper.put("方正姚體", PhysicalFonts.get("FZYaoti"));fontMapper.put("方正舒體", PhysicalFonts.get("FZShuTi"));fontMapper.put("華文細黑", PhysicalFonts.get("STXihei"));fontMapper.put("宋體擴展",PhysicalFonts.get("simsun-extB"));fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));fontMapper.put("新細明體",PhysicalFonts.get("SimSun"));return fontMapper;}
設置字體,主要是windows使用,其實不設置也沒問題。以上基本解決了代碼的問題。
linux字體環境準備及處理
1、復制windows的C:\Windows\Fonts\下的所有字體庫,放在桌面的fonts文件夾里
2、在linux目錄/usr/share/fonts/chinese 創建文件夾chinese ,如果文件夾不存在,手動創建
3、把fonts拷貝上去,并給他授權chmod -R 755 /usr/share/fonts/chinese
4、# mkfontscale (如果提示 mkfontscale: command not found,需自行安裝 # yum install mkfontscale )
# mkfontdir
# fc-cache -fv (如果提示 fc-cache: command not found,則需要安裝# yum install fontconfig?
5、到路徑vi /etc/fonts/fonts.conf 修改配置,把/usr/share/fonts/chinese添加進去
6、最后執行,fc-cache和fc-list :lang=zh??
/usr/share/fonts/chinese/simsun.ttc: SimSun\-PUA,宋體\-PUA:style=Regular
/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋體:style=Regular
/usr/share/fonts/chinese/simsunb.ttf: SimSun\-ExtB:style=Regular,Normal,oby?ejné,Standard,Κανονικ?,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/chinese/SimsunExtG.ttf: SimSun\-ExtG:style=Regular
/usr/share/fonts/chinese/simsun.ttf: SimSun,宋體:style=Regular
/usr/share/fonts/chinese/simsun.ttc: SimSun,宋體:style=Regular
出現上面的類似即表示成功。