Gremlin/Gremlin_BlazorServer/Services/PdfService.cs

77 lines
2.5 KiB
C#

using System.Diagnostics;
using Gremlin_BlazorServer.Data.EntityClasses;
namespace Gremlin_BlazorServer.Services;
internal abstract class PdfService {
public static async Task<bool> CreatePdf(Quote quote) {
//Copy images to quotePath
try {
string path = HostingService.GetImgPath();
File.Copy(Path.Combine(path, "agilentLogo.png"), Path.Combine(quote.Path, "agilentLogo.png"), true);
File.Copy(Path.Combine(path, "signWoitschetzki.png"), Path.Combine(quote.Path, "signWoitschetzki.png"), true);
//Create PDF twice
if (await RunningPdfLaTeX(quote, 2)) return RemoveTempFiles(quote.Path);
Console.WriteLine("Erstellung des PDF nicht erfolgreich!");
return false;
}
catch (Exception ex) {
Console.WriteLine(ex);
return false;
}
}
private static bool RemoveTempFiles(string quotePath) {
DirectoryInfo directoryInfo = new(quotePath);
foreach (FileInfo file in directoryInfo.EnumerateFiles())
if (file.Extension is ".aux" or ".out" or ".log" or ".png")
try {
file.Delete();
}
catch (Exception ex) {
Console.WriteLine(ex);
return false;
}
return true;
}
private static async Task<bool> RunningPdfLaTeX(Quote quote, int runs) {
for (int i = 0; i < runs; i++) {
using Process process = new();
process.StartInfo.WorkingDirectory = quote.Path;
process.StartInfo.FileName = "pdflatex";
process.StartInfo.Arguments = quote.QuotationNumber;
process.StartInfo.UseShellExecute = true;
try {
_ = await Task.Run(() => process.Start());
}
catch (Exception ex) {
Console.WriteLine(ex);
return false;
}
process.WaitForExit();
}
return true;
}
public static bool OpenPdfWithOkular(Quote quote) {
using Process process = new();
process.StartInfo.WorkingDirectory = quote.Path;
process.StartInfo.FileName = "okular"; //@"C:\Program Files\Okular\bin\okular.exe";
process.StartInfo.Arguments = $"{quote.QuotationNumber}.pdf";
try {
return process.Start();
}
catch (Exception ex) {
Console.WriteLine(ex);
return false;
}
}
}