asp.net html转pdf,使用c#和asp.net将HTML内容转换为Pdf

Here i am trying to convert the content inside html div tag to pdf i found the following error:

Input string was not in a correct format is occur

Here is the code i tried using c#:

public string getWhileLoopData()

{

string htmlStr = "";

SqlConnection thisConnection = new SqlConnection("Data Source=VELU-PC\\SQLEXPRESS;Initial Catalog=EEP;Trusted_Connection=True;");

SqlCommand thisCommand = thisConnection.CreateCommand();

thisCommand.CommandText = "select * from Product_category";

thisConnection.Open();

SqlDataReader reader = thisCommand.ExecuteReader();

while (reader.Read())

{

string id = reader.GetString(6);

string Name = reader.GetString(3);

string Pass = reader.GetString(5);

htmlStr += "

+%20id%20+%20
" + Name + "
Features:

" + Pass + "

";

}

thisConnection.Close();

return htmlStr;

}

void generatetable()

{

divexcel.Visible = true;

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);

divexcel.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

Document pdfDoc = new Document(PageSize.A4, 80f, 80f, -2f, 35f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();

htmlparser.Parse(sr);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();

}

public override void VerifyRenderingInServerForm(System.Web.UI.Control control)

{ }

protected void button_Click(object sender, EventArgs e)

{

generatetable();

}

Here is my html code:

οnclick="button_Click" />

ID
Name
Features:

Pass

Talk1:

if i remove style from the line htmlStr += "

+%20id%20+%20
" + Name + "
Features:

" + Pass + "

"; means its working fine without style.

Solutions1

Try to replace width='200px' with width='200' it should work. Remove px from every places with width.

Solutions2

I recommended to don't use the htmlworker because it is the old one and won't support css styles, so try to use the xmlworker insted of htmlworker. It is very simple refer the following code.

Refer from here

protected void lnkPDF_Clicked(object sender, EventArgs e)

{

Document Doc;

Doc = new Document(PageSize.A4, 10f, 10f, 50f, 20f);

string filename = "PaySlip";

string outXml = selectedhtml.Value;

outXml = "" + outXml;

outXml = outXml.Replace("px", "");

outXml = outXml.Replace("
", "
");

MemoryStream memStream = new MemoryStream();

TextReader xmlString = new StringReader(outXml);

using (Document document = new Document())

{

PdfWriter writer = PdfWriter.GetInstance(document, memStream);

document.Open();

byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(outXml);

MemoryStream ms = new MemoryStream(byteArray);

XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, ms, System.Text.Encoding.UTF8);

document.Close();

}

Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.BinaryWrite(memStream.ToArray());

Response.End();

Response.Flush();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值