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

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




Responses

Posted by: Satyapriyanayak on: 8/31/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
.aspx.cs file below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Xml;
using iTextSharp.text.html.simpleparser;

namespace pdf
{
public partial class _Default : System.Web.UI.Page
{

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


If this post helps you mark it as answer
Thanks

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

Posted by: Satyapriyanayak on: 8/31/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Here is a demo given below
First add the itextsharp.dll file into the application

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="pdf._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<tr>
<td colspan="2">
convert web page to pdf
</td>
</tr>
<tr>
<td>
cell1
</td>
<td>
cell2
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblLabel" runat="server" Text="Welcome"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>




If this post helps you mark it as answer
Thanks

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

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: Evanpan on: 9/26/2015 [Member] Starter | Points: 25

Up
0
Down
How about this pdf converter?( http://www.pqscan.com/convert-pdf/ )

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

Login to post response