Kaynağa Gözat

导出pdf临时实现方案

xf 3 ay önce
ebeveyn
işleme
6b61749551

+ 1 - 1
src/Hotline.Api/Controllers/KnowledgeController.cs

@@ -846,7 +846,7 @@ namespace Hotline.Api.Controllers
             //return _baseDataApplication
             //    .FileType(EFileType.excel | EFileType.pdf)
             //    .Build();
-            var ignoreFileType = EFileType.excel | EFileType.pdf;
+            var ignoreFileType = EFileType.excel ;//| EFileType.pdf;
             var items = EnumExts.GetDescriptions<EFileType>();
             var filteredDictionary = items
                  .Where(kvp => (ignoreFileType & (EFileType)kvp.Key) == 0)

+ 23 - 2
src/Hotline.Application/Tools/StringExtensions.cs

@@ -1,5 +1,7 @@
 using Hotline.Application.ExportWord;
+using Hotline.Pdf;
 using Hotline.Share.Enums.Article;
+using XF.Domain.Exceptions;
 
 namespace Hotline.Application.Tools;
 public static class StringExtensions
@@ -14,9 +16,28 @@ public static class StringExtensions
         return WordHelper.ConvertHtmlToPdf(value);
     }
 
-    public static Stream HtmlToStream(this string value, EFileType fileType)
+    public static Stream HtmlToPDF(this string value, string? title, IPdfManager? pdfManager)
     {
-        if (fileType == EFileType.pdf) return value.HtmlToPDF();
+        if (pdfManager is null)
+            throw new UserFriendlyException($"非法参数: {nameof(pdfManager)}");
+        var content = value
+                .Replace("&nbsp;", " ")
+                .Replace("&amp;", "&")
+                .Replace("&quot;", "\"")
+                .Replace("&gt;", ">")
+                .Replace("&lt;", "<")
+            ;
+
+        var stream = new MemoryStream();
+        pdfManager.GeneratePdf(title, content, stream);
+        stream.Seek(0, SeekOrigin.Begin);
+        return stream;
+    }
+
+    public static Stream HtmlToStream(this string value, EFileType fileType, string? title = null, IPdfManager? pdfManager = null)
+    {
+        //if (fileType == EFileType.pdf) return value.HtmlToPDF();
+        if (fileType == EFileType.pdf) return value.HtmlToPDF(title, pdfManager);
         if (fileType == EFileType.word) return value.HtmlToWord();
         throw new NotImplementedException($"无效的 fileType 入参: {fileType}");
     }