Documents: use WinziPrint instead of WeasyPrint

This commit is contained in:
2023-10-19 16:38:03 +02:00
parent a2f49e1b8b
commit a832879b73
3 changed files with 60 additions and 74 deletions

View File

@ -2,44 +2,50 @@ using System.Threading.Tasks;
using Elwig.Helpers;
using Elwig.Windows;
using System.Diagnostics;
using Balbarak.WeasyPrint;
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 FilesManager WeasyPrintManager = new();
private static string? WeasyPrintPython = null;
private static string? WeasyPrintDir => WeasyPrintManager.FolderPath;
public static bool IsReady => WeasyPrintPython != null && WeasyPrintDir != null;
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) {
if (!WeasyPrintManager.IsFilesExsited()) {
await WeasyPrintManager.InitFilesAsync();
}
WeasyPrintPython = Path.Combine(WeasyPrintManager.FolderPath, "python.exe");
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 Convert(string htmlPath, string pdfPath) {
var p = new Process() { StartInfo = new() {
FileName = WeasyPrintPython,
CreateNoWindow = true,
WorkingDirectory = WeasyPrintDir,
RedirectStandardError = true,
} };
p.StartInfo.EnvironmentVariables["PATH"] = "Scripts;gtk3;" + Environment.GetEnvironmentVariable("PATH");
p.StartInfo.ArgumentList.Add("scripts/weasyprint.exe");
p.StartInfo.ArgumentList.Add("-e");
p.StartInfo.ArgumentList.Add("utf8");
p.StartInfo.ArgumentList.Add(htmlPath);
p.StartInfo.ArgumentList.Add(pdfPath);
p.Start();
await p.WaitForExitAsync();
var stderr = await p.StandardError.ReadToEndAsync();
if (p.ExitCode != 0) throw new Exception(stderr);
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) {