我正在尝试使用itextsharp将html内容转换为pdf。我有版本5.5.4的xmlworker和itextsharp。在尝试执行代码时,在C:/ Test中提到的路径中生成pdf文件,但无法打开该文件。我正在使用外部css文件,下面也会给出参考。
body {margin: 0;font-family: Arial;font-size: 10px;}
table {border-collapse: collapse;font-size: 13px; font-weight:bold; width:100%; border-color:black;}
.NoTableTop{border-top:none;}
tr.NoTop td{border-top:none;}
tr.NoBot td {border-bottom-color:white;}
Dim sb As New System.Text.StringBuilder()
sb.Append("<html xmlns=""http://www.w3.org/1999/xhtml"">")
sb.Append("<head><title></title></head><body>")
sb.Append("<table cellpadding=0 cellspacing=0 border=""1""> ")
sb.Append("<tr><td>")
sb.Append("<div style=""float:left;"" width=""50%""> <img style=""padding-top:3px;"" src=""C:/image1.jpg"" width=70 height=75 style=""vertical-align:middle;"" /></div>")
sb.Append("<div style=""float:right; padding:8px""><img src=""c:/images/Logo.jpg"" width=""400"" height=""70"" /></div>")
sb.Append("</td></tr>")
sb.Append("<tr style=""vertical-align:middle;"">")
sb.Append("<td style=""padding:3px; vertical-align:middle;"">")
sb.Append("<div align=""center"" width=""50%"">Company Invoice - JAN 2015")
sb.Append("</div></td></tr><tr><td>")
sb.Append("<div align=""center"" width=""50%"">Invoice Details</div>")
sb.Append("</td></tr></table>")
sb.Append("<table cellpadding=0 cellspacing=0 border=""1"" class=""NoTableTop"">")
sb.Append("<tr class=""NoTop"">")
sb.Append("<td width=""15%""><div align=""center""> Name of the Company</div></td>")
sb.Append("<td> Company1</td>")
sb.Append("<td><div align=""center""> No.</div></td>")
sb.Append("<td><div align=""center"">12345</div></td></tr>")
sb.Append("<tr><td><div align=""center""> Department</div></td>")
sb.Append("<td colspan=""3""><div> Dept 1</div></td></tr></table>")
我有两个用于生成PDF的代码
代码1
Dim html As New StringReader(sb.ToString)
Using Stream As New MemoryStream
Using doc As New iTextSharp.text.Document(PageSize.A4, 10, 10, 10, 10)
Using writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, Stream)
writer.CloseStream = False
Dim xmlworker = XMLWorkerHelper.GetInstance
doc.Open()
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, html)
doc.Close()
writer.Close()
Stream.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", String.Format("inline;filename={0}.pdf", "filename"))
Response.BufferOutput = False
Response.BinaryWrite(Stream.ToArray())
Response.Flush()
Response.Close()
Response.End()
End Using
End Using
End Using
代码2
Dim Document As New iTextSharp.text.Document(PageSize.A4, 10, 10, 10, 10)
Dim PdfWriter As PdfWriter = PdfWriter.GetInstance(Document, New FileStream("C:/Test/Jan15.pdf", FileMode.Create))
Dim HtmlContext As New HtmlPipelineContext(Nothing)
HtmlContext.SetTagFactory(iTextSharp.tool.xml.html.Tags.GetHtmlTagProcessorFactory)
Dim CSSResolver As ICSSResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(True)
'--- The location of the external CSS file
Dim BCMiCss As String = HttpContext.Current.Server.MapPath("~/style/style12.css")
'--- Add the external CSS file
CSSResolver.AddCssFile(BCMiCss, True)
Dim Pipeline As IPipeline = New CssResolverPipeline(CSSResolver, New HtmlPipeline(HtmlContext, New PdfWriterPipeline(Document, PdfWriter)))
Dim Worker As XMLWorker = New XMLWorker(Pipeline, True)
Dim Parser As XMLParser = New XMLParser(True, Worker)
Parser.Parse(New StringReader(sb.ToString))
Parser.Flush()
代码2不使用外部文件。我尝试在字符串生成器“sb”中添加样式标记。没有任何效果。
sb.Append("<style type=""text/css"">")
sb.Append("body {margin: 0;font-family: Arial;font-size: 10px;}")
sb.Append("table {border-collapse: collapse;font-size: 13px; font-weight:bold; width:100%; border-color:black;}")
sb.Append(".NoTableTop{border-top:none;}")
sb.Append("tr.NoTop td{border-top:none;}")
sb.Append("tr.NoBot td {border-bottom-color:white;}")
sb.Append("</style>")
我坚持这份报告。