-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertWithLibreOffice.cs
191 lines (152 loc) · 6.75 KB
/
ConvertWithLibreOffice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
namespace DocXToPdfConverter.DocXToPdfHandlers
{
//THIS ALL COMES FROM: https://github.com/Reflexe/doc_to_pdf
//And very helpful: https://stackoverflow.com/questions/30349542/command-libreoffice-headless-convert-to-pdf-test-docx-outdir-pdf-is-not
public class LibreOfficeFailedException : Exception
{
public LibreOfficeFailedException(int exitCode)
: base(string.Format("LibreOffice has failed with " + exitCode))
{ }
}
public static class LibreOfficeWrapper
{
private static string GetLibreOfficePath()
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
return "/usr/bin/soffice";
case PlatformID.Win32NT:
string binaryDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
return binaryDirectory + "\\Windows\\program\\soffice.exe";
default:
throw new PlatformNotSupportedException("Your OS is not supported");
}
}
//libreOfficePath for Windows: e. g. "C:\\program\\soffice.exe
//With Portable Apps it is here: C:\PortableApps\LibreOfficePortable\App\libreoffice\program\soffice.exe
public static void Convert(string inputFile, string outputFile, string libreOfficePath)
{
var commandArgs = new List<string>();
string convertedFile = "";
if (string.IsNullOrEmpty(libreOfficePath))
{
libreOfficePath = GetLibreOfficePath();
}
//Create tmp folder
var tmpFolder = Path.Combine(Path.GetDirectoryName(outputFile), "DocXHtmlToPdfConverterTmp" + Guid.NewGuid().ToString().Substring(0, 10));
if (!Directory.Exists(tmpFolder))
{
Directory.CreateDirectory(tmpFolder);
}
commandArgs.Add("--convert-to");
if ((inputFile.EndsWith(".html") || inputFile.EndsWith(".htm")) && outputFile.EndsWith(".pdf"))
{
commandArgs.Add("pdf:writer_pdf_Export");
convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".pdf");
}
else if (inputFile.EndsWith(".docx") && outputFile.EndsWith(".pdf"))
{
commandArgs.Add("pdf:writer_pdf_Export");
convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".pdf");
}
else if (inputFile.EndsWith(".docx") && (outputFile.EndsWith(".html") || outputFile.EndsWith(".htm")))
{
commandArgs.Add("html:HTML:EmbedImages");
convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".html");
}
else if ((inputFile.EndsWith(".html") || inputFile.EndsWith(".htm")) && (outputFile.EndsWith(".docx")))
{
commandArgs.Add("docx:\"Office Open XML Text\"");
convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".docx");
}
commandArgs.AddRange(new[] { inputFile, "--norestore", "--writer", "--headless", "--outdir", tmpFolder });
var procStartInfo = new ProcessStartInfo(libreOfficePath);
foreach (var arg in commandArgs) { procStartInfo.ArgumentList.Add(arg); }
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
var process = new Process() { StartInfo = procStartInfo };
Process[] pname = Process.GetProcessesByName("soffice");
//Supposedly, only one instance of Libre Office can be run simultaneously
while (pname.Length > 0)
{
Thread.Sleep(5000);
pname = Process.GetProcessesByName("soffice");
}
process.Start();
process.WaitForExit();
// Check for failed exit code.
if (process.ExitCode != 0)
{
throw new LibreOfficeFailedException(process.ExitCode);
}
else
{
if (File.Exists(outputFile)) File.Delete(outputFile);
if (File.Exists(convertedFile))
{
File.Move(convertedFile, outputFile);
}
ClearDirectory(tmpFolder);
Directory.Delete(tmpFolder);
}
}
public static void Print(string inputFile, string printerName, string libreOfficePath)
{
var commandArgs = new List<string>();
if (string.IsNullOrEmpty(libreOfficePath))
{
libreOfficePath = GetLibreOfficePath();
}
commandArgs.Add("-p");
if (!string.IsNullOrEmpty(printerName))
{
commandArgs.Add("-pt");
commandArgs.Add(printerName);
}
commandArgs.AddRange(new[] { inputFile, "--norestore", "--writer", "--headless" });
var procStartInfo = new ProcessStartInfo(libreOfficePath);
foreach (var arg in commandArgs) { procStartInfo.ArgumentList.Add(arg); }
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
var process = new Process() { StartInfo = procStartInfo };
Process[] pname = Process.GetProcessesByName("soffice");
//Supposedly, only one instance of Libre Office can be run simultaneously
while (pname.Length > 0)
{
Thread.Sleep(5000);
pname = Process.GetProcessesByName("soffice");
}
process.Start();
process.WaitForExit();
// Check for failed exit code.
if (process.ExitCode != 0)
{
throw new LibreOfficeFailedException(process.ExitCode);
}
}
private static void ClearDirectory(string folderName)
{
var dir = new DirectoryInfo(folderName);
foreach (FileInfo fi in dir.GetFiles())
{
fi.Delete();
}
foreach (DirectoryInfo di in dir.GetDirectories())
{
ClearDirectory(di.FullName);
di.Delete();
}
}
}
}