-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (48 loc) · 1.97 KB
/
Program.cs
File metadata and controls
59 lines (48 loc) · 1.97 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
// ***************************************************************************************
// MIT LICENCE
// The maintenance and evolution is maintained by the HtmlPdfPlus team
// https://github.com/FRACerqueira/HtmlPdfPlus
// ***************************************************************************************
using HtmlPdfPlus;
using Microsoft.AspNetCore.Mvc;
using WebHtmlToPdf.CustomSaveFileServer;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
builder.Services.AddHtmlPdfService<DataSavePDF,string>((cfg) =>
{
cfg.Logger(LogLevel.Debug, "MyPDFServer");
});
var app = builder.Build();
var logger = app.Services.GetService<ILogger<Program>>();
//Warmup HtmlPdfServerPlus on startup for better performance from the first request
var WarmupTS = app.WarmupHtmlPdfService<DataSavePDF, string>();
logger?.LogDebug("HtmlPdfServerPlus ready after {tm}", WarmupTS);
// Configure the HTTP request pipeline.
app.MapOpenApi();
app.UseHttpsRedirection();
app.MapPost("/SavePdf", async ([FromServices] IHtmlPdfServer<DataSavePDF,string> PDFserver, [FromBody] byte[] clienthtmltopdf, CancellationToken token) =>
{
return await PDFserver
.ScopeRequest(clienthtmltopdf)
.BeforePDF( (html,inputparam, _) =>
{
if (inputparam is null)
{
return Task.FromResult(html);
}
//performs replacement token substitution in the HTML source before performing the conversion
var aux = html.Replace("[{FileName}]", inputparam.Filename);
return Task.FromResult(aux);
})
.AfterPDF( (pdfbyte, inputparam, token) =>
{
if (inputparam is null)
{
return Task.FromResult(string.Empty);
}
//TODO : performs writing to file after performing conversion
return Task.FromResult(inputparam.Filename);
})
.Run(token);
}).Produces<HtmlPdfResult<string>>(200);
app.Run();