ReportViewer到div / image / object里面的PDF

时间:2014-09-30 13:01:00

标签: asp.net vb.net reportviewer

一直在寻找过去的几天,我们找不到我想要的东西,希望我没有错过它。

我有一个ASP.NET(4.0)网站,我将它放在一起检索工资单信息。目前我正在使用reportviewer,但由于跨浏览器支持,它不能100%工作。我已将其设置为自动将RV渲染为PDF,将其转换为字节。

我有代码 A)将PDF作为独立文档打开 B)在新窗口中打开PDF

我想要完成的是在同一页面内,在div / table / image /其他对象中打开PDF ......以及那些我难以理解的地方。我目前拥有的VB代码如下......

        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()

我尝试将PDF转换为图像和显示,但在图像框中没有收到任何内容。尝试使用:

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

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

        Image1.Visible = True

1 个答案:

答案 0 :(得分:0)

首先,创建一个名为GeneratePDF.ashx的Generic Handler。注意我没有编写下面的代码,而且我不是VB.NET程序员,所以我不能保证它能够正常工作。

'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()

然后在我们的页面上,我们使用以下链接中的一个技术来嵌入PDF,确保提供指向我们的通用处理程序的URL,并传递通过查询字符串生成PDF所需的任何参数。< / p>

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

以上代码改编自Recommended way to embed PDF in HTML?