From de7f960603d9795e0e1fb4e43252eaf83e706f6f Mon Sep 17 00:00:00 2001 From: alibw Date: Wed, 30 Apr 2025 18:04:10 +0330 Subject: [PATCH] init --- .gitignore | 5 +++ PdfService.sln | 16 ++++++++ PdfService/PdfService.csproj | 15 +++++++ PdfService/Program.cs | 48 +++++++++++++++++++++++ PdfService/Properties/launchSettings.json | 38 ++++++++++++++++++ PdfService/Reporter.cs | 21 ++++++++++ PdfService/appsettings.Development.json | 8 ++++ PdfService/appsettings.json | 9 +++++ 8 files changed, 160 insertions(+) create mode 100644 .gitignore create mode 100644 PdfService.sln create mode 100644 PdfService/PdfService.csproj create mode 100644 PdfService/Program.cs create mode 100644 PdfService/Properties/launchSettings.json create mode 100644 PdfService/Reporter.cs create mode 100644 PdfService/appsettings.Development.json create mode 100644 PdfService/appsettings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/PdfService.sln b/PdfService.sln new file mode 100644 index 0000000..5c27e54 --- /dev/null +++ b/PdfService.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdfService", "PdfService\PdfService.csproj", "{D3E404EC-0CF7-44B7-B08E-B32825C4B9EC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D3E404EC-0CF7-44B7-B08E-B32825C4B9EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D3E404EC-0CF7-44B7-B08E-B32825C4B9EC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D3E404EC-0CF7-44B7-B08E-B32825C4B9EC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D3E404EC-0CF7-44B7-B08E-B32825C4B9EC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/PdfService/PdfService.csproj b/PdfService/PdfService.csproj new file mode 100644 index 0000000..240bb69 --- /dev/null +++ b/PdfService/PdfService.csproj @@ -0,0 +1,15 @@ + + + + net8.0-windows + enable + enable + true + + + + + + + + diff --git a/PdfService/Program.cs b/PdfService/Program.cs new file mode 100644 index 0000000..845c0f3 --- /dev/null +++ b/PdfService/Program.cs @@ -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(); \ No newline at end of file diff --git a/PdfService/Properties/launchSettings.json b/PdfService/Properties/launchSettings.json new file mode 100644 index 0000000..7dce6be --- /dev/null +++ b/PdfService/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/PdfService/Reporter.cs b/PdfService/Reporter.cs new file mode 100644 index 0000000..d07536d --- /dev/null +++ b/PdfService/Reporter.cs @@ -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; + } +} \ No newline at end of file diff --git a/PdfService/appsettings.Development.json b/PdfService/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/PdfService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/PdfService/appsettings.json b/PdfService/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/PdfService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}