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