1
votes

Basic principle is that I am trying to take a GridView and turn it into a PDF document. The GridView includes controls like checkboxes and some css classes with some colouring.

I have tried putting this together, but I think I am misunderstanding how something works and now hit a dead end. At present it all compiles and runs, but when you try and open the returned PDF it says it fails to load. The returned file is 1KB in size, so I am guessing that it isn't actually a valid PDF.

I am using iText7 and htmlPDF, with my code looking like this.

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

I am hoping that I am just being stupid as I've not used iText7 before and it just needs someone to point out the obvious for me. Any help in solving this will be appreciated.