75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using Elwig.Helpers;
|
|
|
|
namespace Elwig.Documents {
|
|
public abstract class Document : IDisposable {
|
|
|
|
private TempFile? 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 TempFile("pdf");
|
|
using (var tmpHtml = new TempFile("html")) {
|
|
await File.WriteAllTextAsync(tmpHtml.FilePath, await Render());
|
|
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath);
|
|
}
|
|
Pdf.UpdateMetadata(pdf.FilePath, Title, "Winzergenossenschaft 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);
|
|
}
|
|
}
|
|
}
|