Explorar o código

PDF相关操作

zhouj1203@hotmail.com %!s(int64=3) %!d(string=hai) anos
pai
achega
76b670bcf9

+ 106 - 2
TEAMModelOS.SDK/Context/Constant/Constant.cs

@@ -7,7 +7,7 @@ namespace TEAMModelOS.SDK.DI
     public class Constant
     {
         public static readonly List<string> BlobPrefix = new List<string> { "exam", "vote", "survey", "item", "paper", "syllabus", "records", "doc", "image", "res", "video", "audio", "other", "thum", "train", "temp", "jyzx" };
-        public static readonly List<string> ContentPrefix = new List<string> { "doc", "image", "res", "video", "audio", "other"};
+        public static readonly List<string> ContentPrefix = new List<string> { "doc", "image", "res", "video", "audio", "other" };
         public static readonly string TEAMModelOS = "TEAMModelOS";
         public static readonly string ScopeTeacher = "teacher";
         public static readonly string ScopeTmdUser = "tmduser";
@@ -19,8 +19,112 @@ namespace TEAMModelOS.SDK.DI
         public static readonly string Common = "Common";
         public static readonly string Teacher = "Teacher";
         public static readonly string Student = "Student";
-        public static readonly int private_lesson_limit =50;
+        public static readonly int private_lesson_limit = 50;
         public static readonly int private_lesson_expire = 7;
         public static readonly int school_lesson_expire = 7;
+        public static readonly string html = @"
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset=""UTF-8"">
+    <meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">
+    <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
+    <title>校本研修活动完成情况</title>
+    <style>
+        body {
+            font-family: ""fangsong"";
+            display: flex;
+            flex-direction: column;
+            align-items: center;
+        }
+
+        .tch-table,
+        .details-table {
+            width: 100%;
+        }
+
+        .tch-table table {
+            width: 100%;
+            margin: 15px 0;
+            border: 0;
+        }
+
+        .tch-table th {
+            background-color: #c5c5c5;
+            color: #222222;
+             
+        }
+
+
+        .tch-table,
+        .tch-table th,
+        .tch-table td {
+            font-size: 0.95em;
+            text-align: center;
+            padding: 4px;
+            border-collapse: collapse;
+            font-weight: bolder;
+        }
+
+        .tch-table th,
+        .tch-table td {
+            border-bottom: 1px solid #918b8c;
+            border-width: 1px 0 1px 0;
+        }
+
+        .tch-table tr {
+            border: 1px solid #ffffff;
+            height: 3.2em;
+        }
+
+        .title {
+            font-weight: bold;
+            align-self: flex-start;
+            margin: 30px 5px;
+        }
+
+        .title::before {
+            content: """";
+            width: 10px;
+            height: 10px;
+            background: #3a3a3a;
+            display: inline-block;
+            margin-right: 10px;
+        }
+    </style>
+</head>
+
+<body>
+    <h1 style=""text-align:center;font-weight: bold;"">校本研修活动完成情况</h1>
+    <p class=""title"">教师信息</p>
+    <table class=""tch-table"" id=""tchTable"">
+        <tr>
+            <th style=""width: 33.3%;"">教师姓名</th>
+            <th style=""width: 33.3%;"">机构</th>
+            <th style=""width: 33.3%;"">教研组</th>
+        </tr>
+        <tr>
+            <td>{c.stuname}</td>
+            <td>{c.scname}</td>
+            <td>{c.gname}</td>
+        </tr>
+    </table>
+    <p class=""title"">活动明细</p>
+    <table class=""tch-table"" id=""acTable"">
+        <tr>
+            <th style=""width: 25%;"">活动主题</th>
+            <th style=""width: 15%;"">活动类型</th>
+            <th style=""width: 5%;"">任务学时</th>
+            <th style=""width: 5%;"">完成学时</th>
+            <th style=""width: 20%;"">活动时间</th>
+            <th style=""width: 10%;"">活动内容</th>
+            <th style=""width: 10%;"">活动任务</th>
+            <th style=""width: 10%;"">状态</th>
+        </tr>
+        {c.details}
+    </table>
+</body>
+</html>
+";
     }
 }

+ 80 - 2
TEAMModelOS/Controllers/Common/StudyController.cs

@@ -1,4 +1,6 @@
 using Azure.Cosmos;
+using DinkToPdf;
+using DinkToPdf.Contracts;
 using HTEXLib.COMM.Helpers;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
@@ -7,6 +9,7 @@ using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Options;
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
@@ -37,10 +40,10 @@ namespace TEAMModelOS.Controllers.Common
         private readonly Option _option;
         private readonly AzureStorageFactory _azureStorage;
         private readonly AzureRedisFactory _azureRedis;
-
+        private readonly IConverter _converter;
         public IConfiguration _configuration { get; set; }
         private readonly CoreAPIHttpService _coreAPIHttpService;
-        public StudyController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
+        public StudyController(IConverter converter,CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
            IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
         {
             _coreAPIHttpService = coreAPIHttpService;
@@ -52,6 +55,7 @@ namespace TEAMModelOS.Controllers.Common
             _azureStorage = azureStorage;
             _azureRedis = azureRedis;
             _configuration = configuration;
+            _converter = converter;
         }
         /// <summary>
         /// 保存研修信息
@@ -784,5 +788,79 @@ namespace TEAMModelOS.Controllers.Common
             }
             await Task.WhenAll(tasks);
         }
+
+
+        [HttpPost("gen-pdf")]
+        //[Authorize(Roles = Constant.Role_Root)]
+        public IActionResult GenPdf(JsonElement json)
+        {
+            //https://article.itxueyuan.com/JAxOnG
+            //http://t.zoukankan.com/hsiang-p-14608694.html
+            //https://github.com/rdvojmoc/DinkToPdf
+            //https://blog.csdn.net/u011966339/article/details/114964016?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-114964016-blog-118609642.pc_relevant_antiscanv2&spm=1001.2101.3001.4242.1&utm_relevant_index=3
+            
+            try
+            {
+                GlobalSettings globalSettings = new GlobalSettings();
+                globalSettings.ColorMode = ColorMode.Color;
+                globalSettings.Orientation = Orientation.Portrait;
+                globalSettings.PaperSize = PaperKind.A4;
+                globalSettings.Margins = new MarginSettings { Top = 25, Bottom = 25 };
+                globalSettings.Out = @"E:\pdf\test.pdf";
+                ObjectSettings objectSettings = new ObjectSettings();
+                objectSettings.PagesCount = true;
+                objectSettings.HtmlContent = Constant.html;
+                WebSettings webSettings = new WebSettings();
+                webSettings.DefaultEncoding = "utf-8";
+                HeaderSettings headerSettings = new HeaderSettings();
+                headerSettings.FontSize = 15;
+                headerSettings.FontName = "fangsong";
+                headerSettings.Right = "";
+                // headerSettings.Line = true;
+                FooterSettings footerSettings = new FooterSettings();
+                footerSettings.FontSize = 12;
+                footerSettings.FontName = "Ariel";
+                footerSettings.Center = "校本研修活动完成情况([page]/[toPage])页";
+                // footerSettings.Line = true;
+                objectSettings.HeaderSettings = headerSettings;
+                objectSettings.FooterSettings = footerSettings;
+                objectSettings.WebSettings = webSettings;
+                HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument()
+                {
+                    GlobalSettings = globalSettings,
+                    Objects = { objectSettings },
+                };
+                var a = _converter.Convert(htmlToPdfDocument);
+                MemoryStream m = new MemoryStream(a);
+               /* FileStream fs = new FileStream("F:\\1111111111111\\SimplePdf1.pdf", FileMode.Create, FileAccess.Write, FileShare.Read);
+                m.WriteTo(fs);
+                m.Close();
+                fs.Close();*/
+
+                //var doc = new HtmlToPdfDocument()
+                //{
+                //    GlobalSettings = {
+                //        ColorMode = ColorMode.Color,
+                //        Orientation = Orientation.Portrait,
+                //        PaperSize = PaperKind.A4,
+                //        Margins = new MarginSettings() { Top = 10 },
+                //        Out = @"F:\test.pdf",
+                //    },
+                //    Objects = {
+                //        new ObjectSettings()
+                //        {
+                //            Page = "https://zhidao.baidu.com/question/175696719173618004.html",
+                //        },
+                //    }
+                //};
+                //_converter.Convert(doc);
+                return Ok();
+                // return Ok(File(a, "application/octet-stream", "SimplePdf.pdf"));
+            }
+            catch (Exception ex)
+            {
+                return BadRequest(new { ex = ex.StackTrace, exmsg = ex.Message });
+            }
+        }
     }
 }