0

I am trying to write a GridView, gv to a .pdf file.

Having problems with pdfDoc.

    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=Export.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    Dim frm As New HtmlForm()
    gv.Parent.Controls.Add(frm)
    frm.Attributes("runat") = "server"
    frm.Controls.Add(gv)
    frm.RenderControl(hw)
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.[End]()
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
teledextri
  • 125
  • 1
  • 3
  • 14
  • That is a bad idea. Instead, write custom HTML to the PDF, or use native PDF controls. The reason for this is simple: the markup for a GridView is intended for a webpage, and will not render cleanly in a PDF. And anyways, if you're having a problem exporting it, say exactly what the problem is in your question. Don't just say "having problems with pdfDoc". Are you getting an exception? At what line is it occurring? Otherwise, how is the output differing from what you expect? – mason May 27 '14 at 20:29
  • Look through these answers. I highly recommend breaking your process into three parts, render HTML to string, parse string to PDF, send PDF to client. http://stackoverflow.com/a/17171791/231316 http://stackoverflow.com/a/21466653/231316 http://stackoverflow.com/a/23703266/231316 – Chris Haas May 28 '14 at 05:33

1 Answers1

0

Why don't you loop through your Gridview rows or your datasource items and create an html string?

Sev
  • 761
  • 4
  • 16
  • 29