Convert web page(.aspx page) to pdf [Resolved]

Posted by Amulya under ASP.NET on 8/31/2013 | Points: 10 | Views : 65576 | Status : [Member] | Replies : 5
How to Convert web page(.aspx page) to pdf




Responses

Posted by: Rebecca Yang on: 9/27/2013 [Member] Starter | Points: 25

Up
0
Down
You can convert webpage to Pdf by using Spire.PDF for .NET
http://pdfapi.codeplex.com/

C#
using Spire.Pdf;

namespace WPFhtmltopdf
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[STAThread]
private void button1_Click(object sender, RoutedEventArgs e)
{

//Create a new pdf document.
PdfDocument doc = new PdfDocument();
//load the webpage
String url = "http://msdn.microsoft.com/";
doc.LoadFromHTML(url, false, true, true);
//Save html webpage as pdf file.
doc.SaveToFile("sample.pdf");
doc.Close();
}
}
}
{[VB.NET]}
Imports Spire.Pdf

Namespace WPFhtmltopdf
'''
''' Interaction logic for MainWindow.xaml
'''
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
_
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)

'Create a new pdf document.
Dim doc As New PdfDocument()
'load the webpage
Dim url As = "http://msdn.microsoft.com/"
doc.LoadFromHTML(url, False, True, True)
'Save html webpage as pdf file.
doc.SaveToFile("sample.pdf")
doc.Close()
End Sub
End Class
End Namespace

Hope it helps.




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

Posted by: Arronlee on: 12/25/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks for your nice info. I have only tried another PDF converter( http://www.yiigo.com/guides/csharp/how-to-pdf-convert.shtml ) before. I am not sure whether it allows users to convert web page(.aspx page) to pdf. I will try it later.

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

Posted by: Ashokst on: 11/11/2015 [Member] Starter | Points: 25

Up
0
Down
Here is a solution provided by Spire.

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

Full Code:

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");
}
}




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

Posted by: Triashish on: 1/8/2016 [Member] Starter | Points: 25

Up
0
Down
am using your code for convert aspx page to pdf

protected override void Render(HtmlTextWriter writer)
{
MemoryStream mem = new MemoryStream();
StreamWriter twr = new StreamWriter(mem);
HtmlTextWriter myWriter = new HtmlTextWriter(twr);
base.Render(myWriter);
myWriter.Flush();
myWriter.Dispose();
StreamReader strmRdr = new StreamReader(mem);
strmRdr.BaseStream.Position = 0;
string pageContent = strmRdr.ReadToEnd();
strmRdr.Dispose();
mem.Dispose();
writer.Write(pageContent);
CreatePDFDocument(pageContent);
}
public void CreatePDFDocument(string strHtml)
{

string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
StringReader se = new StringReader(strHtml);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
document.Close();
ShowPdf(strFileName);
}
public void ShowPdf(string strFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);
Response.Flush();
Response.Clear();
}


but am getting the error that Stack empty. on obj.Parse(se); this Syntax so can you tell the solution
please rply on ashish60tiwari@gmail.com
Thanks

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

Posted by: Naveed1229 on: 6/12/2017 [Member] Starter | Points: 25

Up
0
Down
How to Convert/Export an ASPX page with texboxes and other controls(Checkbox etc) to PDF using itextsharp

Can any one help?

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

Login to post response