Browse Source

Support custom `FontProvider` in `ITextHtmlToPdfRenderer`.

pull/23529/head
maliming 1 year ago
parent
commit
591aa86a58
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 41
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/Projects/Pdf/IText/ITextHtmlToPdfRenderer.cs

41
modules/docs/src/Volo.Docs.Domain/Volo/Docs/Projects/Pdf/IText/ITextHtmlToPdfRenderer.cs

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using iText.Html2pdf; using iText.Html2pdf;
using iText.Kernel.Pdf; using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action; using iText.Kernel.Pdf.Action;
using iText.Layout.Font;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Docs.Utils; using Volo.Docs.Utils;
@ -14,13 +15,13 @@ namespace Volo.Docs.Projects.Pdf.IText;
public class ITextHtmlToPdfRenderer : IHtmlToPdfRenderer, ITransientDependency public class ITextHtmlToPdfRenderer : IHtmlToPdfRenderer, ITransientDependency
{ {
protected IOptions<DocsProjectPdfGeneratorOptions> Options { get; } protected IOptions<DocsProjectPdfGeneratorOptions> Options { get; }
public ITextHtmlToPdfRenderer(IOptions<DocsProjectPdfGeneratorOptions> options) public ITextHtmlToPdfRenderer(IOptions<DocsProjectPdfGeneratorOptions> options)
{ {
Options = options; Options = options;
} }
public virtual Task<Stream> RenderAsync(string title, string html, List<PdfDocument> documents) public virtual async Task<Stream> RenderAsync(string title, string html, List<PdfDocument> documents)
{ {
var pdfStream = new MemoryStream(); var pdfStream = new MemoryStream();
using (var pdfWriter = new PdfWriter(pdfStream)) using (var pdfWriter = new PdfWriter(pdfStream))
@ -29,33 +30,43 @@ public class ITextHtmlToPdfRenderer : IHtmlToPdfRenderer, ITransientDependency
using (var pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter)) using (var pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter))
{ {
pdfDocument.GetDocumentInfo().SetTitle(title); pdfDocument.GetDocumentInfo().SetTitle(title);
CreatePdfFromHtml(html, pdfDocument); await CreatePdfFromHtmlAsync(html, pdfDocument);
AddOutlinesToPdf(pdfDocument, documents); await AddOutlinesToPdfAsync(pdfDocument, documents);
} }
} }
pdfStream.Position = 0; pdfStream.Position = 0;
return Task.FromResult<Stream>(pdfStream); return pdfStream;
} }
private void CreatePdfFromHtml(string html, iText.Kernel.Pdf.PdfDocument pdfDocument) protected virtual async Task CreatePdfFromHtmlAsync(string html, iText.Kernel.Pdf.PdfDocument pdfDocument)
{ {
var converter = new ConverterProperties(); var converter = new ConverterProperties();
var fontProvider = await GetFontProviderAsync();
if (fontProvider != null)
{
converter.SetFontProvider(fontProvider);
}
var tagWorkerFactory = new HtmlIdTagWorkerFactory(pdfDocument); var tagWorkerFactory = new HtmlIdTagWorkerFactory(pdfDocument);
converter.SetTagWorkerFactory(tagWorkerFactory); converter.SetTagWorkerFactory(tagWorkerFactory);
HtmlConverter.ConvertToDocument(html, pdfDocument, converter); HtmlConverter.ConvertToDocument(html, pdfDocument, converter);
tagWorkerFactory.AddNamedDestinations(); tagWorkerFactory.AddNamedDestinations();
} }
private void AddOutlinesToPdf(iText.Kernel.Pdf.PdfDocument pdfDocument, List<PdfDocument> documents) protected virtual Task<FontProvider> GetFontProviderAsync()
{
return Task.FromResult<FontProvider>(null);
}
protected virtual Task AddOutlinesToPdfAsync(iText.Kernel.Pdf.PdfDocument pdfDocument, List<PdfDocument> documents)
{ {
var pdfOutlines = pdfDocument.GetOutlines(false); var pdfOutlines = pdfDocument.GetOutlines(false);
BuildPdfOutlines(pdfOutlines, documents); BuildPdfOutlines(pdfOutlines, documents);
return Task.CompletedTask;
} }
private void BuildPdfOutlines(PdfOutline parentOutline, List<PdfDocument> pdfDocumentNodes) protected virtual Task BuildPdfOutlines(PdfOutline parentOutline, List<PdfDocument> pdfDocumentNodes)
{ {
foreach (var pdfDocumentNode in pdfDocumentNodes) foreach (var pdfDocumentNode in pdfDocumentNodes)
{ {
@ -63,7 +74,7 @@ public class ITextHtmlToPdfRenderer : IHtmlToPdfRenderer, ITransientDependency
{ {
continue; continue;
} }
var outline = parentOutline.AddOutline(pdfDocumentNode.Title); var outline = parentOutline.AddOutline(pdfDocumentNode.Title);
if (!pdfDocumentNode.Id.IsNullOrWhiteSpace()) if (!pdfDocumentNode.Id.IsNullOrWhiteSpace())
{ {
@ -75,5 +86,7 @@ public class ITextHtmlToPdfRenderer : IHtmlToPdfRenderer, ITransientDependency
BuildPdfOutlines(outline, pdfDocumentNode.Children); BuildPdfOutlines(outline, pdfDocumentNode.Children);
} }
} }
return Task.CompletedTask;
} }
} }

Loading…
Cancel
Save