Link to home
Start Free TrialLog in
Avatar of rock815
rock815

asked on

export to a formatted pdf

I am trying to export data from sql server database to an adobe pdf file using asp.net.  I'm not able to find much documentation on how to accomplish this.  Do you have any references for me to learn how to do this?  Easiest Ways? Etc?

Thank you for any help you can provide.
Avatar of AerosSaga
AerosSaga

This will create a file from the blob value in SQL Server:

Private Sub saveFile(ByVal sFileName As String, sBlobText As String)
    Dim fileStream As IO.FileStream
    Dim streamWriter As IO.BinaryWriter
    Dim filePath As String

    'create a new instance of the file stream object
    'note: if the file does not exist, the constructor will create it
    fileStream = New IO.FileStream(path:=sFileName, mode:=IO.FileMode.OpenOrCreate, access:=IO.FileAccess.Write)

    'create an instance of a character writer
    streamWriter = New IO.StreamWriter(stream:=fileStream)

    'set the file pointer to the end of the file
    streamWriter.BaseStream.Seek(offset:=0, origin:=IO.SeekOrigin.Begin)

    'write a line of text to the end of the file
    streamWriter.Write(value:=sBlobText)

    'apply the update to the file
    streamWriter.Flush()

    'close the stream writer
    streamWriter.Close()

    'close the file stream object
    fileStream.Close()

  End Sub

And this will print the file:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim wrdApp As New Word.Application
    wrdApp.Documents.Open("C:\temp\temp.doc")
    wrdApp.Dialogs.Item(Word.WdWordDialog.wdDialogFilePrint).Show()
    wrdApp.Visible = False
    wrdApp.ActiveDocument.PrintOut = False
    wrdApp.ActiveDocument.Close()
    wrdApp.Quit()
End Sub

Regards,

Aeros
will also print pdf!!
I was under the impression that it is not that easy to do as Aeros has suggested.  I have not tried his code, so who knows it may export the data to PDF.

You may want to check out this link: http://www.aspnetworld.com/articles/2004011801.aspx

That should give you some other options.
Avatar of rock815

ASKER

Anymore information out there on asp.net to pdf?
rock:

You can use Crystal Reports.NET, which comes with Visual Studio .NET.  You design the report just like you would if you were going to print the report in a desktop app.  Crystal's ReportClass has an Export() method that, with a number of property settings, will write the report to a PDF file.  You then use Response.WriteFile() to stream it to the browser.  In my case I delete the file after streaming it to the browser.  All the info you need, with code and all, is here:

http://support.businessobjects.com/communityCS/TechnicalPapers/crnet_web_app_printing.pdf

Also, configuring your target server to support this is a bit of a PIA, but it is manageable.  Everything you will need is at:

http://support.businessobjects.com/communityCS/TechnicalPapers/crnet_deployment.pdf

Also, make sure to go to BusinessObjects website and get the latest hotfixes for Crystal Reports.NET.  Apply them to your dev box and to any target servers AFTER you run the setup project referred to in the crnet_deployment.pdf document linked to above.

I recommend you have an aspx page dedicated solely to managing the retrieval of data, instantiating of the report, exporting to a PDF file and streaming it to the browser.  If someone clicks a button meant to show the report, do a Response.Redirect() to that page with info in the querystring concerning what report will be shown and any parameters that might be needed for the data retrieval.  This will enable them to hit the back button and go back to the page they were on.  If you stream a PDF file from the page that the user clicked a button to get the report, you are still on that page, technically.  If they hit the back button, they will get the previous page, which will be very confusing.

I have done a lot of this.  Once you get the hang of the coding, it will be pretty easy and I think you will find it a very elegant way to handle presenting data nicely in a PDF file.  I'd be happy to throw some code up if you need it.

John
Avatar of rock815

ASKER

John,

Thanks for your help, I think your route is the one I'll take.  Can you email me or post some code that would allow a parameter to be passed to crystal reports?  I'm using the common report parameters that will be input by the user (Start Date & End Date).  This would be most helpful! :-)  If you prefer to email me the code, please send it to afraze@gmail.com

Thanks again!,
Adam
ASKER CERTIFIED SOLUTION
Avatar of jnhorst
jnhorst

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rock...

I just realized that if you follow the comments above and drag a ReportDocument object from the Components toolbar to the page designer and select your report, you will not need to dim a variable to an instance of the report.  That will be done in the automatically generated code.  You just need to call rpt.SetDataSource(theDataSet) and then follow the rest of the code above.

John
Avatar of rock815

ASKER

John,

Thanks for all your help on this, I really appreciate it.  I think I understand where you are going with this, so I can take it from here.. Should I have any issues, maybe I'll drop you an email.

Thanks again,
Adam