Renamed Print/ to Documents/

This commit is contained in:
2023-03-05 14:58:31 +01:00
parent 7416c03b92
commit a6264d7a64
10 changed files with 104 additions and 49 deletions

55
WGneu/Documents/Pdf.cs Normal file
View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using PuppeteerSharp;
using PuppeteerSharp.Media;
using WGneu.Windows;
namespace WGneu.Documents {
public static class Pdf {
private static readonly string CHROMIUM = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
private static IBrowser? _Browser = null;
public static void Init() {
var b = Browser;
}
private static IBrowser Browser {
get {
_Browser ??= Puppeteer.LaunchAsync(new LaunchOptions {
Headless = true,
ExecutablePath = CHROMIUM,
}).GetAwaiter().GetResult();
return _Browser;
}
}
public static async Task Convert(string path_html, string path_pdf) {
using var page = await Browser.NewPageAsync();
await page.GoToAsync("file://" + path_html);
await page.WaitForFunctionAsync("() => window.finished");
await page.PdfAsync(path_pdf, new() {
PreferCSSPageSize = true,
//Format = PaperFormat.A4,
DisplayHeaderFooter = false,
MarginOptions = new() {
Top = "0mm",
Right = "0mm",
Bottom = "0mm",
Left = "0mm",
},
});
}
public static void Display(string title, string path) {
var w = new PdfViewerWindow(title, path);
w.Show();
}
}
}

View File

@ -5,14 +5,14 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace WGneu.Print { namespace WGneu.Documents {
class Template { class Template {
private static readonly string ROOT = @"C:\Users\Lorenz\Desktop\"; private static readonly string ROOT = @"C:\Users\Lorenz\Desktop\";
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.Display(ROOT + "test.pdf"); Pdf.Display("Test-Dokument", ROOT + "test.pdf");
} }
} }
} }

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace WGneu.Print { namespace WGneu.Documents {
internal class TestTemplate { internal class TestTemplate {
} }
} }

View File

@ -1,41 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using PuppeteerSharp;
namespace WGneu.Print {
public static class Pdf {
private static readonly string CHROMIUM = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
private static IBrowser? Browser = null;
public static async Task Init() {
Browser = await Puppeteer.LaunchAsync(new LaunchOptions {
Headless = true,
ExecutablePath = CHROMIUM,
});
}
public static async Task Convert(string path_html, string path_pdf) {
if (Browser == null)
await Init();
using var page = await Browser?.NewPageAsync();
await page.GoToAsync("file://" + path_html);
await page.WaitForFunctionAsync("() => window.finished");
await page.PdfAsync(path_pdf);
}
public static void Display(string path) {
Process.Start(new ProcessStartInfo() {
FileName = path,
UseShellExecute = true,
});
}
}
}

View File

@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.14" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1587.40" />
<PackageReference Include="ModernWpfUI" Version="0.9.6" /> <PackageReference Include="ModernWpfUI" Version="0.9.6" />
<PackageReference Include="PuppeteerSharp" Version="9.0.2" /> <PackageReference Include="PuppeteerSharp" Version="9.0.2" />
</ItemGroup> </ItemGroup>

View File

@ -13,6 +13,9 @@
<Compile Update="Windows\DeliveryReceptionWindow.xaml.cs"> <Compile Update="Windows\DeliveryReceptionWindow.xaml.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Update="Windows\PdfViewerWindow.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Page Update="Windows\MainWindow.xaml"> <Page Update="Windows\MainWindow.xaml">
@ -24,5 +27,8 @@
<Page Update="Windows\DeliveryReceptionWindow.xaml"> <Page Update="Windows\DeliveryReceptionWindow.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
<Page Update="Windows\PdfViewerWindow.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -19,9 +19,6 @@ using WGneu.Models;
namespace WGneu.Windows { namespace WGneu.Windows {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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;
@ -32,7 +29,7 @@ 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();
Print.Pdf.Init(); Documents.Pdf.Init();
countryViewSource.Source = _context.Countries.Local.ToObservableCollection(); countryViewSource.Source = _context.Countries.Local.ToObservableCollection();
} }
@ -47,7 +44,7 @@ namespace WGneu.Windows {
} }
private void Button3_Click(object sender, EventArgs e) { private void Button3_Click(object sender, EventArgs e) {
Print.Template.Test(); Documents.Template.Test();
} }
private void Button_Click(object sender, RoutedEventArgs e) { private void Button_Click(object sender, RoutedEventArgs e) {

View File

@ -0,0 +1,14 @@
<Window x:Class="WGneu.Windows.PdfViewerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:local="clr-namespace:WGneu.Windows"
mc:Ignorable="d"
Title="PDF Ansicht"
MinHeight="600" MinWidth="420" Height="600" Width="420">
<Grid>
<wv2:WebView2 Name="WebView"/>
</Grid>
</Window>

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WGneu.Windows {
public partial class PdfViewerWindow : Window {
public PdfViewerWindow(string title, string path) {
InitializeComponent();
Title = Title + " - " + title;
WebView.Source = new("file://" + path);
}
}
}