Rename solution directory to Elwig
This commit is contained in:
78
Elwig/Documents/Document.cshtml.cs
Normal file
78
Elwig/Documents/Document.cshtml.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user