Hello!
I am working on implementing HTML to PDF conversions in my .NET10 service. I got it working in my local environment (Windows) and even in a local Docker container, but when I push my changes to a feature branch, the same functionality fails in the k8s pod. The code I’m using is below.
Simple html to pdf function for testing:
public bool TestHtmlToPdf() {
try {
var htmlContent = "<html><body>Hello World!</body></html>";
var htmlBytes = System.Text.Encoding.UTF8.GetBytes(htmlContent);
var htmlStream = new MemoryStream(htmlBytes);
var pdfStream = new MemoryStream();
var htmlLoadOptions = new HtmlLoadOptions();
var document = DocumentModel.Load(htmlStream, htmlLoadOptions);
document.Save(pdfStream, SaveOptions.PdfDefault);
return true;
} catch (Exception e) {
Console.WriteLine(e);
return false;
}
}
I am providing custom font files in my Program.cs file like this:
var path = builder.Environment.IsDevelopment() ? "" : AppContext.BaseDirectory;
var fontsFolder = Path.Combine(path, "Fonts");
//load fonts used for PDF operations
if (Directory.Exists(fontsFolder)) {
FontSettings.FontsBaseDirectory = fontsFolder;
} else {
throw new DirectoryNotFoundException($"Fonts directory not found at: {fontsFolder}");
}
I confirmed the font files are present in the pod with this output:
Listing font files: │
│ /app/Fonts/calibrib.ttf │
│ /app/Fonts/calibri.ttf │
│ /app/Fonts/arialbd.ttf │
│ /app/Fonts/arial.ttf │
│ /app/Fonts/timesi.ttf │
│ /app/Fonts/LibreBarcode39-Regular.ttf │
│ /app/Fonts/IDAutomationC39XS.ttf │
│ /app/Fonts/timesbd.ttf │
│ /app/Fonts/times.ttf │
│ /app/Fonts/timesbi.ttf
However, when the TestHtmlToPdf function gets called, this exception gets thrown on document.Save():
│ System.IndexOutOfRangeException: Index was outside the bounds of the array. ││ at .() │
│ at .() ││ at z6..( , Int32 ) │
│ at z6.( , UInt32 ) ││ at Y6.(Int32 ) │
│ at Y6.(UInt32 ) ││ at Y6.[TTable]( ) │
│ at Y6. ) ││ at .(`1& , ) │
│ at .() ││ at .(Boolean , IEnumerable`1& , IEnumerable`1& , Boolean& , Boolean& ) │
│ at ..ctor( ) ││ at .() │
│ at ..ctor(IList`1 ) ││ at F6.(Uri , ) │
│ at `2.[TArg]( , Func`3 , TArg ) ││ at F6.(Uri , ) │
│ at F6.( , ) ││ at F6.( , Object ) │
│ at `2.[TArg]( , Func`3 , TArg ) ││ at F6.( ) │
│ at .( ) ││ at .( , & ) │
│ at .(Uri , String , & ) ││ at .( , Boolean& , & ) │
│ at .( , Int32 , & ) ││ at U6.( , Int32 ) │
│ at U6.(String , Boolean , Boolean , Int32 ) ││ at .(CharacterFormat , , , U6 , Int32 ) │
│ at .(CharacterFormat , , , U6 ) ││ at .( ) │
│ at .() ││ at .() │
│ at GemBox.Document.DocumentModel.GetPaginator(PaginatorOptions options, IProgressTracker progressTracker, FormattedText formattedTe ││ at GemBox.Document.PdfSaveOptions.Save(DocumentModel , Stream , String ) │
│ at GemBox.Document.SaveOptions.67tg5rkvl9zwp8v7723mndgzuc3b4vj8(DocumentModel , Stream , String , Boolean ) ││ at GemBox.Document.DocumentModel.Save(Stream stream, SaveOptions options)
I am assuming this has something to do with the font files but I’m not completely sure. The same code works in every other environment so I was just hoping for some insight into what might be going wrong here. I can provide any other snippets needed.
Appreciate any help!