Save Crystal Report as PDF

Last post 06-29-2009 1:59 PM by WanderingEye. 6 replies.

Sort Posts:

  • Save Crystal Report as PDF

    06-27-2009, 7:44 PM
    • Member
      33 point Member
    • WanderingEye
    • Member since 02-19-2009, 11:04 AM
    • Posts 71

    Hey all,

    I'm wondering if its possible to generate a crystal report from a dataset and save as a PDF file. I don't want to display Crystal Reports in the browser - want it all done in the background....just want to create the report from the dataset and save it as a PDF file to the server. Is this possible? If so, could you provide some examples.

    Visual Studio 2008 (.net 3.5) 

    Much appreciated.

  • Re: Save Crystal Report as PDF

    06-27-2009, 8:53 PM
    • Participant
      1,768 point Participant
    • nmreddy83
    • Member since 01-21-2009, 6:00 PM
    • India
    • Posts 316

    in this case you can use .net Report Viewer the same can save or render in whatever format you want

    http://msdn.microsoft.com/en-us/library/bb386063.aspx


    refer Report Viewer concepts from the above link

    ***Hope this helps you***
    ***Please mark as answer if this helps you. ***

    thank you,
    -nm reddy
  • Re: Save Crystal Report as PDF

    06-29-2009, 8:38 AM

    WanderingEye this is the code to export the crystal report into PDF file, if u dont want to display the report then u just bind the data to report and make it visible to false and apply this code

    private void Button1_Click(object sender, System.EventArgs e)
    {
    MemoryStream oStream;
    // using System.IO
    oStream = (MemoryStream)
    report.ExportToStream(
    CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    Response.Clear();
    Response.Buffer=
    true;
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(oStream.ToArray());
    Response.End();
    }

     

    Any doubts please feel free to ask me.

    If this post is answer of your question then don't forgot to Click Mark As Answer.

    Thanks & Regards,
    J.Jeyaseelan
  • Re: Save Crystal Report as PDF

    06-29-2009, 10:19 AM
    • Member
      33 point Member
    • WanderingEye
    • Member since 02-19-2009, 11:04 AM
    • Posts 71

    Thanks  jevaseelan

    Your method appears to save the file to memory....I need to save the file to disk on the server for later use. Any suggestions for that?

  • Re: Save Crystal Report as PDF

    06-29-2009, 11:16 AM
    • Member
      33 point Member
    • WanderingEye
    • Member since 02-19-2009, 11:04 AM
    • Posts 71

    Just bumped into another issue....my asp.net webiste is now complaining about the report object: 

    Dim rpt as New CrystalReport1 - "Type 'CrystalReport1 is not defined"

    I've added the report file (CrystalReport1.rpt) to the website.....I was not having this issue when testing as Windows Forms application. What gives? I've done some searching on the web and have not found a solution for this.

    .aspx file contains: 

    <%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>

    .aspx.vb contains:

    Imports CrystalDecisions.Shared, CrystalDecisions.CrystalReports.Engine

    Any help woud be greatly appreciated.

     

  • Re: Save Crystal Report as PDF

    06-29-2009, 1:27 PM
    • Member
      33 point Member
    • WanderingEye
    • Member since 02-19-2009, 11:04 AM
    • Posts 71

    So, I'm guessing a way around the second issue would be to create a generic ReportDocument object and point it to the report file? Is there any issues with using this method? My quick little PDF solution is turning out not to be so quick :>)

    For example:

    Dim doc as ReportDocument

    Dim FileName as String = Server.MapPath("MyReprt.rpt")

    doc.Load(fileName)

     

    Still need assistance with saving the file - Thanks!

  • Re: Save Crystal Report as PDF

    06-29-2009, 1:59 PM
    Answer
    • Member
      33 point Member
    • WanderingEye
    • Member since 02-19-2009, 11:04 AM
    • Posts 71

    Okay, figured out the file part. Here's the code in case anyone needs it. I'm going to post more than just the export (to pdf) portion as I know there are also issues with Crystal prompting for username/password when opening the DataSet. I am not having that issue with this code. Hopefully I can save someone the time I have spent on this. This uses the a strongly typed DataSet...I believe they call this the "push" method.

    Now All I need to do is figure out how to format the report so it looks niceTongue out

    Imports CrystalDecisions.Shared
    Imports CrystalDecisions.Reporting
    Imports System.Data.SqlClient
    Imports System.Data
    Imports System.IO

    Partial Class _Default
        Inherits System.Web.UI.Page
        Dim doc As New ReportDocument

    Public Sub CreatePDF(ByVal formid As String)
            Dim myConnection As SqlConnection
            Dim MyCommand As New SqlCommand()
            Dim myDA As New SqlDataAdapter()
            Dim myDS As New MyDataSet() 'The DataSet you created.

            ' Workaround for strongly typed method not working
            Dim fileName As String = Server.MapPath("CrystalReport.rpt") 
            doc.Load(fileName)

            Try

                myConnection = New SqlConnection("Data Source=server;Initial Catalog=DB;Persist Security Info=True;User ID=userid;Password=pass")

                MyCommand.Connection = myConnection
                MyCommand.CommandText = "SELECT * FROM Table WHERE  (FormID = @formid)"
                MyCommand.Parameters.Add(New SqlParameter("@formid", formid))
                MyCommand.CommandType = CommandType.Text
                myDA.SelectCommand = MyCommand

                myDA.Fill(myDS, "MyDataTable")
                doc.SetDataSource(myDS)
                CrystalReportViewer1.ReportSource = doc
                CrystalReportViewer1.Visible = False

               
            Catch Ex As Exception
            End Try
        End Sub

    Protected Sub btnConvert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvert.Click

            Try
                Dim ExpOptions As ExportOptions
                Dim DiskFileDestOpts As New DiskFileDestinationOptions
                Dim FormatTypeOpts As New PdfRtfWordFormatOptions
                DiskFileDestOpts.DiskFileName = "D:\Temp\Test.pdf"
                ExpOptions = doc.ExportOptions
                With ExpOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                    .DestinationOptions = DiskFileDestOpts
                    .FormatOptions = FormatTypeOpts
                End With
                doc.Export()
            Catch Ex As Exception
                MsgBox(Ex.ToString)
            End Try
        End Sub

     

Page 1 of 1 (7 items)