Reputation: 17288
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
Reputation: 6636
There are two steps to this.
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
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 :
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
Reputation: 1374
Also you need to set your mime type to "application/pdf" by setting the content-type header.
Upvotes: 0