Pdf report not working with browser [Resolved]

Posted by Klbaiju under ASP.NET on 2/6/2016 | Points: 10 | Views : 1603 | Status : [Member] | Replies : 1
Hi following is my code for creating a pdf report.

string strQuery = "select stdid,class name1,name2,surname,fname,mname,section,room,house from stdmst1  where class=10 ";
//SqlCommand cmd = new SqlCommand(strQuery, con);
SqlCommand cmd = new SqlCommand(strQuery);

DataTable dt = GetData(cmd);
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();


the code doesn't contains any error.

problem is it will not download to browser or directly views in browser.

my requirement is to view in browser directly.

it will work only if there exists a third party downloadmanager like IDM.

How to solve this

Regards

Baiju




Responses

Posted by: Sheonarayan on: 2/6/2016 [Administrator] HonoraryPlatinum | Points: 50

Up
1
Down

Resolved
Dear Klbaiju,

The direct view in the browser depends on whether browser is capable of showing .ppf inside or not. For example, Google chrome browser should be able to show the .pdf.

However, it also depends on how your Content-disposition line is written.

If you add the "content-disposition" line with inline, it should be opening it in the browser.

Response.AppendHeader("Content-Disposition", "inline; filename=DataTable.pdf");

Thanks



Regards,
Sheo Narayan
http://www.dotnetfunda.com

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response