How to convert aspx page to pdf [Resolved]

Posted by Prems under ASP.NET on 8/9/2011 | Points: 10 | Views : 15366 | Status : [Member] | Replies : 12
Any idea for converting an aspx page to pdf format using c# with asp.net.




Responses

Posted by: SheoNarayan on: 8/9/2011 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
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
http://www.dotnetfunda.com

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

Posted by: Dhirendra on: 8/11/2011 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Chandra.Shekhar on: 4/24/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Chandra.Shekhar on: 4/24/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Ariv.Manickam on: 5/10/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Evanpan on: 3/22/2016 [Member] Starter | Points: 25

Up
0
Down
I wonder whether the pdf conversion toolkit I am testing these days can help with it?
http://www.pqscan.com/convert-pdf/
Any suggestion will be appreciated. Thanks in advance.



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

Posted by: Yasminpriya on: 3/23/2016 [Member] Starter | Points: 25

Up
0
Down
Use this code to convert the url directly to pdf. You need to place the wkhtmltopdf.exe in 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
}



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

Posted by: Oliver32 on: 4/1/2016 [Member] Starter | Points: 25

Up
0
Down
I used below code snippets for the same purpose.

using System.Drawing;
using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter.Qt;

namespace SPIREPDF_HTMLtoPDF
{
class Program
{
static void Main(string[] args)
{
HtmlConverter.Convert("http://www.wikipedia.org/", "HTMLtoPDF.pdf",

//enable javascript
true,

//load timeout
100 * 1000,

//page size
new SizeF(612, 792),

//page margins
new PdfMargins(0, 0));
System.Diagnostics.Process.Start("HTMLtoPDF.pdf");
}
}
}


reference: http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-with-New-Plugin.html

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

Posted by: Kuldeepnd22 on: 5/21/2016 [Member] Starter | Points: 25

Up
0
Down
Packers and Movers Pune #
http://www.movers5th.in/packers-and-movers-pune/
Packers and Movers Mumbai #
http://www.movers5th.in/packers-and-movers-mumbai/
Packers and Movers Bangalore #
http://www.movers5th.in/packers-and-movers-bangalore/

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

Posted by: Kuldeepnd22 on: 5/21/2016 [Member] Starter | Points: 25

Up
0
Down
Packers and Movers Gurgaon #
http://www.movers5th.in/packers-and-movers-gurgaon/
Packers and Movers Hyderabad #
http://www.movers5th.in/packers-and-movers-hyderabad/
Packers and Movers Delhi #
http://www.movers5th.in/packers-and-movers-delhi/


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

Posted by: Kuldeepnd22 on: 5/21/2016 [Member] Starter | Points: 25

Up
0
Down
Packers and Movers Noida @
http://www.movers5th.in/packers-and-movers-noida/
Packers and Movers Chennai @
http://www.movers5th.in/packers-and-movers-chennai/
Packers and Movers Navi Mumbai @
http://www.movers5th.in/packers-and-movers-navimumbai/
Packers and Movers Thane @
http://www.movers5th.in/packers-and-movers-thane/
Packers and Movers Ghaziabad @
http://www.movers5th.in/packers-and-movers-ghaziabad/
Packers and Movers Faridabad @
http://www.movers5th.in/packers-and-movers-faridabad/


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

Posted by: Kuldeepnd22 on: 5/21/2016 [Member] Starter | Points: 25

Up
0
Down
Packers and Movers in Hyderabad @
http://www.moveby5th.in/packers-and-movers-hyderabad.html
Packers and Movers in Pune @
http://www.moveby5th.in/packers-and-movers-pune.html
Packers and Movers in Bangalore @
http://www.moveby5th.in/packers-and-movers-bangalore.html
Packers and Movers in Mumbai @
http://www.moveby5th.in/packers-and-movers-mumbai.html
Packers and Movers in Delhi @
http://www.moveby5th.in/packers-and-movers-delhi.html
Packers and Movers in Gurgaon @
http://www.moveby5th.in/packers-and-movers-gurgaon.html

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

Login to post response