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)) { } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)