12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using DinkToPdf;
- using DinkToPdf.Contracts;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- namespace TEAMModelOS.SDK.Models.Service
- {
- public static class StudyService
- {
- public static async Task<string> GenPdf(string tId,string areaId, string cname,string sname,string gname,string details, IConverter _converter, AzureStorageFactory _azureStorage,DingDing _dingDing)
- {
- //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
- {
- var html = Constant.html.Replace("{c.cname}", cname).Replace("{c.sname}", sname).Replace("{c.gname}", gname).Replace("{c.details}", details);
- 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 = 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(a);
- await _azureStorage.GetBlobContainerClient("teammodelos").UploadFileByContainer(m, $"{areaId}", $"{tId}/offline-report.pdf", true);
- string url = $"{areaId}/{tId}/offline-report.pdf";
- return url;
- /* 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(File(a, "application/octet-stream", "SimplePdf.pdf"));
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StudyService,GenPdf()\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return null ;
- }
- }
- }
- }
|