dotnet core 在CentOS下 使用libreoffice 把Office 转换成pdf

1.安装libreoffice,我的服务器是centos,直接使用:

         

yum install libreoffice  

  

 2.因为CentOS 里面没有中文字体,文件中有中文会出现乱码,解决方案

先把 c:WindowsFonts文件夹复制一份到其它盘,然后打包成Fonts.zip,通过rz Fonts.zip 将压缩包传到服务器上面。

cd /usr/share/fonts
rz
unzip Fonts.zip
mv Fonts win
cd win
chmod  -Rf 755 *
mkfontscale
mkfontdir
fc-cache –fv

3.使用命令转换成pdf文件

libreoffice  --invisible --convert-to pdf  DanaZhang.docx

4.使用dotnet core 转化

using System;
using System.Diagnostics;

namespace DanaZhang
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello DanaZhang!");

            var psi = new ProcessStartInfo("libreoffice", "--invisible --convert-to pdf  DanaZhang.docx") { RedirectStandardOutput = true };
            //启动
            var proc = Process.Start(psi);
            if (proc == null)
            {
                Console.WriteLine("不能执行.");
            }
            else
            {
                Console.WriteLine("-------------开始执行--------------");
                //开始读取
                using (var sr = proc.StandardOutput)
                {
                    while (!sr.EndOfStream)
                    {
                        Console.WriteLine(sr.ReadLine());
                    }

                    if (!proc.HasExited)
                    {
                        proc.Kill();
                    }
                }
                Console.WriteLine("---------------执行完成------------------");

                Console.WriteLine($"退出代码 : {proc.ExitCode}");
            }

        }
    }
}

  

  

原文地址:https://www.cnblogs.com/ayzhanglei/p/9111836.html