Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to convert pdf dynamically from html file
Posted

1 solution

priate void ConvertHtmlToPdf()
{
string htmlText = ReadHtmlFile(Request.PhysicalApplicationPath+"\\sampleHTML.html");
			string pdfName = Guid.NewGuid().ToString()+".pdf";  
			Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + pdfName, FileMode.Create));
            document.Open();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Parse(new StringReader(htmlText));
            document.Close();
}

public string ReadHtmlFile(string htmlFilePath)
        {
            string htmlText = string.Empty;
            try
            {
                using (StreamReader template = new StreamReader(htmlFilePath))
                {
                    htmlText = ConvertHtmlToString(template, false);
                    template.Close();
                }                
            }
            catch(Exception E)
            {
               
            }  
return htmlText;			
        }
		
public string ConvertHtmlToString(TextReader streamToRead, bool isHtml)
        {
            StringBuilder body = new StringBuilder();
            StringBuilder nextTag = new StringBuilder();
            bool inTag = false;
            char nextCharacter = char.MinValue;
            char tagStart = '$';

            while (streamToRead.Peek() >= 0)
            {
                nextCharacter = Convert.ToChar(streamToRead.Peek());
                if (nextCharacter.Equals(tagStart)) inTag = !inTag;

                if (inTag)
                {
                    nextTag.Append(Convert.ToChar(streamToRead.Read()));
                    if (nextTag.Length >= 50)
                    {
                        body.Append(nextTag.ToString());
                        nextTag.Length = 0;
                        inTag = false;
                    }
                }
                else if (nextTag.Length > 0)
                {
                    if (nextCharacter.Equals(tagStart)) nextTag.Append(Convert.ToChar(streamToRead.Read()));
                    body.Append(ReplaceHtmlValues(nextTag.ToString(), isHtml));
                    nextTag.Length = 0;
                }
                else
                {
                    body.Append(Convert.ToChar(streamToRead.Read()));
                }
            }

            return body.ToString();
        }
		
		private string ReplaceHtmlValues(string tag, bool isHtml)
        {
            string returnValue = string.Empty;
            tag = tag.Trim();

            switch (tag)
            {
                case "$Employee$":
                    returnValue = "Employee Name";
                    break;
            }
            return returnValue;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900