I am trying to return the current webpage as a PDF on button click. When the user clicks the button I want the PDF to appear so the user can then save it. I have the following code so far:
<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="iTextSharp.text" %>
<%@ Import Namespace="iTextSharp.text.pdf" %>
<%@ Import Namespace="iTextSharp.text.html" %>
<%@ Import Namespace="iTextSharp.text.xml" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="iTextSharp.text.html.simpleparser" %>
<script runat="server">
public void CreatePDFDocument(string strHtml)
{
string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
//Creation of a document-object
Document document = new Document();
//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();
}
private void buttonPDF_Click(object sender, System.EventArgs e)
{
CreatePDFDocument("hello.pdf");
}
</script>
<asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>
But when I click I get nothing...
Firstly, do I need to create a pdf file called test.pdf locally? I'm just not sure about how to get this working?
When I click the button it does call the method but it doesn't do anything, my problem is that I need to return the pdf to the page.
I have tried that method and when I ckick on the button it goes to an error page:
This webpage is not found
<div id="errorSummary" jsselect="summary" jstcache="1">No webpage was found for the web address: http://abc.com</div> <div jsselect="summary" jstcache="1"> </div> <div id="errorDetails" jsselect="details" jscontent="$this" jstcache="3" class="$this">Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.</div> <div jsselect="details" jscontent="$this" jstcache="3" class="$this"></div>
<div jsselect="details" jscontent="$this" jstcache="3" class="$this"></div>
billy_111
Member
333 Points
878 Posts
Using iTextSharp to convert current webpage to PDF
Mar 24, 2012 07:11 PM|LINK
Hi there,
I am trying to return the current webpage as a PDF on button click. When the user clicks the button I want the PDF to appear so the user can then save it. I have the following code so far:
<%@ Control Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Collections.Generic" %> <%@ Import Namespace="System.Linq" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.UI" %> <%@ Import Namespace="System.Web.UI.WebControls" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Text.RegularExpressions" %> <%@ Import Namespace="iTextSharp.text" %> <%@ Import Namespace="iTextSharp.text.pdf" %> <%@ Import Namespace="iTextSharp.text.html" %> <%@ Import Namespace="iTextSharp.text.xml" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="iTextSharp.text.html.simpleparser" %> <script runat="server"> public void CreatePDFDocument(string strHtml) { string strFileName = HttpContext.Current.Server.MapPath("test.pdf"); //Creation of a document-object Document document = new Document(); //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(); } private void buttonPDF_Click(object sender, System.EventArgs e) { CreatePDFDocument("hello.pdf"); } </script> <asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>But when I click I get nothing...
Firstly, do I need to create a pdf file called test.pdf locally? I'm just not sure about how to get this working?
When I click the button it does call the method but it doesn't do anything, my problem is that I need to return the pdf to the page.
Can someone please help?
Regards,
Vipindas
Contributor
5720 Points
858 Posts
Re: Using iTextSharp to convert current webpage to PDF
Mar 24, 2012 07:16 PM|LINK
http://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx
billy_111
Member
333 Points
878 Posts
Re: Using iTextSharp to convert current webpage to PDF
Mar 24, 2012 08:01 PM|LINK
Thank you,
I have tried that method and when I ckick on the button it goes to an error page:
This webpage is not found
<div id="errorSummary" jsselect="summary" jstcache="1">No webpage was found for the web address: http://abc.com</div> <div jsselect="summary" jstcache="1"></div> <div id="errorDetails" jsselect="details" jscontent="$this" jstcache="3" class="$this">Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.</div> <div jsselect="details" jscontent="$this" jstcache="3" class="$this"></div> <div jsselect="details" jscontent="$this" jstcache="3" class="$this"></div>
paritoshmmec
Participant
1555 Points
304 Posts
Re: Using iTextSharp to convert current webpage to PDF
Mar 27, 2012 08:53 AM|LINK
There might be some link which is not found.
Parser is trying to embed inside the page.
Thanks and Regards,
Paritosh