Search code examples
c#asp.net-mvcpdflibreoffice

LibreOffice Convert XLSX to PDF in ASP.NET MVC


Version 4.3

In C# I am trying to use the headless option to convert a XLSX to a PDF but nothing happens when I run this from ASP.NET or a simple Command prompt.

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

Where the exe and params are:

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

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

I used the GUI to test that LibreOffice can convert the file and it worked fine.


Solution

  • Here is how to convert Excel, Word etc to PDF on an ASP.NET MVC web site at no cost:

    Install LibreOffice, free

    Set the current directory to the same folder as the existing XLS. This seems to be the missing piece.

    Run this:

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

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

    Make sure your WorkerProcess has access to the exe, by default it does not.