Hi, I am using Aspose.PDF for dotnet in order to convert HTML to PDF. I downloaded a temporary license to compare conversion times in order to see what is better for my project.
I have no problems converting locally, but after deploying to AWS Lambda, I am getting this error:
No font was found. To resolve this problem, you can set up your custom set of fonts, following this documentation article: Font Folder – How to Install using C#
I then set up the fonts the way the document specified and after that didn’t work I tried using the s3 bucket to load fonts.
After loading the font object into a memory stream I would add it to the font repository like this:
FontRepository.Sources.Add(new MemoryFontSource(memoryStream.ToArray()));
I then checked my document locally for all of the fonts being used like this:
var document = new Document(htmlStream, htmlLoadOptions);
var fonts = document.FontUtilities.GetAllFonts();
foreach (var f in fonts)
{
Console.WriteLine(f.FontName);
}
I then checked if these fonts were in the repository in AWS Lambda at the time of creating the document like this:
var font = FontRepository.FindFont(font);
return font==null?Ok(“Failure”): Ok(“Successfully Got Font”);
This returned true for all fonts found in the document.
I guess I am confused as to why the fonts seem to exist in the repository, but then still throw the error, but only after deploying.
@kevin.augustine1
It seems you are encountering a font-related issue when converting HTML to PDF using Aspose.PDF in AWS Lambda. Here are some steps and considerations that might help you resolve this problem:
-
Font Repository Setup: Ensure that you have correctly set up the font repository in your AWS Lambda environment. You mentioned that you are adding fonts from a memory stream, which is a good approach. Make sure that the fonts are being loaded correctly and that the MemoryFontSource
is being added to the FontRepository
before you attempt to create the document.
-
Check Font Availability: You can verify if the fonts are available in the repository by using the FontRepository.FindFont(fontName)
method. If this returns null
, it indicates that the font is not found in the repository, even if you believe it should be there.
-
Font Saving Options: When saving the document, consider using the HtmlSaveOptions
to specify how fonts should be handled. For example, you can set the FontSavingMode
to save fonts in TTF format or other formats as needed. This can help ensure that the fonts are correctly embedded in the output PDF.
-
Debugging in AWS Lambda: Since the issue only occurs after deployment, it might be beneficial to add logging to your AWS Lambda function to capture the state of the font repository and any exceptions that occur during the conversion process. This can help you identify if the fonts are being loaded correctly or if there are any other underlying issues.
-
Local vs. AWS Environment: The difference in behavior between your local environment and AWS Lambda could be due to differences in the execution environment. Ensure that all dependencies and configurations are consistent between the two environments.
If you continue to face issues, consider reviewing the documentation on setting up custom font folders and handling fonts in Aspose.PDF, as it may provide additional insights into resolving your problem.
If you need further assistance, feel free to ask!
Sources:
[1]: SaveFonts.cs
@kevin.augustine1
Please try to specify the font folder using below code snippet before converting the document and then deploy the application:
FontRepository.Sources.Add(new FolderFontSource("Font Folder Path"));
In case issue still persists, please share complete error information with stack trace so that we can log an investigation ticket and share the ID with you.