I currently need to convert a single .aspx page to pdf. This page contains images. I used the following code, but it gave an error. I added iTextsharp.dll to my project.
protected void btnConvertToPDF_Click(object sender, EventArgs e) { Uri strurl = Request.Url; string url = strurl.ToString(); string text = GetPageText(url); string filepath = Server.MapPath("test.htm"); StreamWriter writer = new StreamWriter(filepath); writer.Write(text); writer.Close(); htmltopdf(text); } public string GetPageText(string url) { string htmlText = string.Empty; string FILE_NAME = Server.MapPath("test.xml"); //"c:\\test.xml"; try { HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url); requestIP.Timeout = 10000; using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse()) { using (Stream streamIP = responseIP.GetResponseStream()) { using (StreamReader readerText = new StreamReader(streamIP)) { htmlText = readerText.ReadToEnd(); string text = htmlText; StreamWriter writer = new StreamWriter(FILE_NAME); writer.Write(text); writer.Close(); } } } } finally { } return htmlText; } public void htmltopdf(string strHtml) { Document doc = new Document(); PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("test.pdf"), System.IO.FileMode.Create)); HTMLParser.Parse(doc, Server.MapPath("test.htm")); if (File.Exists(Server.MapPath("test.htm"))) File.Delete(Server.MapPath("test.htm")); if (File.Exists(Server.MapPath("test.xml"))) File.Delete(Server.MapPath("test.xml")); }
The error displayed on the HTMLparser.Parse line is because "HTMLPARSER does not exist in the current context" even before the code runs. If I comment on this line and run the code, it will create a single pdf file with the error "'t be open.It was corrupted, not the correct version, etc. etc.". Please can someone tell me what's wrong? Is there any open source code for this task? I have to do this work, not encoded by purchasing any component ..
source share