This question already has answers here:
Convert HTML to PDF in .NET [closed]
(26 answers)
Closed 9 years ago.
Can anybody help me how to export Repeater data to PDF file without using iTextSharp.
I am generating PDF files from Repeater control using iTextSharp, it works properly but are there any other suggestions on how to export repeater data to a PDF document?
use the below code.Hope it will resolve your issue.
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
protected void BtnExportToPdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Repeater1.DataBind();
Repeater1.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();
}
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 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 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.
I am trying to find an html parser that can parse an html page even with jquery and convert it to pdf.
previously, i have been using the following code to export an html file to pdf:
using System;using System.Web;
using System.Web.UI;
using System.Data;
using System.IO;using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
protected void btnPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.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, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
But by using iTextSharp dll , will i be able to convert an html page that has Jquery UI in it into a pdf?
I ran out of ideas. can any one please give inputs or sugesstions how can we achieve this?
First of all, i would like to know whether this can be achieved at all? is there any dll that has capability to convert an html page(Jquery UI) to pdf?
Evo PDF is a commercial software library that can handle this for you.
If you do not mind to use a command line tool, you can have a look at wkhtmltopdf
i used jspdf.debug.js to solve this ..
function pdfFromHtml(element,gridConfig) {
getHtmlForExport(element,gridConfig,"pdf")
var source='<table>'+$('#exportTable').html()+'</table>';
var pdf = new jsPDF('l', 'pt', 'a0');
var margins = {
top: 60,
bottom: 10,
left: 10,
width: 720
};
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, {// y coord
'width': margins.width, // max width of content on PDF
},
function (obj) {
var elementClicked = element;
var headerText = $(elementClicked).text();
var fileName;
if (elementClicked == undefined || headerText == undefined) {
fileName = "pdfExport"
} else {
fileName = headerText.toString().trim()
}
pdf.setFontSize(22);
pdf.setFontType("bold");
pdf.text(40, 40, fileName+" grid");
pdf.save(fileName + '.pdf');
}, margins);
}
getHtmlForExport(element,gridConfig,"pdf") is the function to get plain html from the table which you want to export to pdf ....
Note
CSS won't be supported as far as i know.
I'm trying to print a panel which contains data like labels and GridView to a PDF using itextsharp in my webpage. But if i'm not able to apply the styles to the GridView.
Can you help me??
I'm using both css and html styles to style my GridView.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnl_print.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();
sr.Close();
hw.Close();
sw.Close();
How can I add styles to Gridview in Panel??
Panel ID is pnl_print
GridView ID is gv1
Can you Help me out ??
I am not sure if it possible to add an extranal css to the pdf with itextsharp, but you can always create a css with a functions frovided by itextsharp using something like this, rewriting the style in the itextsharp css class.
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("p", "size", "10pt");
styles.LoadTagStyle("p", "color", "#0000ff");