0

Been searching for the past couple of days and haven't been able to find what I'm looking for, hopefully I haven't missed it.

I have an ASP.NET (4.0) site that I'm putting together to retrieve payroll information. Currently I'm using the reportviewer, but because of cross-browser support it doesn't work 100%. I already have it set up to automatically render the RV into a PDF, turning it into bytes.

I have code to A) Open the PDF as a standalone document B) Open the PDF in a new window

What I want to accomplish is open the PDF within the same page, within a div/table/image/other object... And that's where I'm stumped. VB code I currently have is below...

        Dim bytes As Byte()

        Dim warnings As Warning()
        Dim streamIds As String()
        Dim mimeType As String = Nothing
        Dim encoding As String = Nothing
        Dim extension As String = Nothing
        Dim filename As String = "PayDetail"

        bytes = rvPayroll.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamIds, warnings)

        ' ***** AUTOMATICALLY ASK THE USER TO SAVE/OPEN PDF
        'Response.Buffer = True
        'Response.Clear()
        'Response.ContentType = mimeType
        'Response.AddHeader("content-disposition", "inline; filename=" & filename & "." & extension)
        'Response.BinaryWrite(bytes)
        'Response.Flush()

        ' ***** OPEN PDF AS NEW WEB PAGE
        'Response.BufferOutput = True
        'Response.ClearContent()
        'Response.ClearHeaders()
        'Response.ContentType = mimeType
        'Response.AddHeader("Content-Length", bytes.Length.ToString)
        'Response.AddHeader("content-disposition", "inline;filename=PayDetail.pdf")
        'Response.ContentType = "applicatin/pdf"
        'Response.BinaryWrite(bytes)
        'Response.Flush()
        'Response.Clear()

I have tried turning the PDF into an image and display, but received nothing in the image box. Tried that using:

        Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)

        Image1.ImageUrl = "data:image/png;base64," & base64String

        Image1.Visible = True
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Joe
  • 3
  • 2
  • I'm just assuming that you're running this code directly on the Page. That's not a good idea. Instead, you should put the code for generating the PDF and writing the response into a Generic Handler (.ashx) file. Then you'll [embed the PDF](http://stackoverflow.com/questions/291813/recommended-way-to-embed-pdf-in-html) in your web page, with the URL set to point to the .ashx file. – mason Sep 30 '14 at 13:24
  • I'm pretty new to the web page building... please enlighten me on why running the code directly in the page is a bad idea... I will definitely start looking into and learning the Generic handler stuff... – Joe Sep 30 '14 at 13:27
  • Because the PDF is a different document from the HTML. How are you going to get the PDF onto the page if the same code responsible for generating the HTML is also doing the PDF? – mason Sep 30 '14 at 13:31

1 Answers1

0

First, create a Generic Handler called GeneratePDF.ashx. Note I didn't write the below code and I'm not a VB.NET programmer, so I can't guarantee it'll work.

'generate bytes, perhaps based on Request.QueryString parameters
`write bytes to output
Response.Buffer = True
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "inline; filename=" & filename & "." & extension)
Response.BinaryWrite(bytes)
Response.Flush()

Then on our page, we use one of the techiques from the below link to embed the PDF, making sure to give the URL that points to our generic handler, and passing any parameters needed to generate the PDF via query string.

<embed src="GeneratePDF.ashx?parameter1=asdf&parameter2=qwer" width="500" height="375" type="application/pdf" />

Above code adapted from Recommended way to embed PDF in HTML?

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • I believe I've got where you're pointing to... just trying to get there the rest of the way. The ashx parameters will be dynamic, specifically from A) a listbox and B) a session variable. So how do I pass those 2 over to the ashx for processing? – Joe Sep 30 '14 at 16:01
  • @Joe You just manipulate the `src` attribute of the `embed` element to include the query string parameters (ex: `?parameter1=asdf&parameter2=qwer`). This should probably be done on the client side in JavaScript. But if you don't know how then you could do it on the server by adding `runat="server"` to the `embed` element and then manipulating on the server side upon each postback. – mason Sep 30 '14 at 17:12
  • Thanks for the help thus far, think I only have one last hurdle to get past... rendering. I'm calling the ashx as I should be, I know the data is getting pulled and the bytes are being created from the PDF, but I can't get the PDF to display within the embed. All content types are set to application/pdf. – Joe Oct 01 '14 at 14:04
  • Whoa - so apparently it does render, depending on the browser. I was testing everything in IE, but as soon as I tested in Chrome I'm good. Any thoughts on why IE wouldn't want to cooperate? – Joe Oct 01 '14 at 14:09
  • Depends on your browser and document mode and your actual version of IE. Hit F12 to see what those are and let me know. – mason Oct 01 '14 at 14:14
  • Yeah well just figured out I broke it myself... forgot I told IE to run in compatibility mode for IE 9. As soon as I took that off... boom... I now have a PDF. Thank you very much for the help... appreciate it. – Joe Oct 01 '14 at 14:19