Hello I've been trying to convert a HTML string to PDF in iText, it works and I get the file but any arabic text in the HTML is now showing, ofcourse I searched for a solution and found out that I need to register a font for arabic and I tried more than one font but no luck at all, here is my code and any help would be great
private void CreatePDFItextSharp()
{
string dataFile = Server.MapPath("~/Html.txt");
string html = File.ReadAllText(dataFile);
//StringReader sr = new StringReader(html);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
//Font normal = FontFactory.GetFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, 18, Font.BOLD);
string arialFontFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arial.ttf");
FontFactory.Register(arialFontFile);
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.Register(arialFontFile);
byte[] bytes;
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
var xmlWorker = XMLWorkerHelper.GetInstance();
MemoryStream htmlContent = new MemoryStream(Encoding.UTF8.GetBytes(html));
xmlWorker.ParseXHtml(writer, pdfDoc, htmlContent, null, Encoding.UTF8, fontProvider);
pdfDoc.Close();
bytes = memoryStream.ToArray();
memoryStream.Close();
}
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Invoice.pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
I finlly got it to work, turns out that I have to register the font and use it in the HTML text.
this line to register the fonts in this case I'll register all fonts
XMLWorkerFontProvider(Environment.GetFolderPath(Environment.SpecialFolder.Fonts));
and in the html I should have something like this:
<body style="font-family:'Arial Unicode MS'">
to use the font