2008/08/18

Web Export to PDF with SOAP, Forcing Download of PDF: an example

Author: Pinolian

I needed to have my solution do the following, primarily due to our third party fax software's requirements:



In one submit button on an ASPX page, I needed to do the following:



1. Generate a report programattically on the intranet, based on parameters submitted in by a web form.

2. Export the report to PDF automatically.

3. Name the PDF programattcally in such a way that the 3rd party Fax software could use it automatically.

4. Have adobe launch automatially on the client computer with the PDF, so all the user had to do was print within 2 key strokes.



This is what I did:



I had the report working fine when I was debugging it. So the report itself was good to go. With this example its assumed you also have a report that is working fine.



The submit button in question on the web form just did a redirect to an empty web form. I deleted all of the HTML in the page, leaving the page language stuff at the top.



You have to make a web reference to your report server, in your web solution . Just right click on your web solution, click add web reference, and in the box, on the page that pops up, put in your report server as follows:



http://yourservername/ReportServer/ReportService.asmx



Give it a name, mine was named hbs2. put that name in the box on the lower right. Click ok.



Add the following imports:



Imports System
Imports System.IO 'for writing the PDF
Imports System.Web.Services.Protocols ' for soap exception



Where ever you write the report to on the web server you have to grant WRITE permissions to the User Account that is running the aspnet_wp.exe process.

So in my example, I have given the ASPNET account write permissions on the web server to the C:\Temp folder.



In the page load I have the following code:



Dim format As String = "PDF"

'This is the name/location where the report will be generated on the web server

'the best part is that you can name the actual file anything you want in this code

'give write permissions to this folder:
Dim fileName As String = "C:\Temp\Tester.PDF"



'name of report on the report server that you want to use

'its basically / project name / report name
Dim reportPath As String = "/NDPReports/Report1"

Dim historyID As String = Nothing
Dim deviceInfo As String = Nothing
Dim showHide As String = Nothing


'results is the file stream that we will be using

Dim results() As Byte
Dim encoding As String
Dim mimeType As String



'hbs2 is the name of my web ref to the reportserver,

Dim warnings() As hbs2.Warning = Nothing

Dim reportHistoryParameters() As hbs2.ParameterValue = Nothing

Dim streamIDs() As String = Nothing



'The actual reporting service

Dim rs As New hbs2.ReportingService


rs.Credentials = System.Net.CredentialCache.DefaultCredentials



'report parameters, passed in by querystring

Dim Parameters(1) As hbs2.ParameterValue


Parameters(0) = New hbs2.ParameterValue
Parameters(0).Name = "Initials"
Parameters(0).Value = Request.QueryString("Initials")


Parameters(1) = New hbs2.ParameterValue
Parameters(1).Name = "WorkDate"
Parameters(1).Value = Request.QueryString("WorkDate")



'this is where the report is generated

results = rs.Render(reportPath, format, historyID, deviceInfo, Parameters, _
Nothing, showHide, encoding, mimeType, _
reportHistoryParameters, warnings, streamIDs)



'Write the file to the webserver, using what ever name you want!

Dim stream As FileStream = File.OpenWrite(fileName)
stream.Write(results, 0, results.Length)
stream.Close()

DownloadFile(fileName, True)

Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"


------This is the Download file function that forces the download------------



Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
Dim path As Path
Dim fullpath = path.GetFullPath(fname)
Dim name = path.GetFileName(fullpath)
Dim ext = path.GetExtension(fullpath)
Dim type As String = ""

If Not IsDBNull(ext) Then
ext = LCase(ext)
End If

Select Case ext
Case ".pdf", ".html"
type = "Application/pdf"
Case ".txt"
type = "text/plain"
Case ".doc", ".rtf"
type = "Application/msword"
Case ".csv", ".xls"
type = "Application/x-msexcel"
Case Else
type = "text/plain"
End Select

If (forceDownload) Then
Response.AppendHeader("content-disposition", "attachment; filename=" + name)
End If
If type <> "" Then
Response.ContentType = type
End If

Response.WriteFile(fullpath)
Response.End()

End Sub





Now here is the thing.. I needed the download file function because the Save Open dialogue box allowed me to name the file exactly as I wanted to. If you just force adobe to open with



Response.BinaryWrite(results)
Response.ContentType = "application/pdf"



Adobe will open, but the name of the file will include the entire path on the webserver, as opposed to the pristine name we needed (something like @201 name @@202 1115553333@.pdf )



Without the extra code, and the extra click the 3rd party fax stuff would have been broken, trying to send a fax using C:\Temp\@201 name @@202 1115553333 @.pdf instead



I hope this code works out for you. It took me about a day to put it together. The book certainly helped me, and I want to give credit where credit is due. However, forcing the download and working with the parameters in code were areas I had to research myself (btw if you need a null value, just pass in an object set to nothing).



Good Luck



-Pinolian

Tidak ada komentar:

News Update

Anda mau membeli atau mencari Buku?


Masukkan Code ini K1-39C35C-1
kutukutubuku.com
Anda malas datang langsung ke toko buku pada saat anda ingin membeli buku,gak usah khawatir anda bisa langsung pesan buku yang anda inginkan hanya dengan melalui website http://kutukutubuku.com.Anda cukup mengisi identitas diri anda dan buku yang anda inginkan.

Berita IT dan Artikel

News Liputan6

Computer Science

Gallery

  Gallery (Friend's 4ever )