Using Html/Pdf Initializer
This commit is contained in:
@ -3,6 +3,7 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:local="clr-namespace:WGneu"
|
xmlns:local="clr-namespace:WGneu"
|
||||||
StartupUri="Windows\MainWindow.xaml"
|
StartupUri="Windows\MainWindow.xaml"
|
||||||
|
Startup="App_Startup"
|
||||||
xmlns:ui="http://schemas.modernwpf.com/2019">
|
xmlns:ui="http://schemas.modernwpf.com/2019">
|
||||||
<Application.Resources>
|
<Application.Resources>
|
||||||
<!--<ResourceDictionary>
|
<!--<ResourceDictionary>
|
||||||
|
@ -5,11 +5,32 @@ using System.Data;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
|
||||||
namespace WGneu {
|
namespace WGneu {
|
||||||
public partial class App : Application {
|
public partial class App : Application {
|
||||||
|
|
||||||
|
public static bool IsPrintingReady => Documents.Html.IsReady && Documents.Pdf.IsReady;
|
||||||
|
|
||||||
public App() : base() {
|
public App() : base() {
|
||||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void App_Startup(object sender, EventArgs e) {
|
||||||
|
Task.Run(() => Documents.Html.Init(PrintingReadyChanged));
|
||||||
|
Task.Run(() => Documents.Pdf.Init(PrintingReadyChanged));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PrintingReadyChanged() {
|
||||||
|
Dispatcher.BeginInvoke(App_PrintingReadyChanged, this, new EventArgs());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void App_PrintingReadyChanged(object sender, EventArgs e) {
|
||||||
|
foreach (Window w in Windows) {
|
||||||
|
foreach (var b in Utils.FindVisualChilds<Button>(w).Where(b => "Print".Equals(b.Tag))) {
|
||||||
|
b.IsEnabled = IsPrintingReady;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ namespace WGneu.Documents {
|
|||||||
|
|
||||||
private async Task<string> Render() {
|
private async Task<string> Render() {
|
||||||
if (this is BusinessLetter bl) {
|
if (this is BusinessLetter bl) {
|
||||||
return await Pdf.CompileRenderAsync("BusinessLetter.cshtml", bl);
|
return await Html.CompileRenderAsync("BusinessLetter", bl);
|
||||||
}
|
}
|
||||||
throw new InvalidOperationException();
|
throw new InvalidOperationException();
|
||||||
}
|
}
|
||||||
|
31
WGneu/Documents/Html.cs
Normal file
31
WGneu/Documents/Html.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using RazorLight;
|
||||||
|
using RazorLight.Extensions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WGneu.Documents {
|
||||||
|
public static class Html {
|
||||||
|
private static RazorLightEngine? Engine = null;
|
||||||
|
public static bool IsReady => Engine != null;
|
||||||
|
|
||||||
|
public static async Task Init(Action evtHandler) {
|
||||||
|
var e = new RazorLightEngineBuilder()
|
||||||
|
.UseFileSystemProject(@"C:\Users\Lorenz\source\repos\WGneu\WGneu\Documents")
|
||||||
|
.UseMemoryCachingProvider()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
await e.CompileTemplateAsync("BusinessLetter");
|
||||||
|
|
||||||
|
Engine = e;
|
||||||
|
evtHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<string> CompileRenderAsync(string key, object model) {
|
||||||
|
if (Engine == null) throw new InvalidOperationException("The razor engine has not been initialized yet");
|
||||||
|
return await Engine.CompileRenderAsync(key, model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -16,35 +16,19 @@ namespace WGneu.Documents {
|
|||||||
public static class Pdf {
|
public static class Pdf {
|
||||||
|
|
||||||
private static readonly string CHROMIUM = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
|
private static readonly string CHROMIUM = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
|
||||||
private static IBrowser? _Browser = null;
|
private static IBrowser? Browser = null;
|
||||||
private static RazorLightEngine? _Engine = null;
|
public static bool IsReady => Browser != null;
|
||||||
|
|
||||||
public static void Init() {
|
public static async Task Init(Action evtHandler) {
|
||||||
var b = Browser;
|
Browser = await Puppeteer.LaunchAsync(new LaunchOptions {
|
||||||
var e = Engine;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IBrowser Browser {
|
|
||||||
get {
|
|
||||||
_Browser ??= Puppeteer.LaunchAsync(new LaunchOptions {
|
|
||||||
Headless = true,
|
Headless = true,
|
||||||
ExecutablePath = CHROMIUM,
|
ExecutablePath = CHROMIUM,
|
||||||
}).GetAwaiter().GetResult();
|
});
|
||||||
return _Browser;
|
evtHandler();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static RazorLightEngine Engine {
|
|
||||||
get {
|
|
||||||
_Engine ??= new RazorLightEngineBuilder()
|
|
||||||
.UseFileSystemProject(@"C:\Users\Lorenz\source\repos\WGneu\WGneu\Documents")
|
|
||||||
.UseMemoryCachingProvider()
|
|
||||||
.Build();
|
|
||||||
return _Engine;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task Convert(string path_html, string path_pdf) {
|
public static async Task Convert(string path_html, string path_pdf) {
|
||||||
|
if (Browser == null) throw new InvalidOperationException("The puppeteer engine has not been initialized yet");
|
||||||
using var page = await Browser.NewPageAsync();
|
using var page = await Browser.NewPageAsync();
|
||||||
await page.GoToAsync("file://" + path_html);
|
await page.GoToAsync("file://" + path_html);
|
||||||
await page.WaitForFunctionAsync("() => window.finished");
|
await page.WaitForFunctionAsync("() => window.finished");
|
||||||
@ -68,13 +52,13 @@ namespace WGneu.Documents {
|
|||||||
doc.Save(path);
|
doc.Save(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Display(string title, string path) {
|
public static void Show(string path, string title) {
|
||||||
var w = new DocumentViewerWindow(title, path);
|
var w = new DocumentViewerWindow(title, path);
|
||||||
w.Show();
|
w.Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<string> CompileRenderAsync(string key, object model) {
|
public static void Print(string path) {
|
||||||
return await Engine.CompileRenderAsync(key, model);
|
// TODO print pdf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ namespace WGneu.Documents {
|
|||||||
public static async void Test() {
|
public static async void Test() {
|
||||||
await Pdf.Convert(ROOT + "din-5008.html", ROOT + "test.pdf");
|
await Pdf.Convert(ROOT + "din-5008.html", ROOT + "test.pdf");
|
||||||
Pdf.UpdateMetadata(ROOT + "test.pdf", "Test Dokument", "Winzergenossenschaft für Matzen und Umgebung reg. Gen.m.b.H.");
|
Pdf.UpdateMetadata(ROOT + "test.pdf", "Test Dokument", "Winzergenossenschaft für Matzen und Umgebung reg. Gen.m.b.H.");
|
||||||
Pdf.Display("Test-Dokument", ROOT + "test.pdf");
|
Pdf.Show(ROOT + "test.pdf", "Test-Dokument");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async void Generate(WgContext c) {
|
public static async void Generate(WgContext c) {
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
<Grid>
|
<Grid>
|
||||||
<ComboBox HorizontalAlignment="Left" Margin="175,149,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Source={StaticResource countryViewSource}}" ItemTemplate="{StaticResource asdf}" SelectionChanged="ComboBox_SelectionChanged" IsEditable="True"/>
|
<ComboBox HorizontalAlignment="Left" Margin="175,149,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Source={StaticResource countryViewSource}}" ItemTemplate="{StaticResource asdf}" SelectionChanged="ComboBox_SelectionChanged" IsEditable="True"/>
|
||||||
<Button x:Name="Button2" Content="Mitglieder" Margin="472,182,178,0" VerticalAlignment="Top" Click="Button2_Click"/>
|
<Button x:Name="Button2" Content="Mitglieder" Margin="472,182,178,0" VerticalAlignment="Top" Click="Button2_Click"/>
|
||||||
<Button x:Name="Button3" Content="Print" Margin="425,255,225,0" VerticalAlignment="Top" Click="Button3_Click"/>
|
<Button x:Name="Button3" Content="Print" Margin="425,255,225,0" VerticalAlignment="Top" Click="Button3_Click" Tag="Print"/>
|
||||||
<Button x:Name="Button4" Content="Generate" Margin="425,300,225,0" VerticalAlignment="Top" Click="Button4_Click"/>
|
<Button x:Name="Button4" Content="Generate" Margin="425,300,225,0" VerticalAlignment="Top" Click="Button4_Click" Tag="Print"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
@ -22,6 +22,7 @@ namespace WGneu.Windows {
|
|||||||
public partial class MainWindow : Window {
|
public partial class MainWindow : Window {
|
||||||
private readonly WgContext _context = new WgContext();
|
private readonly WgContext _context = new WgContext();
|
||||||
private CollectionViewSource countryViewSource;
|
private CollectionViewSource countryViewSource;
|
||||||
|
|
||||||
public MainWindow() {
|
public MainWindow() {
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
countryViewSource = (CollectionViewSource)FindResource("countryViewSource");
|
countryViewSource = (CollectionViewSource)FindResource("countryViewSource");
|
||||||
@ -29,8 +30,9 @@ namespace WGneu.Windows {
|
|||||||
|
|
||||||
private void Window_Loaded(object sender, RoutedEventArgs e) {
|
private void Window_Loaded(object sender, RoutedEventArgs e) {
|
||||||
_context.Countries.Load();
|
_context.Countries.Load();
|
||||||
Documents.Pdf.Init();
|
|
||||||
countryViewSource.Source = _context.Countries.Local.ToObservableCollection();
|
countryViewSource.Source = _context.Countries.Local.ToObservableCollection();
|
||||||
|
Button3.IsEnabled = App.IsPrintingReady;
|
||||||
|
Button4.IsEnabled = App.IsPrintingReady;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnClosing(CancelEventArgs e) {
|
protected override void OnClosing(CancelEventArgs e) {
|
||||||
|
Reference in New Issue
Block a user