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.
Related
I want to export asp.net page to pdf.My web page is a form where user enters some data in text boxes and radio buttons.I tried to export to PDF using Itextsharp dll but alignment is not at all perfect.And am not getting radio buttons in PDF.If I take only selected item from radio buttons list and displayed in a label then huge alignment problem.I have used below code,plz check and suggest me.
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
dt.Columns.Add("REquestType");
dt.Columns.Add("Name");
dt.Rows.Add();
if (rbtlstRqstType.SelectedItem != null)
{
dt.Rows[0]["REquestType"] = rbtlstRqstType.SelectedItem.Text;
}
dt.Rows[0]["Name"] = txtFname.Text;
lbllstRqstType.Text = dt.Rows[0]["REquestType"].ToString();
lblName.Text = dt.Rows[0]["Name"].ToString();
}
try
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=IDform.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
rbtlstRqstType.Visible = false;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//this.Page.RenderControl(hw);
pnlIDForm.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();
}
Create a PDF template, containing a form.
Add the form fields, and then populate the form fields using C# and iTextSharp, something like:
acroFields.SetField("CustomerName", customer.Name);
acroFields.SetField("BranchName", customer.Branch);
You can use Rotativa which is design to generate PDF in MVC application.
You can get all documents regarding methods and demo from https://github.com/webgio/Rotativa
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
We are unable to change font size of pdf generated from below code anybody can help us?
We would like to convert that html file into pdf after changing the font size.
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
protected void ConvertToPDFNow()
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
print.RenderControl(w);
string htmWrite = sw.GetStringBuilder().ToString();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", "");
htmWrite = htmWrite.Replace("\r\n", "");
StringReader reader = new StringReader(htmWrite);
Document doc = new Document(PageSize.A4);
//Creating Document of A4 Size
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
try
{
//rendering Html File
parser.Parse(reader);
}
catch (Exception ex)
{
}
finally
{
doc.Close();
}
}
try to apply stylesheet:
...
var style = new StyleSheet();
style.LoadTagStyle("body", "size", "12px");
parser.SetStyleSheet(style);
...
The C# code below works fantastically to change itextpdf font size:
var style = new StyleSheet();
style.LoadTagStyle("body", "size", "8px");
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.SetStyleSheet(style);
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();
}
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");