使用 c# 和 asp.net 将 HTML 内容转换为 Pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19603899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert HTML content to Pdf using c# and asp.net
提问by Velu
Here i am trying to convert the content inside html div tag to pdfi found the following error:
在这里,我试图将html div 标签内的内容转换为 pdf我发现以下错误:
Input string was not in a correct format is occur
出现输入字符串格式不正确
Here is the code i tried using c#:
这是我尝试使用 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 += "<tr><td><table width='200px'><tr><td align='center'><img src=" + id + " /></td></tr><tr><td align='center'>" + Name + "</td></tr></table></td><td><table width='600px'><tr><td align='left' style='border:1px solid blue;border-radius:7px;box-shadow: 10px 0 10px #888888; padding: 8px 6px 0 7px;'>Features: <br/><p style='margin-top: 10px;'>" + Pass + "</p></td></tr></table></td></tr>";
}
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:
这是我的 html 代码:
<div id="divexcel" runat="server">
<table><tr><td><asp:Button ID="button" runat="server" Text="Submit"
onclick="button_Click" /></td></tr></table>
<table align="center" width="100%">
<tr>
<td>
<table>
<tr><td align="center">ID</td></tr>
<tr><td align="center">Name </td></tr>
</table>
</td>
<td>
<table>
<tr><td align='left'>Features: <br/><p>Pass</p></td></tr>
</table>
</td>
</tr>
<%=getWhileLoopData()%>
</table>
</div>
采纳答案by Monika
Try to replace width='200px' with width='200' it should work. Remove px from every places with width.
尝试用 width='200' 替换 width='200px' 它应该可以工作。从每个有宽度的地方删除 px。
回答by Merbin Joe
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
我建议不要使用htmlworker,因为它是旧的,不支持css样式,所以尽量使用htmlworker安装的xmlworker。参考以下代码非常简单。
从这里参考
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 = "<style>#tdiv1{background:red;color:white;}</style>" + outXml;
outXml = outXml.Replace("px", "");
outXml = outXml.Replace("<br>", "<br/>");
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();
}