首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用HiQPdf将动态html转换为pdf

用HiQPdf将动态html转换为pdf
EN

Stack Overflow用户
提问于 2014-01-03 18:21:04
回答 2查看 2.6K关注 0票数 0

一段时间以来,我一直试图让我的HTML准确地翻译成PDF格式,但我看不出我做错了什么。

这是我的页面代码:

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

发布于 2014-01-03 19:39:54

请先试一试:

  1. 增加明确: Response.Clear() Response.ClearHeaders()
  2. 后MyBase.Reder法 htw.Flush()
  3. 在前面的Response.End Response.Flush()
  4. 如果上面什么都没有用的话: 电话支援:)
票数 0
EN

Stack Overflow用户

发布于 2014-01-04 02:09:21

我认为问题在于,在页面呈现后(使用JavaScript)您正在更改该页面的状态,并且您期望:-

代码语言:javascript
复制
    MyBase.Render(htw)

    'get the current page html code

若要向您提供页面的当前状态,请执行以下操作。它不会-它会给你页面的状态,因为它是呈现的。如果在页面加载后使用Knockout或任何其他脚本来操作DOM,则页面的服务器端模型对这些更改一无所知。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20910256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档