using Newtonsoft.Json; using StackExchange.Redis; using System.Reflection; using WebSupergoo.ABCpdf11; using static PDFMonitor_SVG.Entities; namespace PDFMonitor_SVG { public partial class FormMain : Form { private ConnectionMultiplexer? redisClient; private string? redisServerUrl; private string? redisServerPwd; private string? redisTaskKey; private string? redisCallbackKey; //生成的PDF文件目录 private string? completedPDFPath; //private FixedThreadPool threadPool; private SynchronizationContext context; public FormMain() { InitializeComponent(); ReadServerConfig(); try { redisClient = ConnectionMultiplexer.Connect(redisServerUrl + ",password=" + redisServerPwd); memoLog.Items.Add("redis连接成功"); } catch (Exception ex) { memoLog.Items.Add("redis连接失败,请检查程序设置"); memoLog.Items.Add(ex.Message); } //threadPool = new FixedThreadPool(8); //默认8个线程 ThreadPool.SetMinThreads(4, 4); ThreadPool.SetMaxThreads(8, 8); // 获取当前的SynchronizationContext context = SynchronizationContext.Current; WebSupergoo.ABCpdf11.XSettings.InstallLicense("X/VKS0cMn8tAun4hGNvFONyWaelyItt2pqrH5srEKm4brmiSKC69N5FsGmRijGFheK9mhXoi/HVdi/VrNv0Vv1RyfQWQCg=="); } ~FormMain() { redisClient?.Dispose(); //threadPool.Shutdown(); } private void ReadServerConfig() { string iniPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\PDFMonitor_SVG.ini"; IniFile ini = new(iniPath); redisServerUrl = ini.Read("redis", "redisServerUrl", "127.0.0.1:6379"); redisServerPwd = ini.Read("redis", "redisServerPwd", "Admin@dounengyin@123"); redisTaskKey = ini.Read("redis", "redisTaskKey", "sdtool:mall:designer:svg:task:list:1000"); redisCallbackKey = ini.Read("redis", "redisCallbackKey", "sdtool:mall:designer:pdf:callback:list:1000"); completedPDFPath = ini.Read("path", "completedPDFPath", "D:\\testPdfOut\\"); if (!Directory.Exists(completedPDFPath)) { Directory.CreateDirectory(completedPDFPath); } } private void TimerGetTask_Tick(object sender, EventArgs e) { RedisValue taskInfo = redisClient.GetDatabase().ListLeftPop(redisTaskKey); if (!taskInfo.IsNullOrEmpty) { // 有任务了,干活 SVGTaskInfo svgTaskInfo = JsonConvert.DeserializeObject(taskInfo); if (svgTaskInfo == null) return; memoLog.Items.Add("获取任务[" + svgTaskInfo.SvgFileName + "]成功"); //todo //threadPool.Execute(() => { // doConvertTask(svgTaskInfo); //}); ThreadPool.QueueUserWorkItem(new WaitCallback(doConvertTask), svgTaskInfo); } } private void doConvertTask(object state) { SVGTaskInfo svgTaskInfo = (SVGTaskInfo)state; try { context.Post(_ => { memoLog.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 任务[" + svgTaskInfo.SvgFileName + "]开始"); }, null); //传过来的是px,转成mm int fileWidth = px2mm(double.Parse(svgTaskInfo.Width)); int fileHeight = px2mm(double.Parse(svgTaskInfo.Height)); int fileWidthPx = (int)Math.Ceiling(double.Parse(svgTaskInfo.Width)); Doc doc = new Doc(); doc.Units = UnitType.Mm; //doc.MediaBox.Top = 0; //doc.MediaBox.Left = 0; //doc.MediaBox.Right = fileWidth; //doc.MediaBox.Bottom = fileHeight; string mediaBoxStr = "0 0 " + fileWidth + " " + fileHeight; doc.MediaBox.String = mediaBoxStr; doc.MediaBox.Pin = XRect.Corner.TopLeft; doc.Rect.String = doc.MediaBox.String; doc.Rect.Pin = XRect.Corner.TopLeft; doc.HtmlOptions.Media = MediaType.Print; string convertedFileName = Path.ChangeExtension(svgTaskInfo.SvgFileName, ".pdf"); string convertedFilePath = completedPDFPath + convertedFileName; doc.AddImageUrl(svgTaskInfo.SvgPath, true, fileWidthPx, true); doc.Save(convertedFilePath); doc.Clear(); doc.Dispose(); PDFCallbackInfo pdfCallbackInfo = new PDFCallbackInfo(); pdfCallbackInfo.IdDesignOrder = svgTaskInfo.IdDesignOrder; pdfCallbackInfo.PdfFileName = convertedFileName; redisClient.GetDatabase().ListRightPush(redisCallbackKey, JsonConvert.SerializeObject(pdfCallbackInfo)); context.Post(_ => { memoLog.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 任务[" + svgTaskInfo.SvgFileName + "]完成"); }, null); } catch (Exception ex) { context.Post(_ => { memoLog.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 任务[" + svgTaskInfo.SvgFileName + "]失败"); memoLog.Items.Add($"{ex.Message}"); }, null); } } private void btnStartService_Click(object sender, EventArgs e) { if (redisClient == null) { memoLog.Items.Add("redis客户端未连接,请修正程序设置后重启"); return; } memoLog.Items.Add("服务已启动"); timerGetTask.Start(); } private void btnStopService_Click(object sender, EventArgs e) { timerGetTask.Stop(); memoLog.Items.Add("服务已停止"); } } }