Documents: Add Letterhead
This commit is contained in:
@ -2,11 +2,15 @@ using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using Elwig.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
|
||||
namespace Elwig.Documents {
|
||||
public abstract class Document : IDisposable {
|
||||
public abstract partial class Document : IDisposable {
|
||||
|
||||
private TempFile? PdfFile = null;
|
||||
private TempFile? _pdfFile = null;
|
||||
private string? _renderedHtml = null;
|
||||
|
||||
public bool ShowFoldMarks = App.Config.Debug;
|
||||
|
||||
@ -35,17 +39,56 @@ namespace Elwig.Documents {
|
||||
Date = DateTime.Today;
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"</body>.*?</footer>\s*</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled)]
|
||||
private static partial Regex GeneratedDocumentHeaderRegex();
|
||||
private static readonly Regex DocumentHeaderRegex = GeneratedDocumentHeaderRegex();
|
||||
|
||||
[GeneratedRegex(@"<style>.*?/style>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled)]
|
||||
private static partial Regex GeneratedHtmlStyleRegex();
|
||||
private static readonly Regex HtmlStyleRegex = GeneratedHtmlStyleRegex();
|
||||
|
||||
[GeneratedRegex(@"<link[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled)]
|
||||
private static partial Regex GeneratedHtmlLinkRegex();
|
||||
private static readonly Regex HtmlLinkRegex = GeneratedHtmlLinkRegex();
|
||||
|
||||
~Document() {
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
PdfFile?.Dispose();
|
||||
PdfFile = null;
|
||||
_pdfFile?.Dispose();
|
||||
_pdfFile = null;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private Task<string> Render() {
|
||||
public static async Task<Document> Merge(IEnumerable<Document> docs) {
|
||||
string html = "";
|
||||
var styles = new List<string>();
|
||||
foreach (var d in docs) {
|
||||
var h = await d.Render();
|
||||
var s = HtmlStyleRegex.Matches(h).Select(m => m.Value).ToList();
|
||||
var l = HtmlLinkRegex.Matches(h).Select(m => m.Value).ToList();
|
||||
if (s.All(styles.Contains)) {
|
||||
h = HtmlStyleRegex.Replace(h, "");
|
||||
} else {
|
||||
styles.AddRange(s);
|
||||
}
|
||||
if (l.All(styles.Contains)) {
|
||||
h = HtmlLinkRegex.Replace(h, "");
|
||||
} else {
|
||||
styles.AddRange(l);
|
||||
}
|
||||
html += h;
|
||||
}
|
||||
html = DocumentHeaderRegex.Replace(html, "<div class='document-break'/>");
|
||||
return new InternalDocument("Mehrere Dokumente") {
|
||||
_renderedHtml = html,
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<string> Render() {
|
||||
if (_renderedHtml != null)
|
||||
return _renderedHtml;
|
||||
string name;
|
||||
if (this is BusinessLetter) {
|
||||
name = "BusinessLetter";
|
||||
@ -55,14 +98,17 @@ namespace Elwig.Documents {
|
||||
name = "CreditNote";
|
||||
} else if (this is DeliveryJournal) {
|
||||
name = "DeliveryJournal";
|
||||
} else if (this is Letterhead) {
|
||||
name = "Letterhead";
|
||||
} else {
|
||||
throw new InvalidOperationException("Invalid document object");
|
||||
}
|
||||
return Render(name);
|
||||
return await Render(name);
|
||||
}
|
||||
|
||||
private Task<string> Render(string name) {
|
||||
return Html.CompileRenderAsync(name, this);
|
||||
private async Task<string> Render(string name) {
|
||||
_renderedHtml = await Html.CompileRenderAsync(name, this);
|
||||
return _renderedHtml;
|
||||
}
|
||||
|
||||
public async Task Generate() {
|
||||
@ -71,22 +117,26 @@ namespace Elwig.Documents {
|
||||
await File.WriteAllTextAsync(tmpHtml.FilePath, await Render(), Utils.UTF8);
|
||||
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath);
|
||||
}
|
||||
PdfFile = pdf;
|
||||
_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);
|
||||
if (_pdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
|
||||
File.Copy(_pdfFile.FilePath, pdfPath);
|
||||
}
|
||||
|
||||
public async Task Print(int copies = 1) {
|
||||
if (PdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
|
||||
await Pdf.Print(PdfFile.FilePath, copies);
|
||||
if (_pdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
|
||||
await Pdf.Print(_pdfFile.FilePath, copies);
|
||||
}
|
||||
|
||||
public void Show() {
|
||||
if (PdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
|
||||
Pdf.Show(PdfFile.NewReference(), Title);
|
||||
if (_pdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet");
|
||||
Pdf.Show(_pdfFile.NewReference(), Title);
|
||||
}
|
||||
|
||||
private class InternalDocument : Document {
|
||||
public InternalDocument(string title) : base(title) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user