We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Issues after publishing to Azure Websites

Hello, this is my 1st post here. We have the need to dynamically create high quality PDFs from an Azure WebAPI method so I have downloaded the trial version and created a simple test .Net project and .aspx page as a proof of concept. This proof of concept works on my local dev machine; however, after I publish to a test Azure Websites website it no longer works and errors. I am guessing this is an issue related to our use of the Arial fonts which might not be available in the shared Azure Websites environment.


This simple test works great locally ( I have the ttf files); however, in Azure it crashes when it encounters the pdf.Save(finalStream); statement. I have attempted to add the various Arial ttf files to the fonts folder with Build Actions of “Content” and “Embedded Resource” but it still crashes.

If I comments out the following 2 lines from the BuildSection1 method it works fine except it uses the default font, not the Arial Narrow.

seg1.TextInfo.FontName = “Arial Narrow”;
seg1.TextInfo.IsTrueTypeFontBold = true;

Here are the relevant code snippets, the control method calls BuildSection1() then StreamPDF() to stream the PDF

protected void BuildSection1()
{

try
{
Aspose.Pdf.Generator.Section sect1 = pdf.Sections.Add();
sect1.BackgroundImageFile = Server.MapPath("~/Images/Background.jpg");
Aspose.Pdf.Generator.Text t1 = new Aspose.Pdf.Generator.Text(sect1);
sect1.Paragraphs.Add(t1);
Aspose.Pdf.Generator.Segment seg1 = new Aspose.Pdf.Generator.Segment(t1);
seg1.Content = “This is content added to seg1.Content…”;

// note: if I comment the following 2 lines and publish to azure everything works except it uses the default font.
// note: everything works fine locally and the proper font is used; however, the pdf.Save(finalStream); command below blows up in Azure when a font is selected
seg1.TextInfo.FontName = “Arial Narrow”;
seg1.TextInfo.IsTrueTypeFontBold = true;
seg1.TextInfo.IsFontEmbedded = true;

seg1.TextInfo.Color = new Aspose.Pdf.Generator.Color(“red”);
seg1.TextInfo.FontSize = 12;
t1.Opacity = 1F;
t1.Segments.Add(seg1);
}
catch (Exception) { throw; }
}
protected void StreamPDF(string documentTitle)
{
try
{
System.IO.MemoryStream finalStream = new System.IO.MemoryStream();
pdf.Save(finalStream); //note pdf var was defined globally in the real code…
byte[] downloadBytes = finalStream.ToArray();
HttpContext.Current.Response.AddHeader(“Content-Type”, “application/pdf”);
HttpContext.Current.Response.AddHeader(“Content-Disposition”, “inline; filename=” + documentTitle + “.pdf”);
HttpContext.Current.Response.BinaryWrite(downloadBytes);
HttpContext.Current.Response.End();
}
catch (Exception) { throw; }
}

Is there a better way to use fonts and publish them to Azure?

Thanks…

Hi,


Thanks for using our API’s.

As per your observations, the problem seems to be occurring due to unavailability of font in Azure production environment. So in order to resolve the issue, ensure that referenced font is installed over system. Furthermore, as I have observed that you are using legacy Aspose.Pdf.Generator approach to create PDF document but I would recommend using new Document Object Model to create PDF files. For further information, please visit Add Text in an Existing PDF File (you can also create PDF file from scratch i.e. create Document object without providing input file name).

OK, I’ve made a little progress. On Azure I am able to publish the fonts to the ‘fonts’ folder within my project. I am also able to access the font via the following C# code.


string fontFolderPath = MapPath("~/fonts/");
string fontARIALN = fontFolderPath + “ARIALN.TTF”;
var fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(fontARIALN);
var myFontFamily = fontCollection.Families[0];

My goal is to do something similar within Aspose so that I can use various fonts as needed, i.e something like:

Aspose.Pdf.Text.FontCollection asposeFontCollection;
asposeFontCollection.Add(fontARIALN);

I also tried the following to using you earlier code to have the FindFont metod locate the font within my fonts folder in azure but it did not find it:

TextFragment textFragment = new TextFragment(“main text”);
textFragment.Position = new Position(100, 600);
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont(“Arial Narrow”);

Could you point me in the right direction?

PS, thanks for the heads up concerning the Newer DOM api.

Hi,


Thanks for sharing the details.

In order to check the compatibility of Aspose.Pdf for .NET with Azure environment, I have logged an investigation ticket in our issue tracking system as PDFNEWNET-38565 in our issue tracking system. We will further look into the details of this problem and will keep you posted with our findings. We are sorry for this inconvenience.

Hi,


In reference to the ticket ID PDFNET-38565, please use method FontRepository.OpenFont() instead of FontRepository.FindFont. The reason is that FindFont method searches fonts in the system fonts directory and also in directories provided by the user, while OpenFont method takes the name of font file directly.

Sample code:
[.NET, C#]
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>TextFragment textFragment = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> TextFragment(“main text”);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
textFragment.Position = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Position(100, 600);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
textFragment.TextState.FontSize = 12;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>string<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> fontFolderPath = MapPath("~/fonts/");<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>string<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> fontARIALN = fontFolderPath + “ARIALN.TTF”;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
textFragment.TextState.Font = FontRepository.OpenFont(fontARIALN);

However, if required to use the multiple fonts, it is better to add specified directory in FontRepository object and only then use method FontRepository.FindFont().

Sample code:
[.NET, C#]
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>string<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> fontFolderPath = MapPath("~/fonts/");<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
FolderFontSource fs = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> FolderFontSource(fontFolderPath);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
FontRepository.Sources.Add(fs);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
TextFragment textFragment = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> TextFragment(“main text”);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
textFragment.Position = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Position(100, 600);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
textFragment.TextState.FontSize = 12;<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>textFragment.TextState.Font = FontRepository.FindFont(“Arial Narrow”);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>TextFragment textFragment2 = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> TextFragment(“additional text”);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>textFragment.Position = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Position(100, 400);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>textFragment.TextState.Font = FontRepository.FindFont(“Arial”);
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>