Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to resolve the issue while exporting the data from to PDF in asp.net. Here I am using Itextsharp dill. The issue is the PDF is damaged or corrupted message is coming on opening the PDF after exporting is done.Locally it worked fine. But issue is happening in some other machine hosted.

What I have tried:

Please find the complete code:
MISReport objclass = new MISReport();
try
{

    if (validation() == 0)
    {


        DataSet ds = objclass.getPageAccessReport(ViewState["FDOB"].ToString(), ViewState["TDOB"].ToString(), drdRoleName.SelectedValue);
        DataTable dt = new DataTable();
        if (ds.Tables[0].Rows.Count > 0)
        {
            dt = ds.Tables[0];
            grdExport.DataSource = dt;
            grdExport.DataBind();

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=PageAccesLog_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            grdExport.AllowPaging = false;
            grdExport.DataBind();
            grdExport.RenderControl(hw);
            // grdExport.HeaderStyle.Width = "1";
            // grdExport.HeaderRow.Style.Add("font-size", "10px");
            grdExport.Style.Add("text-decoration", "none");
            grdExport.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdExport.Style.Add("font-size", "8px");
            string strHead = strReportHeading + " FROM " + ViewState["FDOB"].ToString() + " TO " + ViewState["TDOB"].ToString();
            StringBuilder strbn = new StringBuilder();
            //string Imagepath = Common.SITE_URL + "logo_barclays.gif";
            strbn.Append("<span style="float: right; text-align: right; font-weight: bold">Date:" + DateTime.Today.ToString("dd-MMM-yyyy") + "</span><div style="text-align: center; font-weight: bold; background-color: White; width: 100%; font-size: 20px; text-decoration: underline"> " + strHead + "</div> <br>");
            strbn.Append(sw.ToString());
            StringReader sr = new StringReader(strbn.ToString());
            Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }
        else
        {
            Common.showMessage("No Records Found", this);
            return;
        }




    }


}
catch (Exception ex)
{

}
Posted
Updated 25-Apr-18 18:28pm
v2
Comments
[no name] 25-Apr-18 11:41am    
Your 2 "functions" (creating a pdf; sending a pdf), which have been turned into a single mound of mud, should be separated; so they can be tested properly.

Your problem is probably that your pdf is not being created properly in the first place ... but who can tell from this mud?
ranio 26-Apr-18 0:28am    
I have updated the code portion for your better understanding. Locally with the same code updated it is fine. Issue seen after hosting
ranio 26-Apr-18 1:43am    
Solution obtained. It was a path related issue which was rectified and now fine in hosted machine too. Same code worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900