How to convert .aspx to pdf using C #?

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 ..

+4
source share
5 answers

You can use WkHtmltoPdf to cover a pdf page. See this post for more details.

Edit:

Use this code to convert the URL directly to pdf. You need to put the wkhtmltopdf.exe file in the bin folder of your project

 string url= @"http://www.google.com"; try { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.FileName = Server.MapPath("~/bin/") + "wkhtmltopdf.exe"; process.StartInfo.Arguments = "\""+ url+ " " + Server.MapPath("~/PDFFiles/") + "test.pdf\""; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.Start(); process.WaitForExit(); } catch (Exception ee) { //logging } 
+5
source

If you do not pay for an external library, you can do this very easily with ABCPDF . This can save you a headache.

 Doc theDoc = new Doc(); theDoc.AddImageUrl("http://www.google.com/"); theDoc.Save(Server.MapPath("htmlimport.pdf")); theDoc.Clear(); 
+3
source

Take a look at ExpertPdf ( www.html-to-pdf.net ). This is a great html to pdf converter for .NET. There is an online demo here:

http://www.html-to-pdf.net/free-online-pdf-converter.aspx

+2
source

To convert any webpage to a .pdf file, you can use the free wkhtmltopdf shell utility , which uses the webkit rendering engine (which is also used for Google Chrome or Safari).

I provided instructions and code for a possible implementation in this article .

If you have any questions, feel free to ask.

0
source

First download wkhtmltox-0.11.0_rc1-installer from the link http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltox-0.11.0_rc1-installer.exe&can=1&q.Then run the installer, and then copy The wkhtmltopdf file in your project bin folder. My goal is to convert the entire .aspx page to pdf. Using the System.Diagnostics protocol; using System.Configuration; Add a button to your form. On the Click event of this button, write the following code:

 protected void btnConvertToPDF_Click(object sender, EventArgs e) { Uri strurl = Request.Url; string url = strurl.ToString(); string filename = "Test"; HtmlToPdf(url, filename); } public static bool HtmlToPdf(string Url, string outputFilename) { string filename = ConfigurationManager.AppSettings["ExportFilePath"] + "\\" + outputFilename + ".pdf"; Process p = new System.Diagnostics.Process(); p.StartInfo.Arguments = Url + " " + filename; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = HttpContext.Current.Server.MapPath("~/bin/") + "wkhtmltopdf.exe"; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardInput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(60000); // read the exit code, close process int returnCode = p.ExitCode; p.Close(); // if 0 or 2, it works return (returnCode == 0 || returnCode == 2); } 

This code will create a PDF file called Test, and it will be saved on drive C. Friends, that's all. I am very grateful for all the support I have received from all of you.

0
source

Source: https://habr.com/ru/post/1386317/


All Articles