世界十大禁养名猫关于Java项⽬⽤asposeword转pdf在Linux环境乱码问题
jar包之前使⽤的是
刘吉桦这是pdf在Linux上显⽰的问题,都是⽅格,开始以为是乱码问题,后来查了⼀下,说是在Linux上没加载到中⽂字体,然后把⽤到的字体包从windows的C:\Windows\Fonts⾥出来,复制到linux的/usr/share/fonts/windows-fonts下然后就是其他博客的那些,刷新字体# fc-cache。不⽣效重启服务器 # reboot。,刷新了⼀下服务器,感觉还是没起效果,于是,⼜开始查,终于到⼀个,说可能是没有读到字体,于是:
package com.shineyoomon.utils.wordtopdf;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
public class Doc2PdfUtil {
private static Logger logger = Logger(Doc2PdfUtil.class);
/**
* doc转pdf
*
一朵玫瑰花代表什么* @param docPath doc⽂件路径,包含.doc
* @param pdfPath pdf⽂件路径,包含.pdf
* @return
*/
慈母情深课件public static File doc2Pdf(String docPath, String pdfPath) {
word打开是乱码System.out.println("pdfPath = "+pdfPath);
System.out.println("pdfPath = "+pdfPath);
File pdfFile = new File(pdfPath);
//判断是否windows系统,Linux要读取字体,否则pdf字体为⽅格
if(!OSinfo.isWindows()){
档案查询/
/这是重点
**DefaultInstance().setFontsFolder(File.separator + "usr"
+ File.separator + "share" + File.separator + "fonts" +File.separator + "Fonts", true);**
}
try {
String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionTy pe>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f 0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAg KXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>"; ByteArrayInputStream is = new Bytes());
License license = new License();
license.setLicense(is);
Document document = new Document(docPath);
FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
System.out.println("pdf⽂件:"+fileOutputStream);
document.save(fileOutputStream, SaveFormat.PDF);
} catch (Exception e) {
logger.info("****aspose doc转pdf异常");
e.printStackTrace();
}
return pdfFile;
}
}
将DefaultInstance().setFontsFolder(File.separator + “usr”
+ File.separator + “share” + File.separator + “fonts” +File.separator + “Fonts”, true);这段放到读取⽂件之前,就可以了,怎么样,很简单吧!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论