Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix PDF document generation to properly support emoji fonts
Co-authored-by: yufeih <[email protected]>
  • Loading branch information
Copilot and yufeih committed May 21, 2025
commit 60c92e8d73a1e4cc3d0debb64bb2f178c2e98e8f
17 changes: 13 additions & 4 deletions src/Docfx.App/PdfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ static async Task CreatePdf(
using var builder = new PdfDocumentBuilder(output);

builder.DocumentInformation = new() { Producer = producer };

// Try to load emoji fonts
string? emojiFontPath = TryLoadEmojiFont(builder);
if (!string.IsNullOrEmpty(emojiFontPath))
{
builder.DocumentInformation.Keywords += $" emoji-font: {Path.GetFileName(emojiFontPath)}";
}

builder.Bookmarks = CreateBookmarks(outline.items);

await MergePdf();
Expand Down Expand Up @@ -405,9 +413,6 @@ async Task MergePdf()
var pageNumber = 0;
var font = builder.AddStandard14Font(UglyToad.PdfPig.Fonts.Standard14Fonts.Standard14Font.Helvetica);

// Try to use an emoji font if specified via environment variable
TryLoadEmojiFont(builder);

foreach (var (url, node) in pages)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -703,7 +708,8 @@ private static StringComparison GetStringComparison()
}

// Try to load an emoji font from the environment variable or known paths
private static void TryLoadEmojiFont(PdfDocumentBuilder builder)
// Returns the path of the loaded font, or null if no font was loaded
private static string? TryLoadEmojiFont(PdfDocumentBuilder builder)
{
// First, check if a font is specified via environment variable
var emojiFontPath = Environment.GetEnvironmentVariable(EmojiFontPathEnvVar);
Expand All @@ -729,12 +735,15 @@ private static void TryLoadEmojiFont(PdfDocumentBuilder builder)
// Load the font as a TrueType font
builder.AddTrueTypeFont(File.ReadAllBytes(emojiFontPath));
Logger.LogInfo($"Loaded emoji font from {emojiFontPath}");
return emojiFontPath;
}
catch (Exception ex)
{
// Log error but continue with standard fonts if emoji font loading fails
Logger.LogWarning($"Failed to load emoji font from {emojiFontPath}: {ex.Message}");
}
}

return null;
}
}