Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itextSharp - htmlString to pdf Unicode are missing

When I am trying to generate PDF from HTML, some Turkish characters like ĞÜŞİÖÇ ğüşıöç are missing in PDF, I see a space in place of these characters but i want to print that character.

My code is:

 public virtual void print pdf(string html, int id)
    {

    String htmlText = html.ToString();

    Document document = new Document();

    string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");

    PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+id+".pdf", 
    FileMode.Create));

    document.Open();

    iTextSharp.text.html.simpleparser.HTMLWorker hw =

                     new iTextSharp.text.html.simpleparser.HTMLWorker(document);



    hw.Parse(new StringReader(htmlText));

    document.Close();

    }

please reply i need suggestion ... regards

like image 519
Vinit Patel Avatar asked Jan 29 '14 06:01

Vinit Patel


Video Answer


1 Answers

I have finally find a solution for this problem, by this you can print all Turkish characters.

    String htmlText = html.ToString();

    Document document = new Document();

    string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");
    PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+Name+".pdf", FileMode.Create));
    document.Open();

    iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
    FontFactory.Register(Path.Combine(_webHelper.MapPath("~/App_Data/Pdf/arial.ttf")),  "Garamond");   // just give a path of arial.ttf 
    StyleSheet css = new StyleSheet();
    css.LoadTagStyle("body", "face", "Garamond");
    css.LoadTagStyle("body", "encoding", "Identity-H");
    css.LoadTagStyle("body", "size", "12pt");

    hw.SetStyleSheet(css);

    hw.Parse(new StringReader(htmlText));
like image 146
Vinit Patel Avatar answered Sep 28 '22 10:09

Vinit Patel