I am using itextsharp dll to print web page in PDF . I have a <img src="Images/flower.jpg" /> in my html file. The button click event below will render PDF fine when I don't have the image. With the image, I got this error:
Could not find a part of the path 'C:\Program Files (x86)\Common
Files\Microsoft Shared\DevServer\10.0\Images\flower.jpg'
Here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
The easiest solution should be to change:
<img src="Images/flower.jpg" />
to:
<img src="http://www.yourwebsite.com/Images/flower.jpg" />
Otherwise you can use Server.MapPath("Images/flower.jpg") to get the full URL by code. However that implies you having to know the relative path to the image (meaning access to the raw html) at compile-time, which makes your code less maintainable.
Related
If I do onClick of a button(btnConverttoPDF) in some form it will
create PDF with all the content inside the form..
It's working fine locally.. But when the application is deployed in server then
it's raising an error.. as follows
"No connection could be made because the target machine actively
refused it 127.0.0.15:55374" WebException: unable to connect to the
remove server and mentioning the path of the file like
E:\Main\OnlineSite\OnlineBanking\ClosingBalance.aspx.cs
Just now I found the issue..
the Image control is causing an issue.. during
htmlparser.Parse(sr);
Here is the Code which I've tried
protected void btnconvert_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//To Export all pages
grdLast10trans.AllowPaging = false;
BindGrid(); //Calling a Function, here im adding the data to the Datasource and diplaying in Grid
content.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A3, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//pdfDoc.NewPage();
htmlparser.Parse(sr); //Here it's causing error b'coz of image contol
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
I am attempting to convert a tag Label of my webpage to pdf.
While the pdf generation is working correctly, none of the css styles are being applied.
I've tried applying the styles one at a time, but that doesn't seem to work.
How can we create a pdf file from html with textbox controls with CSS styles ?
My code below.
Please help me, thank you in advance.
<asp:Label ID="UserID" runat="server" BorderStyle="Double"></asp:Label>
private void PdfFiles()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Decrypt(Request.QueryString["id"].ToString()) + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
panelView.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 40f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.End();
}
i have a header at the top of my front page like this:
i use iTextSharp code to generate its PDF ... but the resulting PDF do not contain this Header with black : instead some CSS is written in place of header like this:
How can i possibly fix this issue??
Code:
protected void BtnPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Have you tried to use inline css? Because HTML/CSS parser in iTextSharp is not complete. So it may not work as you want.
Also check out the LoadTagStyle property of StyleSheet in iTextSharp, see if it helps.
For more details about it, see this answer: https://stackoverflow.com/a/9616429/604232
I have one Gridview in my asp .Net project (c#) and i want to export it to pdf
for this purpose i'm using iTextSharp as bellow:
public override void VerifyRenderingInServerForm(Control control)
{ /* Do nothing */ }
protected void Button2_Click1(object sender, EventArgs e)
{
Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //ma
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
ASPxGridView1.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();
Response.Write(pdfDoc);
Response.End();
}
when i test it , i get this error:
The UNC path should be of the form \server\share
anybody pls help me?
This is my code :
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
**htmlparser.Parse(sr);** //the exception here
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
the error is :
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type
'iTextSharp.text.Paragraph'.
What is this exception ?
I don't know if this is the answer, but I found that iTextSharp is picky about having valid html. My tests had a table that was opened twice and never closed and that took about an hour for me to notice. The exception was very similar to the one you have there.
use can you A4 size width & height for html deigning .
use this set od code to create pdf from html.
String htmlText = "<div>Your HTML text here</div>";
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\outfile.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=outfile.pdf");
Response.ContentType = "application/pdf";
Response.WriteFile(Request.PhysicalApplicationPath + "\\outfile.pdf");
Response.Flush();
Response.Clear();