Centos7下使用LibreOffice转PDF
LibreOffice简介
LibreOffice 是一款功能强大的办公软件,默认使用开放文档格式 (OpenDocument Format , ODF), 并支持 *.docx, *.xlsx, *.pptx 等其他格式。
它包含了 Writer, Calc, Impress, Draw, Base 以及 Math 等组件,可用于处理文本文档、电子表格、演示文稿、绘图以及公式编辑。
它可以运行于 Windows, GNU/Linux 以及 macOS 等操作系统上,并具有一致的用户体验
centos7 安装libreoffice
yum -y install libreoffice libreoffice-headless libreoffice-writer libreoffice-impress libreoffice-calc libreoffice-draw libreoffice-langpack-zh-Hans
#查看安装版本
$soffice --version
LibreOffice 5.3.6.1 30(Build:1)
libreoffice --version
LibreOffice 5.3.6.1 30(Build:1)
libreoffice 或soffice使用
不会用的话可以使用soffice –help看一下帮助,非常多的参数和使用案例,转换格式就很简单:
soffice --headless --convert-to docx /opt/upload/source/123.doc --outdir /opt/upload/source
以上的命令就是将/opt/upload/source/123.doc
文件转换成docx格式,输出到/opt/upload/source
文件夹里。
默认情况下:
- 会使用源文件名+新的扩展名保存输出文件;
- 会覆盖outdir里已经有的同名文件;
- 转换成功会输出类似这样的:
convert /opt/upload/source/123.doc -> /opt/upload/source/123.docx using filter : MS Word 2007 XML
Overwriting: /opt/upload/source/123.docx
LibreOffice会根据文件格式自动匹配格式过滤器(filter),至于它支持哪些格式,可以参考一下官网。
示例
java程序实现word转pdf(原理是通过cmd调用上述命令)
#java程序实现word转pdf(原理是通过cmd调用上述命令)
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Test {
private static final Logger logger = LoggerFactory.getLogger(Test.class);
public static void main(String[] args) throws NullPointerException {
long start = System.currentTimeMillis();
String srcPath = "C:/Users/liqiang/Desktop/ww/tt.docx", desPath = "C:/Users/liqiang/Desktop/ww";
String command = "";
String osName = System.getProperty("os.name");
if (osName.contains("Windows")) {
command = "soffice --headless --convert-to pdf " + srcPath + " --outdir " + desPath;
exec(command);
}
long end = System.currentTimeMillis();
logger.debug("用时:{} ms", end - start);
}
public static boolean exec(String command) {
Process process;// Process可以控制该子进程的执行或获取该子进程的信息
try {
logger.debug("exec cmd : {}", command);
process = Runtime.getRuntime().exec(command);// exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。
// 下面两个可以获取输入输出流
InputStream errorStream = process.getErrorStream();
InputStream inputStream = process.getInputStream();
} catch (IOException e) {
logger.error(" exec {} 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 exec {}", command, e);
return false;
}
if (exitStatus != 0) {
logger.error("exec cmd exitStatus {}", exitStatus);
} else {
logger.debug("exec cmd exitStatus {}", exitStatus);
}
process.destroy(); // 销毁子进程
process = null;
return true;
}
}
python程序实现word转pdf(原理是通过cmd调用上述命令)
#python程序实现word转pdf(原理是通过cmd调用上述命令)
def doc2pdf_linux(docPath, pdfPath):
"""
convert a doc/docx document to pdf format (linux only, requires libreoffice)
:param doc: path to document
"""
cmd = 'libreoffice --headless --convert-to pdf'.split() + [docPath] + ['--outdir'] + [os.path.dirname(pdfPath)
]
p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p.wait(timeout=30)
stdout, stderr = p.communicate()
if stderr:
raise subprocess.SubprocessError(stderr)
常见问题
1、中文乱码
#查看操作系统是否支持中文字体
fd-list
#安装字体支持
yum -y install cups-libs fontconfig
#在/usr/share目录就可以看到fonts和fontconfig目录了
ls /usr/share |grep font
添加中文字体,在CentOS中,字体库的存放位置正是上图中看到的fonts目录,所以我们首先要做的就是找到中文字体文件放到该目录下,而中文字体文件在我们的windows系统中就可以找到,打开c盘下的Windows/Fonts目录:
#首先在/usr/share/fonts目录下新建一个目录chinese,并将中文字体上传至/usr/share/fonts/chinese目录下
mkdir /usr/share/fonts/chinese
chmod -R 755 /usr/share/fonts/chinese
yum -y install ttmkfdir
#然后执行ttmkfdir命令即可
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
#刷新内存中的字体缓存,这样就不用reboot重启了
fc-cache
#看一下字体列表
fd-list