Hi, can anyone out there help!


I have a default form and a button. On click of that button I want to export a particular crystal report(without actually displaying it) to pdf format and store it in a folder named as temp. Is it possible?

Thanks in advance,

Recommended Answers

All 10 Replies

Hi,

You can find an example how to convert a Crystal report to PDF file, here.

Hi,

I tried the code but its showing a long list of errors in the line that reads
CrReport.Export() ....... :(

Hi i use

Try
    Dim rep As New ReportDocument
    rep.ExportToDisk(ExportFormatType.PortableDocFormat, "Your File Location & Name")
    Catch ex As Exception
    MsgBox(ex)
End Try

Hope it works and remeber you imports...

Hi Dragon840215,

Thanks for your prompt response. But it's still giving an error. I am still not sure about the source of the problem.

I tried using your code. In place of file name and location , I placed the location of the folder to which I want to export the newly converted pdf file. But the folder is empty, so I gave an arbitrary name like "abc.pdf".

Its displaying the following error

CrystalDecisions.CrystalReports.Engine.LoadSaveReportException: Invalid report file path.
at CrystalDecisions.CrystalReports.Engine.EngineExceptionUtils.DoThrowException(String message, EngineExceptionErrorID id)
at CrystalDecisions.CrystalReports.Engine.ExceptionThrower.ThrowEngineException(String messageID, EngineExceptionErrorID id)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(String filename, OpenReportMethod openMethod, Int16 parentJob)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.EnsureLoadReport()
at CrystalDecisions.CrystalReports.Engine.ReportDocument.get_FormatEngine()
at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToDisk(ExportFormatType formatType, String fileName)
at Esol.MyFormName.Button1_Click(Object sender, EventArgs e) in D:\SS_CRM\SS_CRM\SS_CRM\MyFormName.vb:line 301

According to the error you are getting the 1st line gives you the first error.

CrystalDecisions.CrystalReports.Engine.LoadSaveReportException: Invalid report file path.

Sorry if it was my bad but you need to also insert this line of code

rep.Load("Path to your Report Document")
rep.Close()

So your end code should look like this :

Try
Dim rep As New ReportDocument
rep.Load("Path to your Report Document & Name")
rep.ExportToDisk(ExportFormatType.PortableDocFormat, "Your File Location & Name To Export To")
rep.Close()
Catch ex As Exception    
MsgBox(ex)
End Try

Sorry was my bad not getting all the code in 1st reply

Hi,

I am using exactly these lines of code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Try

            Dim RepPayReq As New ReportDocument 'RepPayReq is the name of my report

            RepPayReq.Load("D:\SS_CRM\SS_CRM\SS_CRM\RepPayReq.vb") 'path of the report

            RepPayReq.ExportToDisk(ExportFormatType.PortableDocFormat, "D:\SS_CRM\SS_CRM\temp\abc.pdf")

            RepPayReq.Close()

        Catch ex As Exception

            TextBox1.Text = ex.ToString

        End Try

    End Sub

But it's still giving the following error


System.Exception: Load report failed. ---> System.Runtime.InteropServices.COMException (0x8000020D): Unable to load report.
at CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocumentClass.Open(Object& DocumentPath, Int32 Options)
at CrystalDecisions.ReportAppServer.ReportClientDocumentWrapper.Open(Object& DocumentPath, Int32 Options)
at CrystalDecisions.ReportAppServer.ReportClientDocumentWrapper.EnsureDocumentIsOpened()
--- End of inner exception stack trace ---
at CrystalDecisions.ReportAppServer.ReportClientDocumentWrapper.EnsureDocumentIsOpened()
at CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(String filename, OpenReportMethod openMethod, Int16 parentJob)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(String filename)
at Esol.FrmPayReq.Button1_Click(Object sender, EventArgs e) in D:\SS_CRM\SS_CRM\SS_CRM\FrmPayReq.vb:line 303

Is your App a 64bit or 32bit ?

In Line 9 of your report is says

RepPayReq.Load("D:\SS_CRM\SS_CRM\SS_CRM\RepPayReq.vb")

There is a .vb extension. It should be a .rpt extension

Application is 32 bit. I changed the extension to .rpt and made sure the path is correct. Still it is giving the following error.

CrystalDecisions.CrystalReports.Engine.LogOnException: Logon failed.
Details: 28000:[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'sa'.
Error in File C:\Users\Raghu\AppData\Local\Temp\RepPayReq {2424761D-3A10-4E1C-A948-907B196629F2}.rpt:
Unable to connect: incorrect log on parameters. ---> System.Runtime.InteropServices.COMException (0x8004100F): Logon failed.
Details: 28000:[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'sa'.
Error in File C:\Users\Raghu\AppData\Local\Temp\RepPayReq {2424761D-3A10-4E1C-A948-907B196629F2}.rpt:
Unable to connect: incorrect log on parameters.
at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext)
at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
--- End of inner exception stack trace ---
at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)
at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToDisk(ExportFormatType formatType, String fileName)
at Esol.FrmPayReq.Button1_Click(Object sender, EventArgs e) in D:\SS_CRM\SS_CRM\SS_CRM\FrmPayReq.vb:line 305

Here is code that I use all the time, to export a crystal report to a pdf
from a webpage.

Imports CrystalDecisions.CrystalReports.Engine
    Imports CrystalDecisions.Shared
    Imports CrystalDecisions.Web.Report

    Private m_Rpt As ReportDocument = New ReportDocument
    Private m_ReportPath As String

    Dim connection As IConnectionInfo
    Dim userID As String = "sa"
    Dim password As String = "mypassword"

    m_Rpt.Load(m_ReportPath)
    m_Rpt.SetDatabaseLogon(userID, password)
    Dim subreport As ReportDocument
    For Each subreport In m_Rpt.Subreports
        For Each connection In subreport.DataSourceConnections
            connection.SetLogon(userID, password)
        Next
    Next

Now to decifer some.
The imports, obvious.
Two variables one for the path one for the report.
The path must be a file based path. It can be a UNC path.

Next setup the connection params.
Load the report, then set the login params for the database.
The loop is for all subreports within the main report.
You can include these even if the report does not contain subreports

Now we setup the export part. This below is its own procedure in my
world.

Dim crExportOptions As ExportOptions
        crExportOptions = m_Rpt.ExportOptions
        With crExportOptions
            Select Case Request.QueryString("format") 'rptOptions.SelectedItem.ToString
                Case "PDF"
                    .FormatOptions = New PdfRtfWordFormatOptions()
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                Case "Word"
                    .FormatOptions = New PdfRtfWordFormatOptions()
                    .ExportFormatType = ExportFormatType.WordForWindows
                Case "Excel"
                    .FormatOptions = New ExcelFormatOptions
                    .ExportFormatType = ExportFormatType.Excel
                Case "ExcelDO"
                    .FormatOptions = New ExcelFormatOptions
                    .ExportFormatType = ExportFormatType.ExcelRecord
            End Select
            .ExportDestinationType = ExportDestinationType.NoDestination
        End With
        Dim req As ExportRequestContext = New ExportRequestContext()
        req.ExportInfo = crExportOptions
        Dim st As System.IO.Stream
        st = m_Rpt.FormatEngine.ExportToStream(req)
        Response.ClearContent()
        Response.ClearHeaders()
        Select Case Request.QueryString("format")
            Case "PDF"
                Response.ContentType = "application/pdf"
            Case "Word"
                Response.ContentType = "application/msword"
            Case "Excel"
                Response.ContentType = "application/x-msexcel"
            Case "ExcelDO"
                Response.ContentType = "application/x-msexcel"
        End Select
        Dim b(st.Length) As Byte
        st.Read(b, 0, st.Length)
        Response.BinaryWrite(b)
        Response.End()

Once again to decifer:
I am using a web page for the interface so I am selecting the
exportoptions based on what the user wants.
The bottom half of this sub is for exporting to a web page and
getting the pdf to open in the page.

I think if you just save the st stream to disk will work fine.

I left out the code where I am passing params to the report.

HTH
Ron

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.