What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 1567 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > How to convert aspx page to pdf ...
Prems

How to convert aspx page to pdf

Replies: 5 | Posted by: Prems on 8/9/2011 | Category: ASP.NET Forums | Views: 4277 | Status: [Member] | Points: 10  


Any idea for converting an aspx page to pdf format using c# with asp.net.


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

SheoNarayan
SheoNarayan  
Posted on: 8/9/2011 7:52:12 PM
Level: HonoraryPlatinum | Status: [Microsoft_MVP] [Administrator] | Points: 25

Use this article as base http://www.dotnetfunda.com/articles/article977-pdf-generator-in-net-dynamically-generate-pdf-in-aspnet-.aspx.

The idea is to generate the html for your .aspx page first and convert it to .pdf using above approach.

Thanks

Regards,
Sheo Narayan, Microsoft MVP
The Founder
http://www.dotnetfunda.com

Prems, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Dhirendra
Dhirendra  
Posted on: 8/11/2011 5:14:06 AM
Level: Starter | Status: [Member] | Points: 25

Use PDFsharp, iTextSharpe library to generate PDF documents
look at this open source libraries
http://csharp-source.net/open-source/pdf-libraries

Prems, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Chandra.Shekhar
Chandra.Shekhar  
Posted on: 4/24/2012 6:37:33 AM
Level: Starter | Status: [Member] | Points: 25

How to convert aspx page to pdf

protected void lbtn_PDF_Click(object sender, EventArgs e)
{
Uri strurl = Request.Url;
string url = strurl.ToString();
string text = GetPageText(url);
string filepath = Server.MapPath("test.html");
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");
try
{
HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);
CookieContainer cc = new CookieContainer();
requestIP.CookieContainer = cc;
requestIP.Timeout = 100000;
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();
StringWriter sw = new StringWriter();
StringReader sr = new StringReader(sw.ToString());
HTMLWorker HTMLParser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("test.pdf"), System.IO.FileMode.Create));
HTMLParser.Parse(sr);
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"));
}

Thanks
chandrashekhar

Prems, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Chandra.Shekhar
Chandra.Shekhar  
Posted on: 4/24/2012 6:42:15 AM
Level: Starter | Status: [Member] | Points: 25

How to Download File


try
{
string filepath = Server.MapPath("~/MyDocs/EmpDoc/" + derivedfileName);
System.IO.FileInfo file = new System.IO.FileInfo(filepath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-disposition", "attachment; filename=" + filepath);
Response.AddHeader("content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";//
Response.WriteFile(file.FullName);
Response.End();
}
else
{
//Response.Write("This file does not exist");
//lbl_Msg.Text = "This file does not exist on server";
ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('This file does not exist on server');", true);
}
}
catch
{
}

Thanks
chandrashekhar

Prems, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Ariv.Manickam
Ariv.Manickam  
Posted on: 5/10/2012 7:34:47 AM
Level: Starter | Status: [Member] | Points: 25

Hi Chandrashekhar,

i tried using your code, i am not able to open pdf file , it's saying wrong file format..

can you please help..

thanks in Advance,,

Ariv

Prems, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

Found interesting? Add this to:


 Latest Posts

Write New Post | More ...

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 7:32:03 PM