Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (50 loc) · 1.91 KB
/
Copy pathProgram.cs
File metadata and controls
60 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Atli.Reports.Blazor.Extensions;
using Atli.Reports.Client;
using Atli.Reports.Engine;
using ExampleTemplates.Reports;
using RemoteReportServer;
var builder = WebApplication.CreateSlimBuilder(args);
builder.AddServiceDefaults();
// Blazor reports render in this app, and the reports server converts them to PDF. AddReportsClient
// reads ConnectionStrings:reports-server, which the AppHost sets with WithReference, and replaces the
// in-process engine's converter: this app never starts a browser and needs none installed. It also
// adds a health check, tagged ready, that probes the server, so /health reports the server too.
builder.Services.AddBlazorReports();
builder.AddReportsClient("reports-server");
var app = builder.Build();
app.MapDefaultEndpoints();
var reportsGroup = app.MapGroup("reports");
reportsGroup.MapBlazorReport<ReportWithRepeatingHeaderPerPage>();
// Any HTML document, through the same IHtmlToPdfConverter the reports use.
app.MapPost(
"/html-to-pdf",
async (
HtmlDocument document,
IHtmlToPdfConverter converter,
CancellationToken cancellationToken
) =>
{
var result = await converter.ConvertAsync(
document.Html,
new PdfOptions { PaperSize = PaperSize.A4 },
cancellationToken
);
return result.Match<IResult>(
pdf => Results.File(pdf, "application/pdf", "document.pdf"),
error =>
Results.Problem(
title: error.Message,
statusCode: error.Kind switch
{
ConversionErrorKind.InvalidRequest => StatusCodes.Status400BadRequest,
ConversionErrorKind.Busy or ConversionErrorKind.BrowserUnavailable =>
StatusCodes.Status503ServiceUnavailable,
ConversionErrorKind.Timeout or ConversionErrorKind.SignalTimeout =>
StatusCodes.Status504GatewayTimeout,
_ => StatusCodes.Status500InternalServerError,
}
)
);
}
);
app.Run();