Implement Chromium-based PDF generation service; add fetchAndDownloadPdf utility and update contract generation endpoint
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using FluentValidation;
|
||||
using FrontOffice.Main.Utilities;
|
||||
using System.Net;
|
||||
using FrontOffice.Main.Utilities.Pdf;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -36,41 +37,46 @@ else
|
||||
builder.Services.Configure<EncryptionSettings>(builder.Configuration.GetSection("EncryptionSettings"));
|
||||
builder.Services.AddSingleton<MobileNumberEncryptor>();
|
||||
|
||||
var app = builder.Build();
|
||||
var webApp = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (!app.Environment.IsDevelopment())
|
||||
if (!webApp.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/Error");
|
||||
webApp.UseExceptionHandler("/Error");
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
webApp.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
webApp.UseHttpsRedirection();
|
||||
|
||||
app.UseStaticFiles();
|
||||
webApp.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
webApp.UseRouting();
|
||||
|
||||
app.MapBlazorHub();
|
||||
app.MapFallbackToPage("/_Host");
|
||||
webApp.MapBlazorHub();
|
||||
webApp.MapFallbackToPage("/_Host");
|
||||
|
||||
app.MapGet("/contract/sample", (FrontOffice.Main.Utilities.Pdf.IHtmlToPdfService pdfService) =>
|
||||
{
|
||||
var html = @"<h2 style='text-align:center'>قرارداد نمونه همکاری</h2><p>این یک متن نمونه برای قرارداد است که فقط جهت تست دانلود PDF قرار داده شده است.</p><ul><li>بند ۱: استفاده صرفا مجاز.</li><li>بند ۲: رعایت قوانین لازم است.</li><li>بند ۳: مسئولیت اطلاعات با کاربر است.</li></ul><p class='small'>تاریخ تولید: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm") + "</p>";
|
||||
var bytes = pdfService.GeneratePdf(html, "sample-contract");
|
||||
return Results.File(bytes, "application/pdf", "sample-contract.pdf");
|
||||
});
|
||||
|
||||
app.MapGet("/contract/generate", (string html, string? fileName, FrontOffice.Main.Utilities.Pdf.IHtmlToPdfService pdfService) =>
|
||||
webApp.MapGet("/contract/generate",async (string html, string? fileName, IChromiumPdfService pdfService) =>
|
||||
{
|
||||
var decoded = WebUtility.UrlDecode(html);
|
||||
var safeName = string.IsNullOrWhiteSpace(fileName) ? "contract" : fileName.Trim();
|
||||
var bytes = pdfService.GeneratePdf(decoded, safeName);
|
||||
var bytes =await pdfService.GeneratePdfAsync(decoded, safeName);
|
||||
return Results.File(bytes, "application/pdf", safeName + ".pdf");
|
||||
});
|
||||
|
||||
app.Run();
|
||||
// Support large HTML via POST (body) to avoid URL length limits
|
||||
webApp.MapPost("/contract/generate", async (ContractPdfRequest req, IChromiumPdfService pdfService) =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(req.Html)) return Results.BadRequest("Empty html");
|
||||
var safeName = string.IsNullOrWhiteSpace(req.FileName) ? "contract" : req.FileName.Trim();
|
||||
var bytes = await pdfService.GeneratePdfAsync(req.Html, safeName);
|
||||
return Results.File(bytes, "application/pdf", safeName + ".pdf");
|
||||
});
|
||||
|
||||
webApp.Run();
|
||||
public sealed record ContractPdfRequest(string Html, string? FileName);
|
||||
|
||||
|
||||
|
||||
public class AppSettings
|
||||
|
||||
Reference in New Issue
Block a user