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

View File

@ -6,6 +6,8 @@ using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Shapes;
using PdfSharp.Pdf.IO;
using PuppeteerSharp;
using PuppeteerSharp.Media;
@ -27,12 +29,19 @@ namespace WGneu.Documents {
evtHandler();
}
public static async Task Convert(string path_html, string path_pdf) {
public static async Task Close() {
if (Browser == null) return;
await Browser.CloseAsync();
Browser = null;
}
public static async Task Convert(string htmlPath, string pdfPath) {
if (Browser == null) throw new InvalidOperationException("The puppeteer engine has not been initialized yet");
using var page = await Browser.NewPageAsync();
await page.GoToAsync("file://" + path_html);
await page.WaitForFunctionAsync("() => window.finished");
await page.PdfAsync(path_pdf, new() {
page.Console += OnConsole;
await page.GoToAsync("file://" + htmlPath);
await page.EvaluateFunctionAsync("async () => { await window.PagedPolyfill.preview(); }");
await page.PdfAsync(pdfPath, new() {
PreferCSSPageSize = true,
//Format = PaperFormat.A4,
DisplayHeaderFooter = false,
@ -45,6 +54,10 @@ namespace WGneu.Documents {
});
}
private static void OnConsole(object sender, ConsoleEventArgs e) {
MessageBox.Show(e.Message.Text);
}
public static void UpdateMetadata(string path, string title, string author) {
using var doc = PdfReader.Open(path);
doc.Info.Title = title;
@ -52,9 +65,18 @@ namespace WGneu.Documents {
doc.Save(path);
}
public static void Show(Utils.TemporaryFile file, string title) {
App.MainDispatcher.BeginInvoke(() => {
var w = new DocumentViewerWindow(title, file);
w.Show();
});
}
public static void Show(string path, string title) {
var w = new DocumentViewerWindow(title, path);
w.Show();
App.MainDispatcher.BeginInvoke(() => {
var w = new DocumentViewerWindow(title, path);
w.Show();
});
}
public static void Print(string path) {