Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open report in pdf format

Status
Not open for further replies.

rnooraei

Programmer
Apr 5, 2005
74
CA
Hi
I have this code that open my crystal report. I would like to send it dircet to pdf file not open with crystal report because when I open it in crystal report I get broken link for print, export, ... I tried CRRedist2005_x86.msi and it didn't work. is there any way to do that?
Thanks

Dim crReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument

crReport.Load(Server.MapPath("AppListing.rpt"))
crReport.SetDatabaseLogon("admin", "%%%")
crReport.SetParameterValue(0, Request.QueryString("sDept").ToString)
crReport.SetParameterValue(1, Request.QueryString("sDiv").ToString)
crReport.SetParameterValue(2, Request.QueryString("sSec").ToString)
crReport.SetParameterValue(3, Request.QueryString("sStat").ToString)
crViewer.ReportSource = crReport
 
Report.ASHX
note this is a ASHX file not an aspx file
Code:
<% WebHandler Language="vb" class="Report" %>
Imports [namespaces here]

public class Report
  Inherits HttpHandlerBase

   public property bool IsReusable
     get return true end get
   end property

   public sub ProcessRequest(byval context as httpcontext)
      ReportDocument report = new ReportDocument()
      report.Load("myreport.rpt")
       report .SetDatabaseLogon("admin", "%%%")
       report .SetParameterValue(0, context.Request.QueryString("sDept").ToString)
       report .SetParameterValue(1, context.Request.QueryString("sDiv").ToString)
       report .SetParameterValue(2, context.Request.QueryString("sSec").ToString)
       report .SetParameterValue(3, context.Request.QueryString("sStat").ToString)

       dim stream as Stream = report.ExportToStream(ExportFormatType.PDF);
       dim bytes as byte() = new byte(stream.Length)
       stream.Read(bytes, 0, bytes.Length)
       stream.Dispose()

       context.Response.Buffer = true
       context.Response.ClearContent()
       context.Response.ClearHeaders()
       context.Response.ContentType = "application/pdf"
       context.Response.AppendHeader(Content-Disposition", "inline;filename=report.pdf")
       context.Response.AppendHeader("Content-Length", bytes.Length.ToString)
       context.Response.BinaryWrite(bytes)
       context.Response.Flush()
       context.Response.End()
   end sub
end class
there are known issues with IE6 and pdfs. the method above will work with IE7, IE6 with Adobe Acrobat (not reader) and FireFox. it will automatically open the report without prompting the user for open, save, cancel.

if this is an issue change this line to send as an attachment instead of inline
Code:
context.Response.AppendHeader(Content-Disposition", "attachment;filename=report.pdf")
this will work in IE6. it will prompt the user for action: open, save, cancel.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Jason,
Here is exactly what I have in my VB code. I am also new to ASP.NET so, how could I modify this code base on what you posted for me. you mean I should create a page with ASHX format and not using aspx

Thanks

Partial Class AppListing
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim crReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
' crReport.Load("C:\PeelApps\BAR\AppListing.rpt")
crReport.Load(Server.MapPath("AppListing.rpt"))
crReport.SetDatabaseLogon("admin", "bar")
crReport.SetParameterValue(0, Request.QueryString("sDept").ToString)
crReport.SetParameterValue(1, Request.QueryString("sDiv").ToString)
crReport.SetParameterValue(2, Request.QueryString("sSec").ToString)
crReport.SetParameterValue(3, Request.QueryString("sStat").ToString)
crViewer.ReportSource = crReport
crReport.Export()
End Sub
End Class
 
[ol][li]right click website project.[/li]
[li]Add Item...[/li]
[li]select Generic Handler (Handler.ashx)[/li]
[li]rename Report.ashx[/li]
[li]cut and paste my code above into the file.[/li][/ol]
I did find 1 mistake in my code above.
Replace [tt]Inherits HttpHandlerBase[/tt] with [tt]Inherits IHttpHandler[/tt]. HttpHandlerBase is a comstom class i have. you need to inhert the interface instead.

you will also need to include the relevant namespaces as well.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top