tags:

views:

248

answers:

3

i get data (price of volvo cars) my xml or sql database, i have "xyz.aspx" page. How can i open pdf in a web browser. But i want to learn how can i open pdf(data from sql datatable ) xyz.aspx or data in another.aspx...

Sample: Volvos'prices pdf

+3  A: 

Let's say that you have a table like that :

CREATE TABLE PDFTable (
    [PDFID] [bigint] IDENTITY (1, 1) NOT NULL ,
    [PDFFile] [varbinary(max)]
)

and you store your PDF files in this table in binary format. And you want to expose your PDF files to your visitors like that :

http://www.mysite.com/getpdf.aspx?pdfid=12

Here is a sample for reading binary pdf data from database and writing it to response stream :

// PageLoad event of your getpdf.aspx page :
long pdfId= Convert.ToInt64(Request.QueryString["pdfid"]);

using (var conn = new SqlConnection(connectionString))
{
    using (var command = new SqlCommand(
     "SELECT PDFFile FROM PDFTable WHERE PDFId = @PDFID", conn))
    {
     command.Parameters.Add("@PDFID", SqlDbType.Int).Value = pdfId;
     conn.Open();

     Response.ContentType = "application/pdf";
     Response.BinaryWrite((byte[]) command.ExecuteScalar());
    }
}

This page will result your client to open pdf in his/her browser.

Canavar
Can you give me some details? thanks again...
Phsika
A: 

There are two steps to this.

1) Produce a pdf containing the data. 2) Deliver the pdf to the browser

For step 1) there are many libraries available for pdf writing. Just pick one. Doing your own PDF generation is probably not viable, as the format is very complicated.

For step 2) set the Content.Headers to included a mime/pdf contentType, and use BinaryWrite to putout the pdf data to the browser.

Jason Coyne
A: 

Also you need to set your mime type to "application/pdf" by setting the content-type header.

BYK