1

I currently have the portable version of libreoffice sitting in a folder and initiate the headless version to convert some docx files to pdf one by one. It takes about 20 seconds per pdf despite they are only about 9 pages. I am not sure if my code has a flaw or if there is some method to optimize this because the user experience is terrible waiting 20 to 30 seconds for each pdf to become available. I am also only doing this conversion to display the docx in a pdf viewer (pdf.js) as there is no free solutions to render docx files in the browser unless uploaded to 3rd party servers (ie, iframe google viewer, etc). My code is as follows:

        public static void ConvertToPDF(string PathToItemToConvert, string PathToLibrePortable)
    {
            bool converted = false;

            try
            {
                string fileName = Path.GetFileName(PathToItemToConvert);
                string fileDir = Path.GetDirectoryName(PathToItemToConvert);

                var pdfProcess = new Process();
                pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                pdfProcess.StartInfo.FileName = PathToLibrePortable;
                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();

                converted = true;

            }
            catch (Exception ex)
            {
                converted = false;
                System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }
Irish Redneck
  • 983
  • 7
  • 32
  • Please read [**these @Kurt Pfeifle recommendations**](https://stackoverflow.com/a/30465397/14094617) carefully – JohnSUN Sep 07 '22 at 17:29

1 Answers1

0

I also had this same challenge trying to accomplish the same task. I found this helpful stackoverflow post Here.

Basically I learned you can pass in multiple files in the command arguments, thus saving time for the start and stop processing of LibreOffice improving performance.

In my code, I did something like this which improved speed around 75%. Still not as fast as I would like but a lot better than converting one file at a time:

            string libreOfficePath = "....\LibreOfficePortable\App\libreoffice\program\swriter.exe";  

            string cmdArg = "/C ";
            cmdArg += libreOfficePath;
            cmdArg += " --headless --convert-to pdf --outdir ../PDF";

            string[] filePaths = Directory.GetFiles("myWorkingDirectoryPathHere");

            if (filePaths.Length > 0)
            {
                foreach (var docfilePath in filePaths)
                {
                    cmdArg += " \"" + Path.GetFileName(docfilePath) + "\"";
                }
            }

            Process myProc = new Process();
            ProcessStartInfo processStartInfo = new ProcessStartInfo();
            processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            processStartInfo.FileName = "cmd.exe";
            processStartInfo.WorkingDirectory = "myWorkingDirectoryPathHere";
            processStartInfo.Arguments = cmdArg;
            myProc.StartInfo = processStartInfo;
            myProc.Start();
            myProc.WaitForExit();
hvndev
  • 96
  • 1
  • 7
  • interesting approach. Didnt realize you could do that. My solution ended up being using MS Graph API which has convert to PDF option and its fastttttt. So I just store the files there and convert them as needed now. Seems to work so much better. Done with local server storage which is what I was using this for. However, your answer should help people so I am selecting it. – Irish Redneck Apr 28 '23 at 23:00
  • Ah okay, I will check that out. This docx to PDF conversion thing is such a pain. Thanks for the advice! – hvndev Apr 28 '23 at 23:35