Monday, February 20, 2012

Rendering the ASP.NET local report directly to PDF

Hi,

Since it is not possible to print from a local report in ASP.Net is there anyway to directly render to a PDF or hide the Local report rendering and automatically goto the PDF either in the web page or as a popup.

Thanks

Hi,

You can try this code:

string path = @."D:/Folder/MyFile.pdf";
WebClient client = new
WebClient();
Byte[] buffer = client.DownloadData(path);

if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length",buffer.Length.ToString());
Response.BinaryWrite(buffer);
}

Hope this helps,

Vivek

|||

Can this be done on a report on a server and a parameter is passed?

For example, when a button is clicked in a page, then it will pass a parameter to SSRS and then, in turn, returns a PDF file. (So there's no need for a report viewer)

I am not sure if its possible. Please advise. Thanks!

Lloyd

|||

Lloyd,

How are you returning the PDF file? SSRS will return only data (I do not have much experience in SSRS), I have worked on Crystal Reports and it exported idrectly to PDF.

If you have data then you can format and show it to PDF. You can change the Response.ContentType as I mentioned before or use some tools like:

http://www.websupergoo.com/abcpdf-1.htm

Hope this helps,

Vivek

|||

Hi!

Thanks for the reply!

I'm thinking if this can be done. When I click the "Print" button, the report will be generated (PDF), and the standard pop up message of browser would come up, asking if the user would want to open or save the generated file.

Lloyd

|||

Lloyd,

You can first call the SSRS on the Print button click and then bind the resulting data to some data bound control, such as a DataGrid or GridView. After the bind, you can change the Response.ContentType property to "pdf". See my blog entry for details:

http://geekswithblogs.com/vivek

You can also try an Open Source PDF Report Server Control:http://sourceforge.net/projects/npdf/

Hope this helps,

Vivek

|||

Try this:

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()

Dim rs As New msfirm_db_stl.ReportingService
'rs.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim myCred As New NetworkCredential("Username", "Password", "Domain")
rs.Credentials = myCred

' Render arguments
Dim result As Byte() = Nothing
Dim reportPath As String = "/ImportedReports/" & Me.ReportName
Dim format As String = Me.ReportFormat
Dim historyID As String = Nothing
Dim devInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"

' Prepare report parameter.
Dim params() As String
params = Me.Parameters.Split("&")
Dim nParams As Integer = 0
Dim parameters() As msfirm_db_stl.ParameterValue
If params.Length > 0 And Me.Parameters <> String.Empty Then
ReDim parameters(IIf(params.Length > 0, params.Length - 1, 0))
For Each s As String In params
If s <> String.Empty Then
Dim keyval() As String = s.Split("=")
parameters(nParams) = New msfirm_db_stl.ParameterValue
parameters(nParams).Name = keyval(0)
parameters(nParams).Value = keyval(1)
nParams += 1
End If
Next
End If

Dim testparms() As msfirm_db_stl.ReportParameter = rs.GetReportParameters(reportPath, historyID, False, Nothing, Nothing)
If (testparms.Length.ToString) <> (nParams.ToString) Then
Response.Redirect(ConfigurationSettings.AppSettings.Get("ReportServerURL") & "?%2fImportedReports%2f" & Me.ReportName & "&rs%3aCommand=Render&rs%3aFormat=HTML4.0&rc%3aToolbar=True&rc%3aJavaScript=True")
End If

'Set Web Service user credentials
Dim credentials As msfirm_db_stl.DataSourceCredentials() = Nothing
Dim showHideToggle As String = Nothing
Dim encoding As String
Dim mimeType As String
Dim warnings As msfirm_db_stl.Warning() = Nothing
Dim reportHistoryParameters As msfirm_db_stl.ParameterValue() = Nothing
Dim streamIDs As String() = Nothing
Dim sh As New msfirm_db_stl.SessionHeader
rs.SessionHeaderValue = sh

Try
result = rs.Render(reportPath, format, historyID, devInfo, parameters, credentials, showHideToggle, encoding, mimeType, reportHistoryParameters, warnings, streamIDs)
sh.SessionId = rs.SessionHeaderValue.SessionId
Catch ex As SoapException

End Try
'' Write the contents of the report to an MHTML file.
Try
Response.Buffer = True
Select Case Me.ReportFormat
Case "EXCEL"
Response.ContentType = "application/vnd.ms-excel"
Case "HTML4.0", "HTMLOWC"
Response.ContentType = "text/html"
Case "PDF"
Response.ContentType = "Application/PDF"
End Select

'Response.AddHeader("Content-Disposition", "attachment;filename=" & Me.ReportName & ".pdf")
Response.BinaryWrite(result)
Catch ex As Exception

End Try
End Sub

No comments:

Post a Comment