Solved

export to a formatted pdf

Posted on 2004-08-16
9
429 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:rock815
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
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
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
will also print pdf!!
0
 
LVL 8

Expert Comment

by:thrill_house
Comment Utility
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.
0
 
LVL 1

Author Comment

by:rock815
Comment Utility
Anymore information out there on asp.net to pdf?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
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
0
 
LVL 1

Author Comment

by:rock815
Comment Utility
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
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 125 total points
Comment Utility
rock...

I would pass the parameter info in the querystring of the Response.Redirect() and then use that info either to set command parameters for a stored procedure or the create the SQL statement (if you're using SQL Server I would recommend creating a stored procedure that takes the start and end dates and returns just the data needed).

It would look something like this in the page where the user clicks a button or something to get to the report (this assumes you have text boxes or some other control for the start and end dates):

... in the click event:
Dim strURL As String = yourPDFExportPage.aspx?rpt=yourreportname&start=" & txtStart.Text & "&End=" & txtEnd.Text
Response.Redirect(strURL)

Then in yourPDFExportPage.aspx:

Protected Sub Page_Load()
' get data.
Select Case Request.QueryString("rpt")
     Case "thisreport"
          thisReportsDataAdapter.SelectCommand.Parameters(1).Value = Request.QueryString("start")
          thisReportsDataAdapter.SelectCommand.Parameters(2).Value = Request.QueryString("end")
          thisReportsDataAdapter.Fill(theReportsDataTable)
End Select

If Not Page.IsPostBack
     ' set export options.
     Dim expOpts As ExportOptions()
     Dim destOpts As New DiskFileDestinationOptions()

     ' if the file is going to be saved, you might put this within the select case block to give it a different name
     ' depending on which report you are exporting.  since I delete the file after writing it to the browser, I
     ' usually put this here since the name of the file really does not matter if it is going to be deleted right away.
     ' make sure write permissions are granted to the user (or the anonymous account) on the folder to which the file
     ' will be saved.
     destOpts.DiskFileName = "theFileName"
     
     ' which report?
     Select Case Request.QueryString("rpt")
          Case "thisreport"
               ' the rptThisReport class will be defined when you drag a ReportDocument from the toolbar to your page
               ' designer (the ReoprtDocument usually shows up in the Component toolbar).  A dialog will ask which report
               ' you want.  Select the report and deselect the "Generate Cached Strongly Typed Report" checkbox.  The tray
               ' at the bottom of the page designer will show the instance.  right click it, select properties, and change the name
               ' to rptThisReport (or whatever you want it to be).
               Dim rpt As New rptThisReport()

               ' set the datasource with the DataSet that contains the datatable that is the basis for the report.  you populated
               ' this datatable at the beginning of the Page_Load event with the report's data.
               rpt.SetDataSource(theDataSet)

               ' set the export options.
               expOpts = rpt.ExportOptions
               expOpts.ExportDestinationType = ExportDestinationType.DiskFile;
               expOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
               expOpts.DestinationOptions = destOpts;                  

               ' Export.
               rpt.Export()

          Case "thatReport"
               ' instantiate the report.
               Dim rpt As New rptThatReport()

               '... and so on.

               ' Export.
               rpt.Export()

     End Select

     ' write the report to the broswer.
     Response.ClearContent()
     Response.ClearHeaders()
     Response.ContentType = "application/pdf"
     Response.WriteFile(theFileName)
     Response.Flush()
     Response.Close()

     ' delete file.  You may not want to do this if the report needs to be saved.
     System.IO.File.Delete(theFileName)
End If

End Sub

God Luck
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
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
0
 
LVL 1

Author Comment

by:rock815
Comment Utility
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now