Free Support Forum - aspose.cloud

Embedding Custom Fonts for Cloud App


#1

Hello,


I am trying to use Aspose in an app that will eventually be sitting in an Azure App Service where we cannot install custom fonts to the OS.

I tried using the FontRepository.OpenFont() method to get the custom font, but this only works when the font is installed on the system.

Is Aspose capable of fully embedding a font into the PDF in the way MigraDoc/PDFSharp can so that we can use it in a cloud service?

Thanks.

#2

Hi there,


Thanks for your inquiry. You can easily embed fonts using Aspose.Pdf for .NET both in existing PDF document and a new PDF document while creating it form scratch. Please check following documentation links for details.


Furthermore, you may also define a custom folder as font storage using Aspose.Pdf for .NET as following.

FontRepository.Sources.Add(new FolderFontSource(“C:\CustomFontStorage”));<o:p></o:p>


Please feel free to contact us for any further assistance.

Best Regards,

#3

Thanks for your response.


I am wondering about the best practice for performance when using embedded fonts.

I noticed that setting textFragment.Font = FontRepository.OpenFont(…) works, but only if you are not sharing the result of FontRepository.OpenFont().

E.g.
I have a variable off in some static class and set it to FontRepository.OpenFont(). If I try to set the TextFragment’s Font property to this variable it will not work. If I set the TextFragment’s Font property directly to FontRepository.OpenFont() it does work.

Is there any performance concern by doing FontRepository.OpenFont() every time I want to set the font of a TextFragment? Is this the best practice for embedding fonts?

#4

Hi there,


Thanks for your inquiry. You may also define a TextState object for font settings and use it later as per your requirements. Hopefully it will help you to accomplish task effectively instead setting font property directly. However, if you face any issue then please feel free to contact us for any further assistance.

string inFile = “NotoSansJP-Regular.otf”;<o:p></o:p>

Aspose.Pdf.Text.TextState textState = new Aspose.Pdf.Text.TextState();

textState.Font = FontRepository.OpenFont(inFile);

textState.FontSize = (float)8;

Document doc = new Document();

doc.Pages.Add();

TextFragment textFragment = new TextFragment("test");

textFragment.TextState.ApplyChangesFrom(textState);

....

....


Best Regards,


#5

Thanks for the tip.


Sadly, this wasn’t the root of our performance issues.

It looks like adding images to the file (around 90 small icons) is adding around 6 seconds to the generation time.

Are there any settings or best practices when adding images to improve the performance? We are asking because we are trying to evaluate MigraDoc/PdfSharp vs. Aspose and performance is our top priority aside from font-embedding.

The following code shows our technique, where the image is dynamically selected based on some conditions:

var serviceIconRow = table.Rows.Add();
var serviceIconCell = serviceIconRow.Cells.Add();
var serviceIconImage = new Image
{
File = $@"{directory}{color.ServiceLabelImagePath.Replace(ColorDataCollection.LabelTag, prescriptionInfo.Label)}",
FixHeight = 14.1732,
FixWidth = 14.1732,

};
serviceIconCell.Paragraphs.Add(serviceIconImage);

Thank you.

#6
jlamartina:
It looks like adding images to the file (around 90 small icons) is adding around 6 seconds to the generation time.

Are there any settings or best practices when adding images to improve the performance? We are asking because we are trying to evaluate MigraDoc/PdfSharp vs. Aspose and performance is our top priority aside from font-embedding.

Interesting question. Watching this thread as well, also for evaluation.

#7

Hi,


Thanks for sharing the details.

We are further looking into this matter and will update you accordingly.

#8

Hi there,


jlamartina:
Thanks for the tip.

Sadly, this wasn’t the root of our performance issues.

It looks like adding images to the file (around 90 small icons) is adding around 6 seconds to the generation time.

Are there any settings or best practices when adding images to improve the performance? We are asking because we are trying to evaluate MigraDoc/PdfSharp vs. Aspose and performance is our top priority aside from font-embedding.

The following code shows our technique, where the image is dynamically selected based on some conditions:

var serviceIconRow = table.Rows.Add();
var serviceIconCell = serviceIconRow.Cells.Add();
var serviceIconImage = new Image
{
File = $@"{directory}{color.ServiceLabelImagePath.Replace(ColorDataCollection.LabelTag, prescriptionInfo.Label)}",
FixHeight = 14.1732,
FixWidth = 14.1732,

};
serviceIconCell.Paragraphs.Add(serviceIconImage);

Thank you.

Thanks for your feedback. It is good to know that your fonts related concern is addressed. Furthermore in reference to performance, please note Aspose.Pdf processes the files in memory instead disk, so performance depends upon the file size/contents and system resources. However, we will appreciate it if you please share your sample images here as well, so we will test the scenario and will guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

#9

Hello,


We are simply using a FileStream to bring the attached image into memory and then reusing the stream to set the ImageStream property on our Image objects, repeated around 90 times, each in its own table row.

Getting rid of the piece of code that inserts the image takes off about 6s from the generation time. We’ve also tried using the method of setting the Image FilePath but saw no real performance difference.

The fact that it’s taking this long is a problem for us because we are currently using another PDF generation library that is already much faster with much more complicated rendering logic, and even that is too slow. However, we can’t deploy it to an Azure App Service so we are trying to find a solution that will be both fast and able to be deployed to a sandboxed environment with no OS kernel-level dependencies. From what we’ve seen so far, Aspose will work in the cloud for us but the performance is not good enough for us to switch.

Are there any suggestions/tweaks we can make to speed this up? Our PDF is very image-heavy.

Thanks.


#10

Hi there,


Thanks for your feedback. I am looking into it and will update you soon.

Best Regards,

#11
Hi there,

jlamartina:
Hello
We are simply using a FileStream to bring the attached image into memory and then reusing the stream to set the ImageStream property on our Image objects, repeated around 90 times, each in its own table row.

Getting rid of the piece of code that inserts the image takes off about 6s from the generation time. We've also tried using the method of setting the Image FilePath but saw no real performance difference.

The fact that it's taking this long is a problem for us because we are currently using another PDF generation library that is already much faster with much more complicated rendering logic, and even that is too slow. However, we can't deploy it to an Azure App Service so we are trying to find a solution that will be both fast and able to be deployed to a sandboxed environment with no OS kernel-level dependencies. From what we've seen so far, Aspose will work in the cloud for us but the performance is not good enough for us to switch.

Are there any suggestions/tweaks we can make to speed this up? Our PDF is very image-heavy.

Thanks.


I am sorry for the delayed response. I have tested following code snippet with your shared image using Aspose.Pdf for .NET 11.9.0 on Win 64 bit 8GB RAM and it is taking almost 4 seconds for 100 images. I will appreciate it if you please share your sample code and expected time, so we investigate it further and provide you information accordingly.

Stopwatch sw = new Stopwatch();

sw.Start();

string outFile = @"E:\data\signalTable.pdf";

Document doc = new Document();

Aspose.Pdf.Page page = doc.Pages.Add();

Aspose.Pdf.Image img = new Aspose.Pdf.Image();

//img.File = @"E:\data\erx-service-label-blue_.png";

FileStream fs = new FileStream(@"E:\data\erx-service-label-blue_.png", FileMode.Open, FileAccess.Read);

byte[] tmpBytes = new byte[fs.Length];

fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

MemoryStream mystream = new MemoryStream(tmpBytes);

img.ImageStream = mystream;

img.FixHeight = 14.1732;

img.FixWidth = 14.1732;

Aspose.Pdf.Table signalTable = new Aspose.Pdf.Table();

for (int i = 1; i < 100;i++ )

{

signalTable.ColumnWidths = string.Format("100");

signalTable.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.None);

Aspose.Pdf.Row row1 = signalTable.Rows.Add();

Aspose.Pdf.Cell signalCell = row1.Cells.Add();

signalCell.Alignment = Aspose.Pdf.HorizontalAlignment.Left;

signalCell.VerticalAlignment = VerticalAlignment.Center;

signalCell.Paragraphs.Add(img);

}

page.Paragraphs.Add(signalTable);

doc.Save(outFile);

sw.Stop();

Console.WriteLine(sw.Elapsed.TotalSeconds);

Console.ReadKey();


We are sorry for the inconvenience.

Best Regards,