LibreOffice在ASP.NET MVC中将XLSX转换为PDF

Ian*_*ink 4 c# pdf asp.net-mvc libreoffice

版本4.3

在C#中,我试图使用无头选项将XLSX转换为PDF,但是当我从ASP.NET或简单的命令提示符运行时没有任何反应.

            var pdfProcess = new Process();
            pdfProcess.StartInfo.FileName = exe;
            pdfProcess.StartInfo.Arguments = param + " \"" + fullDocPath +"\"";
            pdfProcess.Start();
Run Code Online (Sandbox Code Playgroud)

exe和params是:

C:\Program Files (x86)\LibreOffice 4\program\soffice.exe

  -norestore -nofirststartwizard -nologo -headless -convert-to pdf  "c:\UDS_Docs\temp\Teller Roster National.xlsx"
Run Code Online (Sandbox Code Playgroud)

我使用GUI来测试LibreOffice可以转换文件,它运行正常.

Ian*_*ink 7

以下是如何在ASP.NET MVC网站上免费将Excel,Word等转换为PDF:

免费安装LibreOffice

将当前目录设置为与现有XLS相同的文件夹.这似乎是缺失的一块.

运行这个:

"C:\Program Files (x86)\LibreOffice 4\program\soffice.exe"  -norestore -nofirststartwizard -headless -convert-to pdf  "TheFile.xlsx"
Run Code Online (Sandbox Code Playgroud)

在C#中:

var pdfProcess = new Process();
pdfProcess.StartInfo.FileName = exePdf;
pdfProcess.StartInfo.Arguments = "-norestore -nofirststartwizard -headless -convert-to pdf  \"TheFile.xlsx\"";
pdfProcess.StartInfo.WorkingDirectory = docPath; //This is really important
pdfProcess.Start();
Run Code Online (Sandbox Code Playgroud)

确保您的WorkerProcess可以访问exe,默认情况下不会.

  • 只是为了补充这一点,因为我一直爬上墙试图弄清楚这一切.基本上在您的应用程序池中,您希望将"加载用户配置文件"设置为true - 我认为这是默认设置,但我的是错误的,并且不会造成任何麻烦. (2认同)