I’m using Aspose.PDF for .NET to generate PDF files in an AWS Lambda environment.
The issue I’m facing is that boxes (□) appear instead of text in the generated PDFs — this seems to happen because the required fonts aren’t available in the Lambda runtime.
Since AWS Lambda has limited filesystem access and size constraints, I’d prefer not to include additional font files in a Lambda Layer.
Could you please advise on:
- How to embed fonts directly in the generated PDF using Aspose.PDF for .NET, so the output displays correctly even when the fonts aren’t installed on the system?
- Whether Aspose can embed standard fonts (e.g., Arial, Times New Roman, etc.) without explicitly providing font files?
- Any code examples or recommended settings for ensuring fonts are embedded properly?
code snippet:
private IDocumentContentModel ConvertTo(byte[] source, SaveFormat format)
{
using var inputStream = new MemoryStream(source);
Document doc = new Document(inputStream);
using MemoryStream outputStream = new MemoryStream();
doc.Save(outputStream, format);
return new DocumentContentModel(outputStream.ToArray(), doc.PageCount);
}
@efatima
To address the issue of missing fonts in your generated PDFs using Aspose.PDF for .NET in an AWS Lambda environment, follow these steps:
-
Using Standard Fonts: Aspose.PDF supports standard fonts like Arial and Times New Roman, which do not require additional font files. These fonts are typically available on most systems and are embedded automatically when used in your document.
-
Code Example: Below is a code snippet that demonstrates how to set a standard font in your PDF:
using Aspose.Pdf;
using Aspose.Pdf.Text;
private IDocumentContentModel ConvertTo(byte[] source, SaveFormat format)
{
using var inputStream = new MemoryStream(source);
Document doc = new Document(inputStream);
// Set the font for the text
foreach (var page in doc.Pages)
{
TextFragment textFragment = new TextFragment("Your text here");
textFragment.TextState.Font = FontRepository.FindFont("Arial"); // Use a standard font
page.Paragraphs.Add(textFragment);
}
using MemoryStream outputStream = new MemoryStream();
doc.Save(outputStream, format);
return new DocumentContentModel(outputStream.ToArray(), doc.PageCount);
}
This code sets the font to Arial, which should be embedded in the PDF, preventing the appearance of boxes instead of text.
By following these steps, you should be able to generate PDFs that display correctly without the need for additional font files in your AWS Lambda environment.
If you have further questions or need additional assistance, feel free to ask!