使用 c# 和 asp.net 将 HTML 内容转换为 Pdf
本文关键字:转换 Pdf HTML asp net 使用 | 更新日期: 2023-09-27 18:36:47
在这里,我试图将htmldiv标签中的内容转换为pdf,我发现了以下错误:
Input string was not in a correct format is occur
这是我尝试使用 c# 的代码:
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();
//head.WriteSelectedRows(0, -1, page.Width / 8, page.Height - pdfDoc.TopMargin + head.TotalHeight, writer.DirectContent);
//pdfDoc.Add(headerTbl);
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
------------------------------------------
这是我的 html 代码
<div id="divexcel" runat="server">
<table id="tbDetails" width="800px">
<thead >
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
尝试在 width="800" 上替换 width="800px"。应该工作