From 021d7cfe297ca48d05d39031f4106bd30045deb9 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 30 Sep 2026 16:48:51 +0200 Subject: [PATCH] Add PDF download functionality to editor components Introduced the ability to download PDFs in the `SenderPdfImageEditor` component: - Added `PdfFileName` parameter to specify the file name. - Implemented `CanDownloadPdf` property to enable/disable downloads. - Created `OnDownloadPdfAsync` method to handle download logic via JS interop. Updated `SenderPdfImageEditorToolbar` to support downloads: - Added `CanDownloadPdf` and `DownloadPdf` parameters. - Enabled "Download PDF" button based on `CanDownloadPdf`. - Added `DownloadPdfAsync` method to trigger the download callback. Enhanced `SenderEditorPage` to pass `PdfFileName` to `SenderPdfImageEditor`. Added `downloadDataUrlAsFile` function in `envelope-editor.js` to facilitate file downloads using a temporary `` element. Made minor adjustments for consistency, including adding `ZoomStepPercent` to the toolbar and refining `SetZoomPercent` logic. --- .../PdfEditor/SenderPdfImageEditor.razor | 29 ++++++++++++++++++- .../SenderPdfImageEditorToolbar.razor | 14 ++++++++- .../Pages/SenderEditorPage.razor | 1 + .../wwwroot/js/envelope-editor.js | 18 ++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditor.razor b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditor.razor index cfbaf571..90a64d16 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditor.razor +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditor.razor @@ -15,6 +15,7 @@ ZoomPercent="@_zoomPercent" MinZoomPercent="@MinZoomPercent" MaxZoomPercent="@MaxZoomPercent" + ZoomStepPercent="@ZoomStepPercent" GoFirstPage="OnGoFirstPageAsync" GoPreviousPage="OnGoPreviousPageAsync" GoNextPage="OnGoNextPageAsync" @@ -22,7 +23,9 @@ GoToPage="OnGoToPageAsync" ZoomOut="OnZoomOutAsync" ZoomIn="OnZoomInAsync" - SetZoomPercent="OnSetZoomPercentAsync" /> + SetZoomPercent="OnSetZoomPercentAsync" + CanDownloadPdf="@CanDownloadPdf" + DownloadPdf="OnDownloadPdfAsync" /> } @@ -120,6 +123,9 @@ [Parameter] public string? PdfDataUrl { get; set; } + [Parameter] + public string? PdfFileName { get; set; } + [Parameter] public IReadOnlyList Fields { get; set; } = Array.Empty(); @@ -143,6 +149,7 @@ private const int MinZoomPercent = 50; private const int MaxZoomPercent = 300; private const int ZoomStepPercent = 5; + private bool CanDownloadPdf => !string.IsNullOrWhiteSpace(PdfDataUrl); private static string GetFieldStyle(SenderPdfFieldOverlay field, double pageWidthPt, double pageHeightPt) { @@ -370,6 +377,26 @@ _zoomPercent = Math.Max(MinZoomPercent, Math.Min(value, MaxZoomPercent)); } + private async Task OnDownloadPdfAsync() + { + if (string.IsNullOrWhiteSpace(PdfDataUrl)) + return; + + var fallbackName = string.IsNullOrWhiteSpace(PdfFileName) + ? "document.pdf" + : PdfFileName; + + var fileName = fallbackName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) + ? fallbackName + : $"{fallbackName}.pdf"; + + await JSRuntime.InvokeVoidAsync( + "envelopeEditor.downloadDataUrlAsFile", + PdfDataUrl, + fileName, + "application/pdf"); + } + private void OnEditorMouseUp(MouseEventArgs args) { _draggedFieldIndex = null; diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditorToolbar.razor b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditorToolbar.razor index 6e1fd0a4..11087b8b 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditorToolbar.razor +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Components/PdfEditor/SenderPdfImageEditorToolbar.razor @@ -94,7 +94,7 @@ -