ReportViewer to PDF inside div/image/object

2019-03-04 18:20发布

问题:

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

回答1:

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?