diff --git a/New_College.Api/Controllers/Front/PcVolunteerController.cs b/New_College.Api/Controllers/Front/PcVolunteerController.cs index 417be48..5f11983 100644 --- a/New_College.Api/Controllers/Front/PcVolunteerController.cs +++ b/New_College.Api/Controllers/Front/PcVolunteerController.cs @@ -2,29 +2,20 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using New_College.Model; using New_College.Model.ViewModels; using New_College.IServices; using New_College.Model.Models; -using New_College.Services; using System.Linq.Expressions; using New_College.IRepository.UnitOfWork; -using New_College.Repository.UnitOfWork; using SqlSugar; using New_College.Common; using Microsoft.AspNetCore.Authorization; using StackExchange.Profiling.Internal; using Newtonsoft.Json; -using NPOI.SS.Formula.Functions; -using Microsoft.AspNetCore.Server.IIS.Core; -using New_College.Common.WebApiClients.HttpApis; -using NPOI.SS.UserModel; using System.IO; using MiniExcelLibs; -using OSS.Common.Extension; -using System.Security.Cryptography; namespace New_College.Api.Controllers.Front { @@ -217,6 +208,31 @@ namespace New_College.Api.Controllers.Front } + /// + /// + /// + /// + /// + //[HttpGet("generate")] + //public IActionResult Generate([FromQuery] MinExcelAttrDto dto) + //{ + //string[] unversityheaders = { "志愿", "概率", "院校代码", "院校名称", "24招生计划", "历年", "2023", "2022", "2021" }; + //var stream = PdfOptHelper.CreatePdf("test", unversityheaders.ToList(), new List() { + // new PdfOptHelper.dataModel() + // { id=1, + // history="录取人数\n计划人数\n最低分\n最低位次\n效位差", + // planName="A222", + // probability="90%", + // universityCode="A99", + // universityName="中科大大家四大石窟记得哈就开始大肆扩大数据库", + // years=new List(){"2023", "2022", "2021" } + // } + //}); + + //// 返回 PDF 文件 + //return File(stream.ToArray(), "application/pdf", "table_example_with_logo_and_title.pdf"); + + //} [HttpGet("Export")] public async Task Export([FromQuery] MinExcelAttrDto dto) { diff --git a/New_College.Api/Controllers/HealthCheckController.cs b/New_College.Api/Controllers/HealthCheckController.cs index 3f72748..93f49d6 100644 --- a/New_College.Api/Controllers/HealthCheckController.cs +++ b/New_College.Api/Controllers/HealthCheckController.cs @@ -3,6 +3,7 @@ using New_College.Common; using New_College.Common.Helper; using New_College.IServices; using New_College.Services; +using System.IO; using System.Threading.Tasks; namespace New_College.Controllers @@ -83,5 +84,9 @@ namespace New_College.Controllers return true; } - } + + + + + } } \ No newline at end of file diff --git a/New_College.Api/New_College.Api.csproj b/New_College.Api/New_College.Api.csproj index c5ea718..6742fef 100644 --- a/New_College.Api/New_College.Api.csproj +++ b/New_College.Api/New_College.Api.csproj @@ -109,6 +109,12 @@ + + + Always + + + diff --git a/New_College.Api/New_College.xml b/New_College.Api/New_College.xml index 4b93dee..c68a440 100644 --- a/New_College.Api/New_College.xml +++ b/New_College.Api/New_College.xml @@ -688,6 +688,13 @@ 志愿表Id + + + + + + + PC志愿表新增 diff --git a/New_College.Api/wwwroot/fonts/NotoSansCJKsc-Regular.otf b/New_College.Api/wwwroot/fonts/NotoSansCJKsc-Regular.otf new file mode 100644 index 0000000..3b56ab3 Binary files /dev/null and b/New_College.Api/wwwroot/fonts/NotoSansCJKsc-Regular.otf differ diff --git a/New_College.sln b/New_College.sln index 2b7ebde..e47e8fc 100644 --- a/New_College.sln +++ b/New_College.sln @@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "New_College.AdminMvc", "New EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "New_Spider", "New_Spider\New_Spider.csproj", "{E23857BF-DFBB-49DD-A86A-1B2932F6D33B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PDFGenerateApi", "PDFGenerateApi\PDFGenerateApi.csproj", "{CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -87,6 +89,10 @@ Global {E23857BF-DFBB-49DD-A86A-1B2932F6D33B}.Debug|Any CPU.Build.0 = Debug|Any CPU {E23857BF-DFBB-49DD-A86A-1B2932F6D33B}.Release|Any CPU.ActiveCfg = Release|Any CPU {E23857BF-DFBB-49DD-A86A-1B2932F6D33B}.Release|Any CPU.Build.0 = Release|Any CPU + {CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CEBFD10D-AAB8-4DCD-94B4-9879532BA45C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PDFGenerateApi/Controllers/WeatherForecastController.cs b/PDFGenerateApi/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..6e91911 --- /dev/null +++ b/PDFGenerateApi/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace PDFGenerateApi.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/PDFGenerateApi/PDFGenerateApi.csproj b/PDFGenerateApi/PDFGenerateApi.csproj new file mode 100644 index 0000000..5317631 --- /dev/null +++ b/PDFGenerateApi/PDFGenerateApi.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + + + + Always + + + Always + + + + diff --git a/PDFGenerateApi/PdfController.cs b/PDFGenerateApi/PdfController.cs new file mode 100644 index 0000000..4ee2e96 --- /dev/null +++ b/PDFGenerateApi/PdfController.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace PDFGenerateApi +{ + [Route("api/[controller]")] + [ApiController] + public class PdfController : ControllerBase + { + + + + /// + /// + /// + /// + /// + [HttpGet("generate")] + public IActionResult Generate() + { + // 加载中文字体 + string fontsPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "fonts", "AlimamaDongFangDaKai-Regular.otf"); + string[] unversityheaders = { "志愿", "概率", "院校代码", "院校名称", "24招生计划", "历年", "2023", "2022", "2021" }; + var stream = PdfOptHelper.CreatePdf(fontsPath, "test", unversityheaders.ToList(), new List() { + new PdfOptHelper.dataModel() + { id=1, + history="录取人数\n计划人数\n最低分\n最低位次\n效位差", + planName="A222", + probability="90%", + universityCode="A99", + universityName="中科大大家四大石窟记得哈就开始大肆扩大数据库", + years=new List(){"2023", "2022", "2021" } + } + }); + + // 返回 PDF 文件 + return File(stream.ToArray(), "application/pdf", "table_example_with_logo_and_title.pdf"); + + } + + } +} diff --git a/PDFGenerateApi/PdfOptHelper.cs b/PDFGenerateApi/PdfOptHelper.cs new file mode 100644 index 0000000..e3e413e --- /dev/null +++ b/PDFGenerateApi/PdfOptHelper.cs @@ -0,0 +1,244 @@ +using static System.Net.Mime.MediaTypeNames; +using iText.Kernel.Pdf; +using iText.Kernel.Geom; +using iText.IO.Image; +using iText.Layout.Element; +using Image = iText.Layout.Element.Image; +using iText.Layout.Properties; +using iText.Layout; +using iText.Kernel.Font; +using iText.IO.Font; +using iText.Kernel.Colors; + +namespace PDFGenerateApi +{ + public class PdfOptHelper + { + + /// + /// + /// + /// + /// + /// + /// + public static MemoryStream CreatePdf(string fontsPath, string pagetitle, List headers, List dataModels) + { + // 创建内存流 + using (var stream = new MemoryStream()) + { + // 创建 PDF Writer + PdfWriter writer = new PdfWriter(stream); + PdfDocument pdf = new PdfDocument(writer); + // 设置页面大小为横向 A4 + PageSize pageSize = PageSize.A4.Rotate(); + // 设置页面大小为 A4 + pdf.SetDefaultPageSize(pageSize); + Document document = new Document(pdf); + + //if (!File.Exists(fontsPath)) + //{ + // bool aa = true; + // // _logger.LogError($"Font file not found at path: {fontsPath}"); + // // return NotFound("Font file not found."); + //} + PdfFont font = PdfFontFactory.CreateFont(fontsPath, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED); + // 加载 logo 图像 + string logoPath = "https://i5.youzy.cn/util4y/media/files/20231026/7840b6f1f30c413eade15babc70be26e_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230811154443.png"; // 确保 logo.png 放在 wwwroot 目录下 + ImageData imageData = ImageDataFactory.Create(logoPath); + Image logo = new Image(imageData); + logo.SetWidth(124); + logo.SetHeight(40); + // 将 logo 添加到文档中 + document.Add(logo); + Paragraph title = CreateTitle(font, pagetitle); + document.Add(title); + // 添加标题下方内容 + Paragraph subtitle = CreateSubTitle(font, string.Format("最后修改时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"))); + document.Add(subtitle); + // 添加表头 + // string[] unversityheaders = { "志愿", "概率", "院校代码", "院校名称", "24招生计划", "历年", "2023", "2022", "2021" }; + Table universitytable = HeaderTable(font, 9, headers, new List() { }); + document.Add(universitytable); + #region ***********写入表格 + // 创建一个 8列的表格,设置表格宽度为 100% 以适应页面宽度 + // Table universitytable = new Table(9); + // universitytable.SetWidth(UnitValue.CreatePercentValue(100)); + + + //int h = 0; + //foreach (var header in unversityheaders) + //{ + // h++; + // if (h == 4) + // { + // universitytable.AddHeaderCell(new Cell().Add(new Paragraph(header).SetWidth(85).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER))); + // } + // else + // { + // universitytable.AddHeaderCell(new Cell().Add(new Paragraph(header).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER))); + // } + //} + + //// 可选:添加表格数据,这里仅作为示例 + //for (int i = 1; i <= 1; i++) + //{ + // universitytable.AddCell(new Cell().Add(new Paragraph(i.ToString()).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // universitytable.AddCell(new Cell().Add(new Paragraph("0.9").SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // universitytable.AddCell(new Cell().Add(new Paragraph("A999").SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // universitytable.AddCell(new Cell().Add(new Paragraph("山东师范大学(本科)(山东公办)省重点 保研 研究生院 省部共建山东 济南市/综合/公办/排名190" + i).SetWidth(85).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // universitytable.AddCell(new Cell().Add(new Paragraph("100").SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + + // universitytable.AddCell(new Cell().Add(new Paragraph("历史数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // universitytable.AddCell(new Cell().Add(new Paragraph("2023数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // universitytable.AddCell(new Cell().Add(new Paragraph("2022数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // universitytable.AddCell(new Cell().Add(new Paragraph("2021数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + + //} + //document.Add(universitytable); + //// float[] columnWidths = { 0.5f, 0.5f, 1, 2, 1, 1, 1, 1, 1 }; + //Table majortable = new Table(9); + //majortable.SetWidth(UnitValue.CreatePercentValue(100)); + //// 添加表头 + //string[] majorlistheaders = { "序号", "概率", "专业代码", "专业名称", "24招生计划", "历年", "2023", "2022", "2021" }; + //foreach (var header in majorlistheaders) + //{ + // majortable.AddHeaderCell(new Cell().Add(new Paragraph(header).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER))); + //} + + + //for (int i = 1; i <= 5; i++) + //{ + // majortable.AddCell(new Cell().Add(new Paragraph(i.ToString()).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // majortable.AddCell(new Cell().Add(new Paragraph("0.9").SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // majortable.AddCell(new Cell().Add(new Paragraph("QL").SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // majortable.AddCell(new Cell().Add(new Paragraph("垫子信息技术" + i).SetWidth(85).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // majortable.AddCell(new Cell().Add(new Paragraph("20").SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + + // majortable.AddCell(new Cell().Add(new Paragraph("录取人数\n计划人数\n最低分\n最低位次\n效位差" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // majortable.AddCell(new Cell().Add(new Paragraph("2023数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // majortable.AddCell(new Cell().Add(new Paragraph("2022数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + // majortable.AddCell(new Cell().Add(new Paragraph("2021数据" + i).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + + //} + //document.Add(majortable); + #endregion + // 将表格添加到文档中 + // 关闭文档 + document.Close(); + // 返回 PDF 文件 + return stream; + } + + } + + /// + /// 标题 + /// + /// + /// + /// + private static Paragraph CreateTitle(PdfFont font, string titletext) + { + Paragraph title = new Paragraph(titletext) + .SetTextAlignment(TextAlignment.CENTER) + .SetFont(font) + .SetBold() + .SetFontSize(16) + .SetMarginTop(20) + .SetMarginBottom(5); + return title; + } + + /// + /// 创建副标 + /// + /// + /// + /// + private static Paragraph CreateSubTitle(PdfFont font, string titletext) + { + Paragraph title = new Paragraph(titletext) + .SetTextAlignment(TextAlignment.CENTER) + .SetFont(font) + .SetBold() + .SetFontSize(16) + .SetMarginTop(20) + .SetMarginBottom(5); + return title; + } + + + /// + /// table表头 + /// + /// + /// + /// + private static Table HeaderTable(PdfFont font, int tb, List headers, List dataList) + { + Table table = new Table(tb); + table.SetWidth(UnitValue.CreatePercentValue(100)); + + + int h = 0; + foreach (var header in headers) + { + h++; + if (h == 4) + { + table.AddHeaderCell(new Cell().Add(new Paragraph(header).SetWidth(85).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER))); + } + else + { + table.AddHeaderCell(new Cell().Add(new Paragraph(header).SetFont(font).SetBold().SetTextAlignment(TextAlignment.CENTER))); + } + } + dataList.ForEach(a => + { + table.AddCell(new Cell().Add(new Paragraph(a.id.ToString()).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + table.AddCell(new Cell().Add(new Paragraph(a.probability).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + table.AddCell(new Cell().Add(new Paragraph(a.universityCode).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + table.AddCell(new Cell().Add(new Paragraph(a.universityName).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + table.AddCell(new Cell().Add(new Paragraph(a.planName).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + table.AddCell(new Cell().Add(new Paragraph(a.history).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + a.years.ForEach(a => + { + table.AddCell(new Cell().Add(new Paragraph(a).SetFont(font).SetTextAlignment(TextAlignment.CENTER))); + }); + }); + return table; + } + + + public class dataModel + { + public int id { get; set; } + + /// + /// + /// + public string probability { get; set; } + + /// + /// + /// + public string universityCode { get; set; } + + /// + /// + /// + public string universityName { get; set; } + + + public string planName { get; set; } + + + public string history { get; set; } + + public List years { get; set; } + + + } + } +} diff --git a/PDFGenerateApi/Program.cs b/PDFGenerateApi/Program.cs new file mode 100644 index 0000000..478b705 --- /dev/null +++ b/PDFGenerateApi/Program.cs @@ -0,0 +1,15 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/PDFGenerateApi/Properties/launchSettings.json b/PDFGenerateApi/Properties/launchSettings.json new file mode 100644 index 0000000..3d8d8e1 --- /dev/null +++ b/PDFGenerateApi/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:48748", + "sslPort": 0 + } + }, + "profiles": { + "PDFGenerateApi": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5092", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/PDFGenerateApi/WeatherForecast.cs b/PDFGenerateApi/WeatherForecast.cs new file mode 100644 index 0000000..90224c3 --- /dev/null +++ b/PDFGenerateApi/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace PDFGenerateApi +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} diff --git a/PDFGenerateApi/appsettings.Development.json b/PDFGenerateApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/PDFGenerateApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/PDFGenerateApi/appsettings.json b/PDFGenerateApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/PDFGenerateApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/PDFGenerateApi/wwwroot/fonts/AlimamaDongFangDaKai-Regular.otf b/PDFGenerateApi/wwwroot/fonts/AlimamaDongFangDaKai-Regular.otf new file mode 100644 index 0000000..8136b2e Binary files /dev/null and b/PDFGenerateApi/wwwroot/fonts/AlimamaDongFangDaKai-Regular.otf differ diff --git a/PDFGenerateApi/wwwroot/fonts/NotoSansCJKsc-Regular.otf b/PDFGenerateApi/wwwroot/fonts/NotoSansCJKsc-Regular.otf new file mode 100644 index 0000000..3b56ab3 Binary files /dev/null and b/PDFGenerateApi/wwwroot/fonts/NotoSansCJKsc-Regular.otf differ