Files
elwig/Elwig/Helpers/Printing/Pdf.cs

96 lines
4.3 KiB
C#

using System.Threading.Tasks;
using Elwig.Windows;
using System.Diagnostics;
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows;
using System.Text.RegularExpressions;
using System.Linq;
namespace Elwig.Helpers.Printing {
public static class Pdf {
private static readonly string PdfToPrinter = new string[] { App.ExePath }
.Union(Environment.GetEnvironmentVariable("PATH")?.Split(';') ?? [])
.Select(x => Path.Combine(x, "PDFtoPrinter.exe"))
.Where(x => File.Exists(x))
.FirstOrDefault() ?? throw new FileNotFoundException("PDFtoPrinter executable not found");
private static readonly string WinziPrint = new string[] { App.ExePath }
.Union(Environment.GetEnvironmentVariable("PATH")?.Split(';') ?? [])
.Select(x => Path.Combine(x, "WinziPrint.exe"))
.Where(x => File.Exists(x))
.FirstOrDefault() ?? throw new FileNotFoundException("WiniPrint executable not found");
private static Process? WinziPrintProc;
public static bool IsReady => WinziPrintProc != null;
public static async Task Init(Action? evtHandler = null) {
var p = new Process() { StartInfo = new() {
FileName = WinziPrint,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true
} };
p.StartInfo.ArgumentList.Add("-p");
p.StartInfo.ArgumentList.Add("-e");
p.StartInfo.ArgumentList.Add("utf-8");
p.StartInfo.ArgumentList.Add("-d");
p.StartInfo.ArgumentList.Add(App.TempPath);
p.StartInfo.ArgumentList.Add("-");
p.Start();
WinziPrintProc = p;
evtHandler?.Invoke();
}
public static async Task<IEnumerable<int>> Convert(string htmlPath, string pdfPath, bool doubleSided = false, IProgress<double>? progress = null) {
return await Convert(new string[] { htmlPath }, pdfPath, doubleSided, progress);
}
public static async Task<IEnumerable<int>> Convert(IEnumerable<string> htmlPath, string pdfPath, bool doubleSided = false, IProgress<double>? progress = null) {
if (WinziPrintProc == null) throw new InvalidOperationException("The WinziPrint process has not been initialized yet");
progress?.Report(0.0);
await WinziPrintProc.StandardInput.WriteLineAsync((doubleSided ? "-2;" : "") + $"{string.Join(';', htmlPath)};{pdfPath}");
while (true) {
var line = await WinziPrintProc.StandardOutput.ReadLineAsync() ?? throw new IOException("Invalid response from WinziPrint");
if (line.StartsWith("error:")) {
throw new IOException($"WinziPrint: {line[6..].Trim()}");
} else if (line.StartsWith("progress:")) {
var parts = line[9..].Trim().Split('/').Select(int.Parse).ToArray();
progress?.Report(100.0 * parts[0] / parts[1]);
} else if (line.StartsWith("success:")) {
var m = Regex.Match(line, @"\(([0-9, ]+)\)");
return m.Groups[1].Value.Split(", ").Select(int.Parse).ToList();
}
}
}
public static void Show(TempFile file, string title) {
App.MainDispatcher.BeginInvoke(() => {
var w = new DocumentViewerWindow(title, file);
w.Show();
});
}
public static void Show(string path, string title) {
App.MainDispatcher.BeginInvoke(() => {
var w = new DocumentViewerWindow(title, path);
w.Show();
});
}
public static async Task Print(string path, int copies = 1) {
try {
var p = new Process() { StartInfo = new() { FileName = PdfToPrinter } };
p.StartInfo.ArgumentList.Add(path);
p.StartInfo.ArgumentList.Add("/s");
p.StartInfo.ArgumentList.Add($"copies={copies}");
p.Start();
await p.WaitForExitAsync();
} catch (Exception e) {
MessageBox.Show("Beim Drucken ist ein Fehler aufgetreten:\n\n" + e.Message, "Fehler beim Drucken");
}
}
}
}