【文件处理】【libreoffice】office文件转为pdf

官网链接:官网:​​​​​​Linux 下的安装方法 | LibreOffice 简体中文官方网站 - 自由免费的办公套件

1、 libreoffice安装

  • 1.1 在ubuntu下安装

安装libreoffice

sudo apt-get install libreoffice

设置中文界面

sudo apt-get install libreoffice-l10n-zh-cn libreoffice-help-zh-cn

  • 1.2 在centos下安装

Centos下的LibreOffice安装_第一天-CSDN博客_centos安装libreoffice

python使用libreoffice将word转换为pdf - SOARING-SUN - 博客园

2、LibreOffice完美解决中文字体乱码问题

参考:LibreOffice完美解决中文字体问题(在黑暗中摸索了好久~) - whist - 博客园

 在安装完Ubuntu12.04后,发现一个问题,用libreoffice打开任何任何文本文件,只要是中文的字体都显示乱码,网上查了一下,说是文字编码问题,window的默认字体是GBK,而 linux的字体是UTF-8,后来按照网上说的做了一遍后,问题依旧,后来才发现我一开始就走了弯路。其实不是编码问题,而是字体问题,linux中由于版权问题,没有安装windows中的默认字体simsum.ttf,所以不能显示,所以在这里,提醒一下ubuntu的fan们,遇到这个问题可能是字体问题。下面是解决这个问题的方法。
 

1. 在Windows上面找到需要的字体

首先,在Windows的字体文件夹(C:\Windows\Fonts)里面找到需要的字体,一般中文为:楷体,宋体黑体仿宋微软雅黑,英文为:Times New Roman
找到之后将这些字体copy到新的文件夹里面,这样有两个好处,一是文件名变成了英文(上传到linux机器上不会出现乱码),二是方便上传。

2. 利用FileZilla上传把这些字体上传到Linux上的/usr/share/fonts/Fonts这个目录

3.终于成功了!激动得泪流满面~~~

Libreoffice自动读取并识别/usr/share/fonts/Fonts里面的字体。
打一些字测试一下:

3、代码示例

import os
 
import_file_name = "/test/seo.docx"
output_file_path = "/test/"
os.system("libreoffice6.4 --headless --convert-to pdf %s --outdir %s" % (import_file_name, output_file_path))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的Uno API来实现LibreOfficeOffice文档转换为PDF的功能。具体实现步骤如下: 1. 首先需要安装LibreOffice,并启动LibreOffice服务。启动命令为: ``` soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" ``` 2. 在Java中使用Uno API连接LibreOffice服务,代码示例如下: ``` XComponentContext xContext = Bootstrap.bootstrap(); XMultiComponentFactory xMCF = xContext.getServiceManager(); Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); ``` 3. 加载需要转换的Office文档,代码示例如下: ``` String inputUrl = "file:///path/to/input.docx"; PropertyValue[] loadProps = new PropertyValue[1]; loadProps[0] = new PropertyValue(); loadProps[0].Name = "Hidden"; loadProps[0].Value = Boolean.TRUE; XComponent xDoc = xCompLoader.loadComponentFromURL(inputUrl, "_blank", 0, loadProps); ``` 4. 将文档转换为PDF格式,代码示例如下: ``` String outputUrl = "file:///path/to/output.pdf"; PropertyValue[] convertProps = new PropertyValue[2]; convertProps[0] = new PropertyValue(); convertProps[0].Name = "FilterName"; convertProps[0].Value = "writer_pdf_Export"; convertProps[1] = new PropertyValue(); convertProps[1].Name = "Overwrite"; convertProps[1].Value = Boolean.TRUE; XStorable xStore = UnoRuntime.queryInterface(XStorable.class, xDoc); xStore.storeToURL(outputUrl, convertProps); ``` 5. 最后需要关闭文档和LibreOffice服务,代码示例如下: ``` xDoc.dispose(); System.exit(0); ``` 完整的代码示例如下: ``` import com.sun.star.comp.helper.Bootstrap; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.beans.PropertyValue; public class OfficeToPdfConverter { public static void main(String[] args) { try { // Connect to LibreOffice service XComponentContext xContext = Bootstrap.bootstrap(); XMultiComponentFactory xMCF = xContext.getServiceManager(); Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); // Load input document String inputUrl = "file:///path/to/input.docx"; PropertyValue[] loadProps = new PropertyValue[1]; loadProps[0] = new PropertyValue(); loadProps[0].Name = "Hidden"; loadProps[0].Value = Boolean.TRUE; XComponent xDoc = xCompLoader.loadComponentFromURL(inputUrl, "_blank", 0, loadProps); // Convert document to PDF String outputUrl = "file:///path/to/output.pdf"; PropertyValue[] convertProps = new PropertyValue[2]; convertProps[0] = new PropertyValue(); convertProps[0].Name = "FilterName"; convertProps[0].Value = "writer_pdf_Export"; convertProps[1] = new PropertyValue(); convertProps[1].Name = "Overwrite"; convertProps[1].Value = Boolean.TRUE; XStorable xStore = UnoRuntime.queryInterface(XStorable.class, xDoc); xStore.storeToURL(outputUrl, convertProps); // Close document and exit xDoc.dispose(); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } } ``` 需要注意的是,在使用Uno API连接LibreOffice服务时,需要在classpath中加入相应的jar包。具体jar包名称和路径可以参考LibreOffice的安装目录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值