Add WeasyPrint to convert PDFs

This commit is contained in:
2023-09-06 16:01:48 +02:00
parent 28b424fe65
commit 2b7d19199a
11 changed files with 136 additions and 154 deletions

View File

@ -2,17 +2,28 @@ using System;
using System.Threading.Tasks;
using System.IO;
using Elwig.Helpers;
using System.Text;
namespace Elwig.Documents {
public abstract class Document : IDisposable {
private TempFile? PdfFile = null;
public string DataPath;
public int CurrentNextSeason;
public string? DocumentId;
public string Title;
public string Author;
public string Header;
public string Footer;
public DateTime Date;
public Document(string title) {
var c = App.Client;
DataPath = App.DataPath;
CurrentNextSeason = Utils.CurrentNextSeason;
Title = title;
Author = App.Client.NameFull;
Header = $"<h1>{c.Name}</h1>";
Footer = Utils.GenerateFooter("<br/>", " \u00b7 ")
.Item(c.NameFull).NextLine()
@ -33,15 +44,7 @@ namespace Elwig.Documents {
GC.SuppressFinalize(this);
}
public string DataPath { get; set; }
public int CurrentNextSeason { get; set; }
public string Title { get; set; }
public string Header { get; set; }
public string Footer { get; set; }
public DateTime Date { get; set; }
public string? DocumentId { get; set; }
private async Task<string> Render() {
private Task<string> Render() {
string name;
if (this is BusinessLetter) {
name = "BusinessLetter";
@ -50,16 +53,19 @@ namespace Elwig.Documents {
} else {
throw new InvalidOperationException("Invalid document object");
}
return await Html.CompileRenderAsync(name, this);
return Render(name);
}
private Task<string> Render(string name) {
return 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 File.WriteAllTextAsync(tmpHtml.FilePath, await Render(), Encoding.UTF8);
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath);
}
Pdf.UpdateMetadata(pdf.FilePath, Title, App.Client.NameFull);
PdfFile = pdf;
}