Added support for culture-specific globalization in the Blazor WebAssembly project by disabling invariant globalization and enabling the loading of all globalization data. Improved error handling in `ReportViewer.razor` by wrapping the `ExportToAsync` method in a `try-catch` block to handle export failures gracefully. Updated user-facing messages to provide clearer feedback in case of errors.
183 lines
7.2 KiB
Plaintext
183 lines
7.2 KiB
Plaintext
@page "/reportviewer/"
|
|
@using System.Drawing
|
|
@using DevExpress.Drawing
|
|
@using DevExpress.Utils
|
|
@using DevExpress.XtraPrinting
|
|
@using DevExpress.XtraPrinting.Drawing
|
|
@using DevExpress.XtraReports.UI;
|
|
@using EnvelopeGenerator.ReceiverUI.Services;
|
|
@inject IJSRuntime JSRuntime
|
|
@inject InMemoryReportStorageWebExtension ReportStorage
|
|
|
|
<link href="_content/DevExpress.Blazor.Themes/blazing-berry.bs5.min.css" rel="stylesheet" />
|
|
<link href="_content/DevExpress.Blazor.Reporting.Viewer/css/dx-blazor-reporting-components.bs5.css" rel="stylesheet" />
|
|
|
|
<div class="card m-3">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Unterschrift</h5>
|
|
<p class="card-text text-muted mb-2">
|
|
@if(SignatureApplied) {
|
|
<span>Die Unterschrift wurde dem Bericht hinzugefuegt. Sie koennen die Unterschrift erneuern oder das signierte PDF exportieren.</span>
|
|
} else {
|
|
<span>Bitte fuegen Sie vor dem PDF-Export Ihre Unterschrift hinzu.</span>
|
|
}
|
|
</p>
|
|
@if(!string.IsNullOrWhiteSpace(SignatureValidationMessage)) {
|
|
<div class="text-danger mb-2">@SignatureValidationMessage</div>
|
|
}
|
|
<div class="d-flex gap-2 flex-wrap">
|
|
<button class="btn btn-primary" @onclick="OpenSignaturePopupAsync">
|
|
@(SignatureApplied ? "Unterschrift erneuern" : "Unterschrift hinzufuegen")
|
|
</button>
|
|
<button class="btn btn-success" disabled="@(!SignatureApplied)" @onclick="ExportSignedPdfAsync">Signiertes PDF exportieren</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<DxPopup @bind-Visible="SignaturePopupVisible"
|
|
HeaderText="Unterschrift erfassen"
|
|
Width="520px"
|
|
ShowFooter="true"
|
|
CloseOnEscape="true"
|
|
CloseOnOutsideClick="false">
|
|
<BodyContentTemplate>
|
|
<p class="text-muted mb-2">Bitte unterschreiben Sie im folgenden Feld.</p>
|
|
<canvas id="receiver-signature-pad" width="420" height="150" class="border rounded bg-white w-100" style="max-width: 420px; touch-action: none;"></canvas>
|
|
@if(!string.IsNullOrWhiteSpace(PopupValidationMessage)) {
|
|
<div class="text-danger mt-2">@PopupValidationMessage</div>
|
|
}
|
|
</BodyContentTemplate>
|
|
<FooterContentTemplate>
|
|
<div class="d-flex gap-2 flex-wrap justify-content-end w-100">
|
|
<button class="btn btn-outline-secondary" @onclick="RenewSignatureAsync">Unterschrift erneuern</button>
|
|
<button class="btn btn-primary" @onclick="ApplySignatureAsync">Zum Bericht hinzufuegen</button>
|
|
<button class="btn btn-secondary" @onclick="CloseSignaturePopup">Schliessen</button>
|
|
</div>
|
|
</FooterContentTemplate>
|
|
</DxPopup>
|
|
|
|
@if(Report is not null) {
|
|
<DxReportViewer @key="ViewerKey" @ref="reportViewer" Report="Report" RootCssClasses="w-100 h-100" />
|
|
}
|
|
|
|
@code {
|
|
DxReportViewer reportViewer;
|
|
XtraReport? Report;
|
|
bool SignatureApplied;
|
|
bool SignaturePopupVisible;
|
|
string? SignatureValidationMessage;
|
|
string? PopupValidationMessage;
|
|
int ViewerKey;
|
|
|
|
protected override async Task OnInitializedAsync() {
|
|
Report = CreateReportInstance();
|
|
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
async Task OpenSignaturePopupAsync() {
|
|
SignaturePopupVisible = true;
|
|
SignatureValidationMessage = null;
|
|
PopupValidationMessage = null;
|
|
await InvokeAsync(StateHasChanged);
|
|
await Task.Delay(50);
|
|
await JSRuntime.InvokeVoidAsync("receiverSignature.initialize", "receiver-signature-pad");
|
|
}
|
|
|
|
async Task RenewSignatureAsync() {
|
|
PopupValidationMessage = null;
|
|
await JSRuntime.InvokeVoidAsync("receiverSignature.clear", "receiver-signature-pad");
|
|
}
|
|
|
|
void CloseSignaturePopup() {
|
|
PopupValidationMessage = null;
|
|
SignaturePopupVisible = false;
|
|
}
|
|
|
|
async Task ApplySignatureAsync() {
|
|
var signatureDataUrl = await JSRuntime.InvokeAsync<string?>("receiverSignature.getDataUrl", "receiver-signature-pad");
|
|
|
|
if(string.IsNullOrWhiteSpace(signatureDataUrl)) {
|
|
PopupValidationMessage = "Die Unterschrift ist fuer den PDF-Export erforderlich.";
|
|
return;
|
|
}
|
|
|
|
PopupValidationMessage = null;
|
|
SignatureValidationMessage = null;
|
|
Report = CreateSignedReportInstance(signatureDataUrl);
|
|
SignatureApplied = true;
|
|
SignaturePopupVisible = false;
|
|
ViewerKey++;
|
|
}
|
|
|
|
async Task ExportSignedPdfAsync() {
|
|
if(!SignatureApplied || Report is null) {
|
|
SignatureValidationMessage = "Bitte fuegen Sie die Unterschrift zuerst zum Bericht hinzu.";
|
|
return;
|
|
}
|
|
|
|
try {
|
|
SignatureValidationMessage = null;
|
|
await reportViewer.ExportToAsync(ExportFormat.Pdf);
|
|
} catch(Exception) {
|
|
SignatureValidationMessage = "Das signierte PDF konnte nicht exportiert werden. Bitte laden Sie die Seite neu und versuchen Sie es erneut.";
|
|
}
|
|
}
|
|
|
|
XtraReport CreateReportInstance() {
|
|
return ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport)
|
|
? savedReport
|
|
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
|
|
}
|
|
|
|
XtraReport CreateSignedReportInstance(string signatureDataUrl) {
|
|
var report = CreateReportInstance();
|
|
AddSignature(report, signatureDataUrl);
|
|
return report;
|
|
}
|
|
|
|
static void AddSignature(XtraReport report, string signatureDataUrl) {
|
|
var imageBytes = Convert.FromBase64String(signatureDataUrl[(signatureDataUrl.IndexOf(',') + 1)..]);
|
|
using var imageStream = new MemoryStream(imageBytes);
|
|
var imageSource = new ImageSource(DXImage.FromStream(imageStream));
|
|
var bottomMargin = report.Bands.OfType<BottomMarginBand>().FirstOrDefault();
|
|
|
|
if(bottomMargin is null) {
|
|
bottomMargin = new BottomMarginBand();
|
|
report.Bands.Add(bottomMargin);
|
|
}
|
|
|
|
bottomMargin.HeightF = Math.Max(bottomMargin.HeightF, 120F);
|
|
RemoveExistingSignature(bottomMargin);
|
|
|
|
var signatureLabel = new XRLabel {
|
|
Name = "receiverSignatureLabel",
|
|
Text = $"Empfaengerunterschrift - {DateTime.Now:g}",
|
|
BoundsF = new RectangleF(390F, 6F, 230F, 18F),
|
|
Font = new DXFont("Open Sans", 8F, DXFontStyle.Bold),
|
|
ForeColor = System.Drawing.Color.FromArgb(73, 80, 87),
|
|
TextAlignment = TextAlignment.MiddleLeft
|
|
};
|
|
|
|
var signature = new XRPictureBox {
|
|
Name = "receiverSignatureImage",
|
|
ImageSource = imageSource,
|
|
BoundsF = new RectangleF(390F, 28F, 230F, 70F),
|
|
Sizing = ImageSizeMode.ZoomImage,
|
|
Borders = BorderSide.Bottom,
|
|
BorderColor = System.Drawing.Color.FromArgb(73, 80, 87)
|
|
};
|
|
|
|
bottomMargin.Controls.AddRange(new XRControl[] { signatureLabel, signature });
|
|
}
|
|
|
|
static void RemoveExistingSignature(BottomMarginBand bottomMargin) {
|
|
var controls = bottomMargin.Controls
|
|
.Cast<XRControl>()
|
|
.Where(control => control.Name is "receiverSignatureLabel" or "receiverSignatureImage")
|
|
.ToArray();
|
|
|
|
foreach(var control in controls)
|
|
bottomMargin.Controls.Remove(control);
|
|
}
|
|
} |