[#15] MailWindow: Add Rundschreiben-Funktion

This commit is contained in:
2024-02-29 10:48:48 +01:00
parent 92c3ed991b
commit e5c462b43f
10 changed files with 922 additions and 50 deletions

View File

@ -11,9 +11,11 @@ namespace Elwig.Documents {
public static string Name => "Dokument";
private static readonly double GenerationProportion = 0.125;
protected static readonly double GenerationProportion = 0.125;
private TempFile? _pdfFile = null;
protected TempFile? _pdfFile = null;
protected string? _pdfPath;
protected string? PdfPath => _pdfPath ?? _pdfFile?.FilePath;
public bool ShowFoldMarks = App.Config.Debug;
public bool DoubleSided = false;
@ -59,6 +61,10 @@ namespace Elwig.Documents {
return new MergedDocument(docs);
}
public static Document FromPdf(string path) {
return new PdfDocument(path);
}
private async Task<string> Render() {
string name;
if (this is BusinessLetter) {
@ -87,20 +93,28 @@ namespace Elwig.Documents {
public async Task Generate(IProgress<double>? progress = null) {
progress?.Report(0.0);
if (this is MergedDocument m) {
if (this is PdfDocument) {
// nothing to do
} else if (this is MergedDocument m) {
var pdf = new TempFile("pdf");
var tmpHtmls = new List<TempFile>();
var tmpFiles = new List<string>();
var n = m.Documents.Count();
int i = 0;
foreach (var doc in m.Documents) {
if (doc is PdfDocument) {
tmpFiles.Add(doc.PdfPath!);
continue;
}
var tmpHtml = new TempFile("html");
await File.WriteAllTextAsync(tmpHtml.FilePath, await doc.Render(), Utils.UTF8);
tmpHtmls.Add(tmpHtml);
tmpFiles.Add(tmpHtml.FileName);
i++;
progress?.Report(GenerationProportion * 100 * i / n);
}
progress?.Report(GenerationProportion * 100);
await Pdf.Convert(tmpHtmls.Select(f => f.FileName), pdf.FileName, DoubleSided, new Progress<double>(v => progress?.Report(GenerationProportion * 100 + v * (1 - GenerationProportion))));
await Pdf.Convert(tmpFiles, pdf.FileName, DoubleSided, new Progress<double>(v => progress?.Report(GenerationProportion * 100 + v * (1 - GenerationProportion))));
foreach (var tmp in tmpHtmls) {
tmp.Dispose();
}
@ -118,13 +132,13 @@ namespace Elwig.Documents {
}
public void SaveTo(string pdfPath) {
if (_pdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
File.Copy(_pdfFile.FilePath, pdfPath);
if (PdfPath == null) throw new InvalidOperationException("Pdf file has not been generated yet");
File.Copy(PdfPath, pdfPath);
}
public async Task Print(int copies = 1) {
if (_pdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
await Pdf.Print(_pdfFile.FilePath, copies);
if (PdfPath == null) throw new InvalidOperationException("Pdf file has not been generated yet");
await Pdf.Print(PdfPath, copies);
}
public void Show() {
@ -132,10 +146,14 @@ namespace Elwig.Documents {
Pdf.Show(_pdfFile.NewReference(), Title + (this is BusinessDocument b ? $" - {b.Member.Name}" : ""));
}
private class MergedDocument : Document {
public IEnumerable<Document> Documents;
public MergedDocument(IEnumerable<Document> docs) : base("Mehrere Dokumente") {
Documents = docs;
private class MergedDocument(IEnumerable<Document> docs) : Document("Mehrere Dokumente") {
public IEnumerable<Document> Documents = docs;
}
private class PdfDocument : Document {
public PdfDocument(string pdfPath) :
base(Path.GetFileNameWithoutExtension(pdfPath)) {
_pdfPath = pdfPath;
}
}
}