Penguen
Penguen

Reputation: 17288

how to open data in browser PDF format?

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

Upvotes: 1

Views: 1843

Answers (3)

Jason Coyne
Jason Coyne

Reputation: 6636

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.

Upvotes: 0

Canavar
Canavar

Reputation: 48088

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.

Upvotes: 5

BYK
BYK

Reputation: 1374

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

Upvotes: 0

Related Questions