Files
elwig/WGneu/Documents/Document.cshtml.cs

78 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
namespace WGneu.Documents {
public abstract class Document : IDisposable {
private Utils.TemporaryFile? PdfFile = null;
public Document(string title) {
Title = title;
Header = "<h1>Winzergenossenschaft Matzen</h1>";
Footer = "Winzergenossenschaft für Matzen und Umgebung reg. Gen.m.b.H.";
Date = DateTime.Today;
}
~Document() {
Dispose();
}
public void Dispose() {
PdfFile?.Dispose();
PdfFile = null;
}
public string Title { get; set; }
public string Header { get; set; }
public string Footer { get; set; }
public string FullDateString {
get => Date.ToString("dddd, d. MMMM yyyy");
}
public DateTime Date { get; set; }
private async Task<string> Render() {
string name;
if (this is BusinessLetter) {
name = "BusinessLetter";
} else {
throw new InvalidOperationException();
}
return await Html.CompileRenderAsync(name, this);
}
public async Task Generate() {
var pdf = new Utils.TemporaryFile(".pdf");
using (var tmpHtml = new Utils.TemporaryFile(".html")) {
await File.WriteAllTextAsync(tmpHtml.FilePath, await Render());
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath);
}
Pdf.UpdateMetadata(pdf.FilePath, Title, "Wizergenossenschaft für Matzen und Umgebung reg. Gen.m.b.H.");
PdfFile = pdf;
}
public void SaveTo(string pdfPath) {
if (PdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
File.Copy(PdfFile.FilePath, pdfPath);
}
public async Task Print() {
if (PdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
Pdf.Print(PdfFile.FilePath);
}
public void Show() {
if (PdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
Pdf.Show(PdfFile.NewReference(), Title);
}
}
}