This commit is contained in:
2025-04-30 18:04:10 +03:30
committed by alibw
commit de7f960603
8 changed files with 160 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Stimulsoft.Reports.Net" Version="2025.2.3" />
</ItemGroup>
</Project>

48
PdfService/Program.cs Normal file
View File

@@ -0,0 +1,48 @@
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using PdfService;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/Print", async (HttpContext context) =>
{
var request = context.Request;
var boundary = request.ContentType.Split("boundary=")[1];
var reader = new MultipartReader(boundary, request.Body);
MultipartSection section;
var parts = new List<(string ContentType, string Content, string ConentDisposition)>();
while ((section = await reader.ReadNextSectionAsync()) != null)
{
using var sr = new StreamReader(section.Body);
var content = await sr.ReadToEndAsync();
var contentType = section.ContentType;
var contentDisposition = section.ContentDisposition;
parts.Add((contentType, content, contentDisposition));
}
string reportTemplate = "";
string fileName = "";
dynamic data = null;
foreach (var part in parts)
{
if (part.ContentType == "application/json")
{
data = JsonConvert.DeserializeObject(part.Content);
}
else if (part.ContentType == "application/xml")
{
reportTemplate = part.Content;
fileName = part.ConentDisposition?.Split(".")[1];
}
}
var reportStream = Reporter.Print(reportTemplate, data);
return Results.File(reportStream, "application/pdf", $"{fileName}.pdf");
});
app.Run();

View File

@@ -0,0 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61601",
"sslPort": 44358
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5155",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7054;http://localhost:5155",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

21
PdfService/Reporter.cs Normal file
View File

@@ -0,0 +1,21 @@
using Stimulsoft.Report;
using Stimulsoft.Report.Export;
namespace PdfService;
public static class Reporter
{
public static Stream Print(string reportTemplate, object data)
{
var report = new StiReport();
report.LoadFromString(reportTemplate);
report.RegBusinessObject("Data", data);
report.Render(false);
var stream = new MemoryStream();
report.ExportDocument(StiExportFormat.Pdf ,stream);
stream.Position = 0;
return stream;
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}