[#15] MailWindow: Add feature to print

This commit is contained in:
2024-03-05 12:18:02 +01:00
parent 234710887e
commit 95850c1d81
4 changed files with 33 additions and 9 deletions

View File

@ -16,6 +16,8 @@ namespace Elwig.Documents {
protected TempFile? _pdfFile = null;
protected string? _pdfPath;
protected string? PdfPath => _pdfPath ?? _pdfFile?.FilePath;
public int? TotalPages { get; private set; }
public int? Pages => TotalPages / (DoubleSided ? 2 : 1);
public bool ShowFoldMarks = App.Config.Debug;
public bool DoubleSided = false;
@ -114,7 +116,8 @@ namespace Elwig.Documents {
progress?.Report(GenerationProportion * 100 * i / n);
}
progress?.Report(GenerationProportion * 100);
await Pdf.Convert(tmpFiles, pdf.FileName, DoubleSided, new Progress<double>(v => progress?.Report(GenerationProportion * 100 + v * (1 - GenerationProportion))));
var pages = await Pdf.Convert(tmpFiles, pdf.FileName, DoubleSided, new Progress<double>(v => progress?.Report(GenerationProportion * 100 + v * (1 - GenerationProportion))));
TotalPages = pages.Pages;
foreach (var tmp in tmpHtmls) {
tmp.Dispose();
}
@ -124,7 +127,8 @@ namespace Elwig.Documents {
using (var tmpHtml = new TempFile("html")) {
await File.WriteAllTextAsync(tmpHtml.FilePath, await Render(), Utils.UTF8);
progress?.Report(50.0);
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath, DoubleSided);
var pages = await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath, DoubleSided);
TotalPages = pages.Pages;
}
_pdfFile = pdf;
}

View File

@ -49,11 +49,11 @@ namespace Elwig.Helpers.Printing {
return Task.CompletedTask;
}
public static async Task<IEnumerable<int>> Convert(string htmlPath, string pdfPath, bool doubleSided = false, IProgress<double>? progress = null) {
public static async Task<(int Pages, IEnumerable<int> PerDoc)> Convert(string htmlPath, string pdfPath, bool doubleSided = false, IProgress<double>? progress = null) {
return await Convert([htmlPath], pdfPath, doubleSided, progress);
}
public static async Task<IEnumerable<int>> Convert(IEnumerable<string> htmlPath, string pdfPath, bool doubleSided = false, IProgress<double>? progress = null) {
public static async Task<(int Pages, IEnumerable<int> PerDoc)> Convert(IEnumerable<string> htmlPath, string pdfPath, bool doubleSided = false, IProgress<double>? progress = null) {
if (WinziPrintProc == null) throw new InvalidOperationException("The WinziPrint process has not been initialized yet");
progress?.Report(0.0);
using var client = new TcpClient("127.0.0.1", 30983);
@ -71,8 +71,8 @@ namespace Elwig.Helpers.Printing {
var parts = line[9..].Trim().Split('/').Select(int.Parse).ToArray();
progress?.Report(100.0 * parts[0] / parts[1]);
} else if (line.StartsWith("success:")) {
var m = Regex.Match(line, @"\(([0-9, ]+)\)");
return m.Groups[1].Value.Split(", ").Select(int.Parse).ToList();
var m = Regex.Match(line, @"([0-9]+) pages \(([0-9, ]+)\)");
return (int.Parse(m.Groups[1].Value), m.Groups[2].Value.Split(", ").Select(int.Parse).ToList());
}
}
}

View File

@ -249,9 +249,11 @@
Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="3" FontSize="14"
Click="PreviewButton_Click"/>
<Button x:Name="PrintButton" Content="Drucken" IsEnabled="False"
Grid.Row="2" Grid.Column="2" FontSize="14"/>
Grid.Row="2" Grid.Column="2" FontSize="14"
Click="PrintButton_Click"/>
<Button x:Name="EmailButton" Content="E-Mails verschicken" IsEnabled="False"
Grid.Row="2" Grid.Column="4" FontSize="14"/>
Grid.Row="2" Grid.Column="4" FontSize="14"
Click="EmailButton_Click"/>
</Grid>
<Button x:Name="BackButton" Content="Zurück" Grid.Row="1"

View File

@ -543,7 +543,7 @@ namespace Elwig.Windows {
GenerateButton.IsEnabled = true;
Mouse.OverrideCursor = null;
PreviewButton.IsEnabled = true;
//PrintButton.IsEnabled = true;
PrintButton.IsEnabled = true;
//EmailButton.IsEnabled = true;
}
@ -571,6 +571,24 @@ namespace Elwig.Windows {
}
}
private async void PrintButton_Click(object sender, RoutedEventArgs evt) {
if (PrintDocument == null) return;
var res = MessageBox.Show($"Sollen {PrintDocument.Pages} Blätter ({PrintDocument.TotalPages} Seiten) gedruckt werden?\n" +
$"Sind die \"Duplex-Einstellungen\" des Standarddruckers entsprechend eingestellt (doppelseitig bzw. einseitig)?",
"Rundschreiben drucken", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (res == MessageBoxResult.Yes) {
if (App.Config.Debug) {
PrintDocument.Show();
} else {
await PrintDocument.Print();
}
}
}
private void EmailButton_Click(object sender, RoutedEventArgs evt) {
// TODO
}
public void AddDeliveryConfirmation() {
AvaiableDocumentsList.SelectedIndex = 1;
if (AvaiableDocumentsList.SelectedItem is not string s || SelectedDocs.Any(d => d.Type == DocType.DeliveryConfirmation))