Create PDF from HTML and Apply Custom Font using Amazon Aspose PDF

how to create pdf from html and apply custom font using Amazon Aspose pdf in the Asp.Net Web Application

December 11, 2020

In this article we will cover how to create pdf from html and apply custom font using Amazon Aspose pdf in the Asp.Net Web Application

To apply custom font, place your font (arial-unicode-ms.ttf) inside App_Data/fonts folder.

Create pdf using below code.

// Add Custom font (arial-unicode-ms.ttf) to Aspose.Pdf FontRepository
string fontpath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/fonts");
var fs = new Aspose.Pdf.Text.FolderFontSource(fontpath);
Aspose.Pdf.Text.FontRepository.Sources.Add(fs);
 
// HTML CONTENT
string Content = "<table><tr><td>CONTENT</td></tr></table>";
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(new MemoryStream(Encoding.UTF8.GetBytes(Content)), new Aspose.Pdf.HtmlLoadOptions());
 
// SAVE IN MemoryStream
using (MemoryStream streampdf = new MemoryStream())
{
    doc.Save(streampdf, Aspose.Pdf.SaveFormat.Pdf);
}
// SAVE TO LOCAL PATH
doc.Save(Savepath + filename);

Note:

1) HTML structure should be proper

2) HTML tags should be proper

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...