wrap your html content around a div or panel which can give you an inner html(if you have already then don't put it)Something like this:-
<div id="dvParentControl" runat=server>
//html control
</div>
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
dvHtml.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close();
The problem is that I am using this as a control. So I have one control where I reference this control like so...
~\controls\controlA.ascx
<!-- User control for PDF generator -->
<form runat="server" id="pdfFormTag" action="index.aspx" method="post">
<rcuk:pdfgenerator ID="pdfgenerator" runat="server"/>
</form>
Then in ~\controls\pdfgenerator.ascx
<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="iTextSharp.text" %>
<%@ Import Namespace="iTextSharp.text.pdf" %>
<%@ Import Namespace="iTextSharp.text.html" %>
<%@ Import Namespace="iTextSharp.text.html.simpleparser" %>
<%@ Import Namespace="log4net" %>
<script runat="server">
// Setup logging
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private void buttonPDF_Click(object sender, System.EventArgs e)
{
//Get page URL
string currentURL = Request.Url.ToString();
//Create new objects
MemoryStream MStream = new MemoryStream();
Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
StringWriter strWriter = new StringWriter();
//Create HTML Text Writer allowing us to write HTML
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(strWriter);
//Render page here??
StringReader sr = new StringReader(strWriter.ToString());
//Attempt to create writer that listens to the document
try
{
HTMLWorker htmlparser = new HTMLWorker(document);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
htmlparser.Parse(sr);
document.Close();
}
//If unsuccessful throw an exception
catch
{
Log.Debug("There has been an error creating a PDF");
}
//Clear headers, set content type to PDF and create filename
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + currentURL + ".pdf");
HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
HttpContext.Current.Response.End();
}
</script>
<asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>
So I don't think dvHtml.RenderControl(hw); will work?
billy_111
Member
333 Points
878 Posts
PDF not opening as part of iTextSharp API
Mar 25, 2012 01:15 AM|LINK
Hi,
I am using the following code on button click to return the current web page as a pdf:
<%@ Control Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.UI" %> <%@ Import Namespace="System.Web.UI.WebControls" %> <%@ Import Namespace="System.Collections.Generic" %> <%@ Import Namespace="System.Linq" %> <%@ Import Namespace="System.Text.RegularExpressions" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="iTextSharp.text" %> <%@ Import Namespace="iTextSharp.text.pdf" %> <%@ Import Namespace="iTextSharp.text.html" %> <%@ Import Namespace="iTextSharp.text.html.simpleparser" %> <%@ Import Namespace="iTextSharp.text.xml" %> <script runat="server"> private void buttonPDF_Click(object sender, System.EventArgs e) { string currentURL = Request.Url.ToString(); //Set content type to PDF Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=" + currentURL + ".pdf"); //Create new objects Document pdfDocument = new Document(); StringWriter strWriter = new StringWriter(); //Create HTML Text Writer allowing us to write HTML HtmlTextWriter htmlTextWriter = new HtmlTextWriter(strWriter); //Create a writer that listens to the document PdfWriter.GetInstance(pdfDocument, Response.OutputStream); pdfDocument.Open(); //Create String Reader to read parts of strings StringReader strReader = new StringReader(strWriter.ToString()); //Create HTML Worker for parsing simple HTML HTMLWorker htmlWorker = new HTMLWorker(pdfDocument); htmlWorker.Parse(strReader); //Stop processing script and return current result Response.End(); } </script> <asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>The pdf is being created successfully but Adobe Acrobat reader says it cannot open the file? I think the file is corrupt and not correct?
Can anybody please help?
Thanks,
billy_111
Member
333 Points
878 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 03:06 AM|LINK
I think I am missing adding to the pdfDocument file? Any help would be much appreciated...
paritoshmmec
Participant
1555 Points
304 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 03:16 AM|LINK
You are not writing anything in stream that why this error is happening,
StringWriter strWriter = new StringWriter();
strWriter.Write("I am Foobar");
Let me know if it works
Thanks and Regards,
Paritosh
shivalthakur
Participant
1839 Points
532 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 03:21 AM|LINK
Once a file open it should be closed.
try
Response.Write("Success");
Best Of Luck
Shival Thakur
billy_111
Member
333 Points
878 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 03:32 AM|LINK
I tried adding strWriter.Write("I am a Foobar");
But that still give me the same error when trying to open the document...
When i open in Notepad++ I can only see:
%PDF-1.4
%âãÏÓ
Any ideas?
billy_111
Member
333 Points
878 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 03:33 AM|LINK
Also when I add a pdfDocument.Close() when I click on the button click, I get a webpage not found message?
billy_111
Member
333 Points
878 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 11:02 AM|LINK
I have nearly got this working, I ghav changed to code to the below:
<%@ Control Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.UI" %> <%@ Import Namespace="System.Web.UI.WebControls" %> <%@ Import Namespace="System.Collections.Generic" %> <%@ Import Namespace="System.Linq" %> <%@ Import Namespace="System.Text.RegularExpressions" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="iTextSharp.text" %> <%@ Import Namespace="iTextSharp.text.pdf" %> <%@ Import Namespace="iTextSharp.text.html" %> <%@ Import Namespace="iTextSharp.text.html.simpleparser" %> <%@ Import Namespace="iTextSharp.text.xml" %> <%@ Import Namespace="log4net" %> <script runat="server"> // Setup logging private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private void buttonPDF_Click(object sender, System.EventArgs e) { //Get page URL string currentURL = Request.Url.ToString(); //Create new objects MemoryStream MStream = new MemoryStream(); Document document = new Document(); //Attempt to create writer that listens to the document try { PdfWriter writer = PdfWriter.GetInstance(document, MStream); document.Open(); document.Add(new iTextSharp.text.Paragraph("This is a test and must work")); document.Close(); } //If unsuccessful throw an exception catch { Log.Debug("There has been an error creating a PDF"); } //Set content type to PDF and create filename HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + currentURL + ".pdf"); HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer()); HttpContext.Current.Response.End(); } </script> <asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>When I click the button a PDF is created and the text appears "This is a test and must work"
Now all I need to do is instead of writing out that text, I need to return the current web page as a PDF. So this line:
document.Add(new iTextSharp.text.Paragraph("This is a test and must work"));I need to render out the page, how can I do this?
Is this possible?
billy_111
Member
333 Points
878 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 12:52 PM|LINK
Any ideas on my updated code? I just need to somehow export the web page and add it to the document?
If I add this:
//Attempt to create writer that listens to the document try { strWriter.Write("Hello"); PdfWriter writer = PdfWriter.GetInstance(document, MStream); document.Open(); document.Add(new iTextSharp.text.Paragraph(strWriter.ToString())); document.Close(); }The PDF opens and shows "Hello", so I just need to change this for the webpage content somehow...
How can I do this?
paritoshmmec
Participant
1555 Points
304 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 01:56 PM|LINK
wrap your html content around a div or panel which can give you an inner html(if you have already then don't put it)Something like this:-
<div id="dvParentControl" runat=server>
//html control
</div>
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Thanks and Regards,
Paritosh
billy_111
Member
333 Points
878 Posts
Re: PDF not opening as part of iTextSharp API
Mar 25, 2012 02:35 PM|LINK
Hi,
The problem is that I am using this as a control. So I have one control where I reference this control like so...
~\controls\controlA.ascx
<!-- User control for PDF generator --> <form runat="server" id="pdfFormTag" action="index.aspx" method="post"> <rcuk:pdfgenerator ID="pdfgenerator" runat="server"/> </form>Then in ~\controls\pdfgenerator.ascx
<%@ Control Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.UI" %> <%@ Import Namespace="System.Web.UI.WebControls" %> <%@ Import Namespace="System.Collections.Generic" %> <%@ Import Namespace="System.Linq" %> <%@ Import Namespace="System.Text.RegularExpressions" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="iTextSharp.text" %> <%@ Import Namespace="iTextSharp.text.pdf" %> <%@ Import Namespace="iTextSharp.text.html" %> <%@ Import Namespace="iTextSharp.text.html.simpleparser" %> <%@ Import Namespace="log4net" %> <script runat="server"> // Setup logging private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private void buttonPDF_Click(object sender, System.EventArgs e) { //Get page URL string currentURL = Request.Url.ToString(); //Create new objects MemoryStream MStream = new MemoryStream(); Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f); StringWriter strWriter = new StringWriter(); //Create HTML Text Writer allowing us to write HTML HtmlTextWriter htmlTextWriter = new HtmlTextWriter(strWriter); //Render page here?? StringReader sr = new StringReader(strWriter.ToString()); //Attempt to create writer that listens to the document try { HTMLWorker htmlparser = new HTMLWorker(document); PdfWriter.GetInstance(document, Response.OutputStream); document.Open(); htmlparser.Parse(sr); document.Close(); } //If unsuccessful throw an exception catch { Log.Debug("There has been an error creating a PDF"); } //Clear headers, set content type to PDF and create filename HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + currentURL + ".pdf"); HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer()); HttpContext.Current.Response.End(); } </script> <asp:Button runat="server" ID="buttonPDF" OnClick="buttonPDF_Click" CssClass="button" Text="PDF"></asp:Button>So I don't think dvHtml.RenderControl(hw); will work?
Any ideas how I can do this?
Thanks