首页 > 解决方案 > 尝试将 gridview 转换为 PDF 文档

问题描述

基本原则是我试图将 GridView 转换为 PDF 文档。GridView 包括复选框等控件和一些带有一些颜色的 css 类。

我试过把它放在一起,但我想我误解了一些东西是如何工作的,现在走到了死胡同。目前它全部编译并运行,但是当您尝试打开返回的 PDF 时,它说它无法加载。返回的文件大小为 1KB,所以我猜它实际上不是有效的 PDF。

我正在使用 iText7 和 htmlPDF,我的代码看起来像这样。

Protected Sub ExportToPDF(sender As Object, e As ImageClickEventArgs)
    Using sw As New StringWriter()
        Using hw As New HtmlTextWriter(sw)
            Response.ContentType = "application/pdf"
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
            Response.Cache.SetCacheability(HttpCacheability.NoCache)

            gvCSC.DataBind()
            gvCSC.RenderControl(hw)

            Dim pdfWriter As New PdfWriter(Response.OutputStream)
            Dim pdfDoc As New PdfDocument(pdfWriter)
            Dim sr As New StringReader(sw.ToString)
            pdfDoc.SetDefaultPageSize(New PageSize(PageSize.A4.Rotate))
            Dim doc As Document = HtmlConverter.ConvertToDocument(sr.ToString, pdfDoc, New ConverterProperties)

            Response.End()

            pdfDoc.Close()
        End Using
    End Using
End Sub

我希望我只是愚蠢,因为我以前没有使用过 iText7,它只需要有人指出对我来说显而易见的事情。解决此问题的任何帮助将不胜感激。

标签: asp.netitext7pdfhtml

解决方案


推荐阅读