我一直试图让我的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时,它就不会反映出来。
答案 0 :(得分:0)
请先尝试:
添加明确:
Response.Clear()
Response.ClearHeaders()
MyBase.Reder方法之后
htw.Flush()
在Response.End之前
Response.Flush()
如果上述情况无效:
致电支持:)
答案 1 :(得分:0)
我认为问题在于您在呈现页面后使用JavaScript更改页面状态,并且您期望这样: -
MyBase.Render(htw)
'get the current page html code
为您提供页面的当前状态。它不会 - 它将为您提供呈现的页面状态。如果您在页面加载后使用Knockout或任何其他脚本操作DOM,则页面的服务器端模型不知道这些更改。