Previewer: added button to show generated PDF

This commit is contained in:
MarcinZiabek
2022-03-24 01:02:57 +01:00
parent 545b76fd40
commit 404914ea7e
7 changed files with 47 additions and 52 deletions
+2
View File
@@ -11,6 +11,8 @@ var model = DataSource.GetReport();
var report = new StandardReport(model);
report.ShowInPreviewer();
return;
Document
.Create(container =>
{
@@ -18,16 +18,18 @@ namespace QuestPDF.Previewer
{
ArgumentNullException.ThrowIfNull(document);
//Currently there is no way to unitialize a previously run avalonia app.
//So we need to check if the previewer was already run and show the window again.
// currently there is no way to utilize a previously run Avalonia app.
// so we need to check if the previewer was already run and show the window again.
if(Application.Current?.ApplicationLifetime is ClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new PreviewerWindow()
{
Document = document,
};
desktop.MainWindow.Show();
desktop.Start(Array.Empty<string>());
return;
}
-2
View File
@@ -39,8 +39,6 @@ namespace QuestPDF.Previewer
public PreviewerControl()
{
ScrollViewportSize = 0.5f;
PagesProperty.Changed.Subscribe(p =>
{
InteractiveCanvas.Pages = Pages;
-43
View File
@@ -1,43 +0,0 @@
using System.Diagnostics;
using Avalonia.Controls;
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
namespace QuestPDF.Previewer
{
static class PreviewerUtils
{
public static async Task<bool> SavePdfWithDialog(IDocument? document, Window dialogOwner)
{
if (document == null)
return false;
var dialog = new SaveFileDialog()
{
DefaultExtension = ".pdf",
InitialFileName = document.GetMetadata().Title ?? "Document",
Filters = new List<FileDialogFilter>()
{
new FileDialogFilter()
{
Extensions = new List<string>() { "pdf" },
}
}
};
var filePath = await dialog.ShowAsync(dialogOwner);
if (string.IsNullOrWhiteSpace(filePath))
return false;
var dirPath = Path.GetDirectoryName(filePath);
if (!Directory.Exists(dirPath))
return false;
//TODO Catch layout exceptions.
document.GeneratePdf(filePath);
Process.Start("explorer.exe", filePath);
return true;
}
}
}
+12
View File
@@ -37,6 +37,18 @@
ScrollViewportSize="{Binding #Window.ScrollViewportSize, Mode=OneWayToSource}"
Pages="{Binding #Window.DocumentRenderer.Pages, Mode=OneWay}" />
<Button Grid.Row="1" Grid.Column="0"
VerticalAlignment="Bottom" HorizontalAlignment="Left"
Margin="32" Padding="10" CornerRadius="100"
Click="ShowPDF"
ToolTip.Tip="Generates PDF file and shows it in the default browser. Useful for testing compatibility and interactive links.">
<Viewbox Width="24" Height="24">
<Canvas Width="24" Height="24">
<Path Fill="White" Data="M14,2H6A2,2 0 0,0 4,4V20A2,2 0 0,0 6,22H13C12.59,21.75 12.2,21.44 11.86,21.1C11.53,20.77 11.25,20.4 11,20H6V4H13V9H18V10.18C18.71,10.34 19.39,10.61 20,11V8L14,2M20.31,18.9C21.64,16.79 21,14 18.91,12.68C16.8,11.35 14,12 12.69,14.08C11.35,16.19 12,18.97 14.09,20.3C15.55,21.23 17.41,21.23 18.88,20.32L22,23.39L23.39,22L20.31,18.9M16.5,19A2.5,2.5 0 0,1 14,16.5A2.5,2.5 0 0,1 16.5,14A2.5,2.5 0 0,1 19,16.5A2.5,2.5 0 0,1 16.5,19Z" />
</Canvas>
</Viewbox>
</Button>
<ScrollBar Grid.Row="1" Grid.Column="1"
Orientation="Vertical"
AllowAutoHide="False"
+27 -4
View File
@@ -5,6 +5,7 @@ using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using ReactiveUI;
@@ -57,10 +58,7 @@ namespace QuestPDF.Previewer
{
VerticalScrollbarVisible = e.NewValue.Value < 1;
}));
// this.FindControl<Button>("GeneratePdf")
// .Click += (_, _) => _ = PreviewerUtils.SavePdfWithDialog(Document, this);
DocumentProperty.Changed.Subscribe(v => Task.Run(() => DocumentRenderer.UpdateDocument(v.NewValue.Value)));
HotReloadManager.UpdateApplicationRequested += InvalidatePreview;
}
@@ -85,5 +83,30 @@ namespace QuestPDF.Previewer
{
AvaloniaXamlLoader.Load(this);
}
private void ShowPDF(object? sender, RoutedEventArgs e)
{
var path = Path.GetTempPath() + ".pdf";
try
{
DocumentRenderer.Document?.GeneratePdf(path);
}
catch (Exception exception)
{
new ExceptionDocument(exception).GeneratePdf(path);
}
var openBrowserProcess = new Process
{
StartInfo = new()
{
UseShellExecute = true,
FileName = path
}
};
openBrowserProcess.Start();
}
}
}
+2 -1
View File
@@ -52,7 +52,8 @@ namespace QuestPDF.Drawing
{
return new SKPaint
{
Color = SKColor.Parse(color)
Color = SKColor.Parse(color),
IsAntialias = true
};
}
}