python office转pdf linux_记录libreoffice实现office转pdf(适用于windows、linux)

由于目前的工作跟office打交道比较多,所以才有了此篇blog,需求是实现word转换pdf方便页面展示。之前lz采用的是jacob(仅支持windows)进行转换的,但是现在服务器改成linux显然不能用了,于是网上搜罗一圈,最终决定采用LibreOffice。(前提:需要安装jdk环境)

LibreOffice中文官网:https://zh-cn.libreoffice.org/   下载合适的版本,本文下载的是6.1.6

已上传百度网盘(链接: https://pan.baidu.com/s/1hS-GUT5yXaDgFDMWq3mjXQ 提取码: 1e9z)

一:windows下实现office转pdf

安装:直接一键默认安装

环境变量:在path前加入libreoffice安装路径(如:D:\Program Files\LibreOffice\program)

进入dos窗口输入soffice 如果弹出libreoffice界面则表示安装成功

java程序实现转换操作(原理通过cmd调用libreoffice指令)

/**

* 利用libreOffice将office文档转换成pdf

* @param inputFile 目标文件地址

* @param pdfFile 输出文件夹

* @return

*/

public static boolean convertOffice2PDF(String inputFile, String pdfFile){

long start = System.currentTimeMillis();

String command;

boolean flag;

String osName = System.getProperty("os.name");

if (osName.contains("Windows")) {

command = "cmd /c start soffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;

}else {

command = "libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;

}

flag = executeLibreOfficeCommand(command);

long end = System.currentTimeMillis();

logger.debug("用时:{} ms", end - start);

return flag;

}

/**

* 执行command指令

* @param command

* @return

*/

public static boolean executeLibreOfficeCommand(String command) {

logger.info("开始进行转化.......");

Process process;// Process可以控制该子进程的执行或获取该子进程的信息

try {

logger.debug("convertOffice2PDF cmd : {}", command);

process = Runtime.getRuntime().exec(command);// exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。

// 下面两个可以获取输入输出流

// InputStream errorStream = process.getErrorStream();

// InputStream inputStream = process.getInputStream();

} catch (IOException e) {

logger.error(" convertOffice2PDF {} error", command, e);

return false;

}

int exitStatus = 0;

try {

exitStatus = process.waitFor();// 等待子进程完成再往下执行,返回值是子线程执行完毕的返回值,返回0表示正常结束

// 第二种接受返回值的方法

int i = process.exitValue(); // 接收执行完毕的返回值

logger.debug("i----" + i);

} catch (InterruptedException e) {

logger.error("InterruptedException convertOffice2PDF {}", command, e);

return false;

}

if (exitStatus != 0) {

logger.error("convertOffice2PDF cmd exitStatus {}", exitStatus);

} else {

logger.debug("convertOffice2PDF cmd exitStatus {}", exitStatus);

}

process.destroy(); // 销毁子进程

logger.info("转化结束.......");

return true;

}

二:Linux下实现office转pdf

安装:把下载下来的三个安装包上传到linux,采用  tar -xvf xxxxxx.tar.gz解压即可

然后进入RPMS包下,采用yum localinstall *.rpm安装rpm文件

测试是否安装成功:libreoffice6.1 -help

为了使用libreoffice创建别名

[root@VM]# alias libreoffice='libreoffice6.0'

[root@VM]# alias

alias cp='cp -i'

alias egrep='egrep --color=auto'

alias fgrep='fgrep --color=auto'

alias grep='grep --color=auto'

alias l.='ls -d .* --color=auto'

alias libreoffice='libreoffice6.0'

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

linux下面命令行测试word转pdf(其参数与windows下的参数大体相同)

命令:libreoffice --convert-to pdf:writer_pdf_Export /usr/lib/files/白头拟稿纸.doc --outdir /usr/lib/files/

关于word转pdf中文乱码问题处理

1:查看fonts目录:cat /etc/fonts/fonts.conf | grep fon

得知字体存放位置:/usr/share/fonts

2: 把Windows下的字体C:\Windows\Fonts下的宋体,即simsun.ttc上传到linux服务器

在fonts下新建Fonts文件 把字体上传到该路径下即可

可以使用LibreOffice的Java API来实现LibreOffice文档换为PDF文档。以下是实现的步骤: 1. 首先需要确保LibreOffice已经安装在系统中,并且已经配置好了环境变量。同时需要下载并安装LibreOffice的Java API。 2. 在Java程序中引入LibreOffice的Java API所在的jar包。 3. 使用以下代码将文档换为PDF文档: ``` 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) { XComponentContext xContext = null; try { //获取LibreOffice的上下文 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xContext)); //设置待换文档的路径和文件名 String fileToConvert = "file:///C:/example.docx"; //设置换后的PDF文档的路径和文件名 String pdfFile = "file:///C:/example.pdf"; //设置换参数 PropertyValue[] conversionProperties = new PropertyValue[2]; conversionProperties[0] = new PropertyValue(); conversionProperties[0].Name = "Hidden"; conversionProperties[0].Value = Boolean.TRUE; conversionProperties[1] = new PropertyValue(); conversionProperties[1].Name = "FilterName"; conversionProperties[1].Value = "writer_pdf_Export"; //打开待换文档 XComponent xComponent = xLoader.loadComponentFromURL(fileToConvert, "_blank", 0, conversionProperties); //将文档换为PDF格式 PropertyValue[] storeProperties = new PropertyValue[3]; storeProperties[0] = new PropertyValue(); storeProperties[0].Name = "FilterName"; storeProperties[0].Value = "writer_pdf_Export"; storeProperties[1] = new PropertyValue(); storeProperties[1].Name = "Overwrite"; storeProperties[1].Value = Boolean.TRUE; storeProperties[2] = new PropertyValue(); storeProperties[2].Name = "Hidden"; storeProperties[2].Value = Boolean.TRUE; UnoRuntime.queryInterface(XStorable.class, xComponent).storeToURL(pdfFile, storeProperties); //关闭文档 UnoRuntime.queryInterface(XCloseable.class, xComponent).close(Boolean.TRUE); } catch (Exception e) { e.printStackTrace(); } finally { if (xContext != null) { com.sun.star.uno.Runtime.getRuntime(xContext).freeUnusedLibraries(); } } } } ``` 4. 运行程序即可将LibreOffice文档换为PDF文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值