libreoffice转换文件为pdf文件乱码问题解决办法

最近系统需要一个office文件预览功能

解决方案为使用libreoffice将office文件转换为pdf文件,然后使用swftools将pdf文件转换为swf文件

最后在前台使用flexpaper浏览swf文件,即可实现预览

环境搭建完成,转换也没有问题,但是预览效果看到所有中文全部为乱码

下载转换后的pdf文件也是乱码,由此可见时libreoffice转换这一步出现了问题

 

服务器转换文件乱码主要是由于没有中文字体导致的,我在ubuntu desktop系统下使用libreoffice打开office文件
发现中文字体大多默认使用的是 Droid Sans Fallback这个字体
对应字体目录是 /usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf这个文件

将字体文件拷贝到服务器的 /usr/share/fonts/truetype/droid 目录下
然后安装字体
sudo mkfontscale
sudo mkfontdir
再次生成pdf和swf文件已经是中文没有乱码

字体文件

转载于:https://www.cnblogs.com/heimirror/p/3792460.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的FileInputStream和FileOutputStream类来读写文件,然后使用LibreOffice的API将文件转换PDF格式。以下是一个示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.lang.XComponent; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class ConvertToPDF { public static void main(String[] args) { String inputFile = "input.docx"; String outputFile = "output.pdf"; convertToPDF(inputFile, outputFile); } public static void convertToPDF(String inputFile, String outputFile) { try { // initialize LibreOffice API XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xContext)); // load input file String inputURL = new File(inputFile).toURI().toString(); PropertyValue[] propertyValues = new PropertyValue[0]; XComponent xComponent = xLoader.loadComponentFromURL(inputURL, "_blank", 0, propertyValues); // prepare output file String outputURL = new File(outputFile).toURI().toString(); PropertyValue[] convertProperties = new PropertyValue[2]; convertProperties[0] = new PropertyValue(); convertProperties[0].Name = "FilterName"; convertProperties[0].Value = "writer_pdf_Export"; convertProperties[1] = new PropertyValue(); convertProperties[1].Name = "Overwrite"; convertProperties[1].Value = true; // convert to PDF UnoRuntime.queryInterface(XComponent.class, xComponent).storeToURL(outputURL, convertProperties); // close input file xComponent.dispose(); // shutdown LibreOffice API com.sun.star.util.XCloseable xCloseable = UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class, xLoader); xCloseable.close(false); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我们打开一个Word文档,将其转换PDF格式,并将其保存到输出文件中。你需要将input.docx替换为你要转换文件名,将output.pdf替换为你想要保存的PDF文件名。请注意,你需要将LibreOffice的JAR文件添加到项目的类路径中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值