From 48b778c95a6c9622e3c6998395ab46dfd7435685 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Thu, 30 Nov 2023 13:33:51 +0800 Subject: [PATCH] =?UTF-8?q?linux=20=E4=B8=8B=E9=80=82=E9=85=8D=20=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helper/FileConvertHelper.cs | 25 ++++++++++ .../Document/TrialEmailNoticeConfigService.cs | 47 ++++++++++++++----- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/IRaCIS.Core.Application/Helper/FileConvertHelper.cs b/IRaCIS.Core.Application/Helper/FileConvertHelper.cs index c79ec4916..fa6aa2324 100644 --- a/IRaCIS.Core.Application/Helper/FileConvertHelper.cs +++ b/IRaCIS.Core.Application/Helper/FileConvertHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,5 +11,29 @@ namespace IRaCIS.Core.Application.Helper { + static public void ConvertWordToPdf(string inputWordFilePath, string outputPdfDir) + { + // 设置 libreoffice 命令行参数 + string arguments = $"--headless --invisible --convert-to pdf \"{inputWordFilePath}\" --outdir \"{outputPdfDir}\""; + + // 启动 libreoffice 进程 + using (Process process = new Process()) + { + process.StartInfo.FileName = "libreoffice"; + process.StartInfo.Arguments = arguments; + process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.RedirectStandardError = true; + process.StartInfo.UseShellExecute = false; + process.StartInfo.CreateNoWindow = true; + + process.Start(); + + // 等待进程结束 + process.WaitForExit(); + } + } + + } } diff --git a/IRaCIS.Core.Application/Service/Document/TrialEmailNoticeConfigService.cs b/IRaCIS.Core.Application/Service/Document/TrialEmailNoticeConfigService.cs index 7dbdc7716..a29e68e9e 100644 --- a/IRaCIS.Core.Application/Service/Document/TrialEmailNoticeConfigService.cs +++ b/IRaCIS.Core.Application/Service/Document/TrialEmailNoticeConfigService.cs @@ -24,6 +24,8 @@ using Spire.Doc; using Microsoft.AspNetCore.Http; using System; using System.Runtime.InteropServices; +using SharpCompress.Common; +using SkiaSharp; namespace IRaCIS.Core.Application.Service { @@ -59,6 +61,7 @@ namespace IRaCIS.Core.Application.Service IRepository subjectVisitRepository, IRepository trialEmailBlackUserRepository, IRepository emailNoticeConfigRepository +, IEmailSendService emailSendService ) { _trialEmailNoticeConfigRepository = trialEmailNoticeConfigRepository; @@ -812,7 +815,7 @@ namespace IRaCIS.Core.Application.Service }); - await SendEmailHelper.SendEmailAsync(sendEmailConfig); + await SendEmailHelper.SendEmailAsync(sendEmailConfig); return string.Empty; } @@ -851,9 +854,17 @@ namespace IRaCIS.Core.Application.Service } else { + var wordStoreServerPath = Path.Combine(Path.GetDirectoryName(serverFilePath), Path.GetFileNameWithoutExtension(serverFilePath)+".docx"); + + using (FileStream fileStream = new FileStream(wordStoreServerPath, FileMode.Create, FileAccess.Write)) + { + wordMemoryStream.WriteTo(fileStream); + } + FileConvertHelper.ConvertWordToPdf(wordStoreServerPath, Path.GetDirectoryName(serverFilePath)); + File.Delete(wordStoreServerPath); } - + } @@ -871,28 +882,42 @@ namespace IRaCIS.Core.Application.Service { - MemoryStream memoryStream = new MemoryStream(); - MemoryStream pdfMemoryStream = new MemoryStream(); + MemoryStream wordMemoryStream = new MemoryStream(); + MemoryStream pdfMemoryStream = new MemoryStream(); - MiniSoftware.MiniWord.SaveAsByTemplate(memoryStream, path, value); + MiniSoftware.MiniWord.SaveAsByTemplate(wordMemoryStream, path, value); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Document document = new Document(); - document.LoadFromStream(memoryStream, FileFormat.Docx); + document.LoadFromStream(wordMemoryStream, FileFormat.Docx); document.SaveToStream(pdfMemoryStream, FileFormat.PDF); + } else { + var wordStoreServerPath = Path.Combine(Path.GetDirectoryName(serverFilePath), Path.GetFileNameWithoutExtension(serverFilePath) + ".docx"); + + using (FileStream fileStream = new FileStream(wordStoreServerPath, FileMode.Create, FileAccess.Write)) + { + wordMemoryStream.WriteTo(fileStream); + } + FileConvertHelper.ConvertWordToPdf(wordStoreServerPath, Path.GetDirectoryName(serverFilePath)); + + File.Delete(wordStoreServerPath); + + using (FileStream fileStream = new FileStream(serverFilePath, FileMode.Open, FileAccess.Read)) + { + fileStream.CopyTo(pdfMemoryStream); + } } - - pdfMemoryStream.Seek(0, SeekOrigin.Begin); - sendEmailConfig.EmailAttachMentConfigList.Add(new EmailAttachMentConfig() - { - FileName = $"{taskInfo.SubjectCode}_{Path.GetFileNameWithoutExtension(_userInfo.IsEn_Us ? emailConfig.AttachName : emailConfig.AttachNameCN)}.pdf", + pdfMemoryStream.Seek(0, SeekOrigin.Begin); + sendEmailConfig.EmailAttachMentConfigList.Add(new EmailAttachMentConfig() + { + FileName = $"{taskInfo.SubjectCode}_{Path.GetFileNameWithoutExtension(_userInfo.IsEn_Us ? trialEmailConfig.AttachName : trialEmailConfig.AttachNameCN)}.pdf", FileStream = pdfMemoryStream });