Templates working

This commit is contained in:
2023-03-09 20:46:01 +01:00
parent a55678e5ef
commit d514a639a9
13 changed files with 158 additions and 41 deletions

@ -4,17 +4,29 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
namespace WGneu.Documents {
public abstract class Document {
public abstract class Document : IDisposable {
private Utils.TemporaryFile? PdfFile = null;
public Document(string title) {
Title = title;
Header = "Winzergenossenschaft Matzen";
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; }
@ -22,24 +34,41 @@ namespace WGneu.Documents {
public string Footer { get; set; }
public string FullDateString {
get {
return Date.ToString("dddd, d. MMMM yyyy");
}
get => Date.ToString("dddd, d. MMMM yyyy");
}
public DateTime Date { get; set; }
private async Task<string> Render() {
if (this is BusinessLetter bl) {
return await Html.CompileRenderAsync("BusinessLetter", bl);
if (this is BusinessLetter) {
return await Html.CompileRenderAsync("BusinessLetter.cshtml", this);
}
throw new InvalidOperationException();
}
public async Task<string> Save() {
// TODO tempfile
await File.WriteAllTextAsync("razor_test.html", await Render());
return "";
public async Task Generate() {
var pdf = new Utils.TemporaryFile(@"C:\Users\Lorenz\Desktop", ".pdf");
using (var tmpHtml = new Utils.TemporaryFile(@"C:\Users\Lorenz\Desktop", ".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);
}
}
}