【问题标题】:LibreOffice Convert XLSX to PDF in ASP.NET MVCLibreOffice 在 ASP.NET MVC 中将 XLSX 转换为 PDF
【发布时间】:2015-01-05 00:42:11
【问题描述】:

4.3 版

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

            var pdfProcess = new Process();
            pdfProcess.StartInfo.FileName = exe;
            pdfProcess.StartInfo.Arguments = param + " \"" + fullDocPath +"\"";
            pdfProcess.Start();

exe 和参数在哪里:

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

  -norestore -nofirststartwizard -nologo -headless -convert-to pdf  "c:\UDS_Docs\temp\Teller Roster National.xlsx"

我使用 GUI 测试了 LibreOffice 可以转换文件并且工作正常。

【问题讨论】:

    标签: c# asp.net-mvc pdf libreoffice


    【解决方案1】:

    以下是如何在 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"
    

    在 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();
    

    确保您的 WorkerProcess 可以访问该 exe,默认情况下它没有。

    【讨论】:

    • 非常感谢您的回答。工作目录是最重要的事情,我在浪费了半天之后终于知道了;)
    • 只是补充一下,因为我一直在爬墙试图弄清楚这一切。基本上,在您的应用程序池中,您希望将 Load User Profile 设置为 true - 我认为这是默认设置,但我的设置为 false,这会导致无穷无尽的麻烦。
    【解决方案2】:

    原来我尝试运行的 exe 需要大量访问权限(它是 LibreOffice,用于从 Excel 制作 PDF)。

    所以最简单的解决方案就是制作一个超级简单的 WindowsService,安装它并运行它。 Web 站点将大量数据放入服务监视的位置,然后进行工作,然后 Web 站点将其拾取。这样我就可以以最低权限运行网站,并且仍然可以运行主要服务。

    【讨论】:

    • 感谢您的见解,但请您指出该网站需要哪些权限以及哪些目录。我已授予 IIS_IUSRS 用户对 LibreOffice 目录的所有权限以及对输出目录和输入文件的所有权限,但仍然没有任何反应。我可能缺少什么?
    • 你没有。创建一个 Windows 服务并让该服务监控网站将文件放入的放置文件夹。 Web 服务器不运行 LibreOffice,WindowsService 可以。您需要编写一个 Windows 服务。
    【解决方案3】:

    基于@BahaiResearch.com 的 GREAT answer,我在转换代码中添加了一些功能:

            string fileName = Path.GetFileName(excelFilePath);
            string fileDir = Path.GetDirectoryName(excelFilePath);
    
            var pdfProcess = new Process();
            pdfProcess.StartInfo.FileName = @"C:\Program Files\LibreOffice\program\soffice.exe";
            pdfProcess.StartInfo.Arguments =
                String.Format("--norestore --nofirststartwizard --headless --convert-to pdf  \"{0}\""
                                      , fileName);
            pdfProcess.StartInfo.WorkingDirectory = fileDir; 
            pdfProcess.StartInfo.RedirectStandardOutput = true;
            pdfProcess.StartInfo.RedirectStandardError = true;
            pdfProcess.StartInfo.UseShellExecute = false;
            pdfProcess.Start();
    
            string output = pdfProcess.StandardOutput.ReadToEnd();
            string error = pdfProcess.StandardError.ReadToEnd();
    

    一些参考:StandardOutputUseShellExecute

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多