一段时间以来,我一直试图让我的HTML准确地翻译成PDF格式,但我看不出我做错了什么。
这是我的页面代码:
Imports HiQPdf
Imports System.Text
Imports System.IO
Imports System.Web.UI
Partial Class MODULES_CostCalculator_CostCalculator
Inherits System.Web.UI.Page
Dim convertToPdf As Boolean = False
Protected Sub printClick()
convertToPdf = True
End Sub
Protected Overrides Sub Render(writer As System.Web.UI.HtmlTextWriter)
If (convertToPdf) Then
System.Diagnostics.Debug.Write("overriding render")
Dim tw As TextWriter = New StringWriter()
Dim htw As HtmlTextWriter = New HtmlTextWriter(tw)
'render the html markup into the TextWriter
MyBase.Render(htw)
'get the current page html code
Dim htmlCode As String = tw.ToString()
System.Diagnostics.Debug.Write(htmlCode)
'convert the html to PDF
'create html to pdf converter
Dim htmlToPdfConv As HtmlToPdf = New HtmlToPdf()
'htmlToPdfConv.MediaType = "print"
'base url used to resolve images, css and script files
Dim currentPageUrl As String = HttpContext.Current.Request.Url.AbsoluteUri
'convert html to a pdf memory buffer
Dim pdfBuffer As Byte() = htmlToPdfConv.ConvertHtmlToMemory(htmlCode, currentPageUrl)
'inform the browser about the binary data format
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf")
'let the browser know how to open the pdf doc
HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("attachment; filename=ConvertThisHtmlWithState.pdf; size={0}",
pdfBuffer.Length.ToString()))
'write the pdf buffer to http response
HttpContext.Current.Response.BinaryWrite(pdfBuffer)
'call End() method of http response to stop ASP.NET page processing
HttpContext.Current.Response.End()
Else
MyBase.Render(writer)
End If
End Sub有人看到我做错什么了吗?很多HTML都链接到了一个Knockout ViewModel,所以我不确定这是否会导致问题。
为了清楚起见,我可以创建页面的PDF格式,但是只有当页面第一次加载时,HTML才会处于状态。如果我更改任何数据绑定HTML,它不会反映当我试图再做一个PDF。
发布于 2014-01-03 19:39:54
请先试一试:
发布于 2014-01-04 02:09:21
我认为问题在于,在页面呈现后(使用JavaScript)您正在更改该页面的状态,并且您期望:-
MyBase.Render(htw)
'get the current page html code若要向您提供页面的当前状态,请执行以下操作。它不会-它会给你页面的状态,因为它是呈现的。如果在页面加载后使用Knockout或任何其他脚本来操作DOM,则页面的服务器端模型对这些更改一无所知。
https://stackoverflow.com/questions/20910256
复制相似问题