Linux下word转pdf
借助LibreOffice, 该软件是openoffice办公套件衍生版,开源,支持多系统
下载地址:https://zh-cn.libreoffice.org/download/libreoffice/
linux下安装方式
具体可见:https://wiki.documentfoundation.org/Documentation/Install/Linux
$ tar zxvf LibreOffice_$version_Linux_x86-rpm.tar.gz
$ cd LibreOffice_$version_Linux_x86-rpm/
$ cd RPMS/
Fedora / CentOS
$ su -c 'yum install *.rpm'
Mandriva / Mageia
$ su -c 'urpmi *.rpm'
Other RPM based systems =
su -c ‘rpm -Uvh *.rpm’
$ sudo ln -n -s /opt/libreoffice$version/ /usr/lib/libreoffice
通知执行libreoffice命令行命令进行转换文件
$ soffice --convert-to pdf --outdir /home --nologo xxx.docx
/home :转换后文件的目录
xxx.docx:要转换的文件
转换后的pdf出现乱码
原因:word中的字体在linux下不存在
解决方法:将字体文件拷贝到linux下
封装类库如下:
using DocXToPdfConverter; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Text; namespace Util { public class OfficeConverter { public static void DocxToPdf(string libreOfficePath, string officePath, string outPutPath) { //获取libreoffice命令的路径 //string libreOfficePath = getLibreOfficePath(); ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, string.Format("--convert-to pdf --outdir {0} --nologo {1}", outPutPath, officePath)); procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; procStartInfo.CreateNoWindow = true; //procStartInfo.WorkingDirectory = Environment.CurrentDirectory; //开启线程 Process process = new Process() { StartInfo = procStartInfo, }; process.Start(); process.WaitForExit(); if (process.ExitCode != 0) { throw new LibreOfficeFailedException(process.ExitCode); } } } public class LibreOfficeFailedException : Exception { public LibreOfficeFailedException(int exitCode) : base(string.Format("LibreOffice错误 {0}", exitCode)) { } } }