75 lines
2.8 KiB
C#
75 lines
2.8 KiB
C#
using System.Threading.Tasks;
|
|
using Elwig.Helpers;
|
|
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.Documents {
|
|
public static class Pdf {
|
|
|
|
private static readonly string PdfToPrinter = App.ExePath + "PDFtoPrinter.exe";
|
|
private static readonly string WinziPrint = App.ExePath + "WinziPrint.exe";
|
|
private static Process? WinziPrintProc;
|
|
public static bool IsReady => WinziPrintProc != null;
|
|
|
|
|
|
public static async Task Init(Action evtHandler) {
|
|
var p = new Process() { StartInfo = new() {
|
|
FileName = WinziPrint,
|
|
Arguments = "-",
|
|
CreateNoWindow = true,
|
|
UseShellExecute = false,
|
|
RedirectStandardInput = true,
|
|
RedirectStandardOutput = true
|
|
} };
|
|
p.Start();
|
|
WinziPrintProc = p;
|
|
evtHandler();
|
|
}
|
|
|
|
public static async Task<IEnumerable<int>> Convert(string htmlPath, string pdfPath) {
|
|
return await Convert(new string[] { htmlPath }, pdfPath);
|
|
}
|
|
|
|
public static async Task<IEnumerable<int>> Convert(IEnumerable<string> htmlPath, string pdfPath) {
|
|
if (WinziPrintProc == null) throw new InvalidOperationException("The WinziPrint process has not been initialized yet");
|
|
await WinziPrintProc.StandardInput.WriteLineAsync($"{string.Join(';', htmlPath)};{pdfPath}");
|
|
var line = await WinziPrintProc.StandardOutput.ReadLineAsync() ?? throw new IOException("Invalid response from WinziPrint");
|
|
if (line.StartsWith("error:")) {
|
|
MessageBox.Show(line[6..].Trim(), "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
return Array.Empty<int>();
|
|
}
|
|
var m = Regex.Match(line, @"\(([0-9, ]+)\)");
|
|
return m.Groups[1].Value.Split(", ").Select(n => int.Parse(n));
|
|
}
|
|
|
|
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) {
|
|
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();
|
|
}
|
|
}
|
|
}
|