【发布时间】:2015-01-31 13:36:39
【问题描述】:
我有一个带有 html 的字符串生成器,其中包含表和数据,从数据库加载的图像。 无论如何我可以使用asp.net将html字符串直接导出为pdf。
谢谢
【问题讨论】:
-
你找过iTextSharp吗?
我有一个带有 html 的字符串生成器,其中包含表和数据,从数据库加载的图像。 无论如何我可以使用asp.net将html字符串直接导出为pdf。
谢谢
【问题讨论】:
您可以使用 iTextSharp 将 HTML 字符串导出为 PDF。
代码如下:
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Protected Sub imgViewPDF_Click(sender As Object, e As ImageClickEventArgs)
Using sw As New StringWriter()
Using hw As New HtmlTextWriter(sw)
Dim sb As New StringBuilder()
sb.Append("<html xmlns='http://www.w3.org/1999/xhtml'>")
sb.Append("<head>")
sb.Append("</head>")
sb.Append("<body>")
sb.Append("</body>")
sb.Append("</html>")
Dim sr As New StringReader(sb.ToString())
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=ExportedClientDocument.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim pdfDoc As New Document(PageSize.A4, 42F, 40F, 90F, 50F)
Dim htmlparser As New HTMLWorker(pdfDoc)
Dim pdfWriter__1 As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
End Using
End Using
End Sub
【讨论】: