Wednesday, May 18, 2005

Output a PDF file in browser (creates PDF on fly)

Like the way .Net makes easier to do playying with files & objects, the ASP.Net objects are now more cool. Can do a lot of things for you. In my project, i have an request to query a pdf file from a location and then to show it into a browser as a PDF file, infact making it downloadable for a user. The following code do this all. It first make a request via WebClient .Net Framework class to a URL where the pdf is present and then downloads the whole data in a byte array and then output it using Response object. Its quite simple, just follow the following code... In fact what i do, as i request the PDF file from a virtual directory which has integrated windows security, so in making the web client request i also passed the credential of a domain user. ________________________________________________________________ Dim remoteFilePath As String Dim credCachae As New CredentialCache Dim myWebClient As New WebClient ' PDF file path remoteFilePath = http://wajahatabbas.blogspot.com/billing/Bill.pdf" ' Domain user id and pwd Dim networkCredential As New networkCredential("username","password","domain") myWebClient.Credentials = networkCredential ' File saves in a byte array after downloading Dim FileBuffer As Byte() = myWebClient.DownloadData(remoteFilePath) ' Clearing the header content and headers Response.ClearContent() Response.ClearHeaders() ' The following makes a file to be a pdf one Response.ContentType = "application/pdf" 'The following makes the file downloadable, a dialouge will open which ask the user to save it Response.AddHeader("Content-Disposition", "attachment;filename=Billing.Pdf") 'This writes the whole file into the browser Response.BinaryWrite(FileBuffer) Response.Flush() Response.Close() _________________________________________________________________ This is how to create a pdf file on fly .........


Post a Comment

<< Home