Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey All ..

when i try to export Datalist or gridview to pdf..ie exported without images !!!

this is my code :

Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
         "attachment;filename=Products.pdf");
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        DataList1.DataBind();
        
        DataList1.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, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
Posted
Updated 20-Feb-17 23:42pm

You may need to make sure that your images are referenced by absolute and not relative paths in your html. This is due to the fact that most pdf writers create a temporary copy of your html input in a temp directory before parsing which then causes the relative paths to fail.
 
Share this answer
 
Comments
shms_rony 8-Jul-11 9:05am    
datalist images get from path in database
Try This:-

iTextSharp.text.Table table = new iTextSharp.text.Table(grdCust_accot_stmt.Columns.Count);
        table.Cellpadding = 2;
        table.Width = 100;
        grdCust_accot_stmt.AllowPaging = false;
        this.grd_Account_stmt();       
        //Transfer rows from GridView to table
        for (int i = 0; i < grdCust_accot_stmt.Columns.Count; i++)
        {
            string cellText = Server.HtmlDecode(grdCust_accot_stmt.Columns[i].HeaderText); 
            iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
            cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#e8eff3"));
            table.AddCell(cell);
        }
 
        for (int i = 0; i < grdCust_accot_stmt.Rows.Count; i++)
        {
            if (grdCust_accot_stmt.Rows[i].RowType == DataControlRowType.DataRow)
            {
                for (int j = 0; j < grdCust_accot_stmt.Columns.Count; j++)
                {
                    string cellText = Server.HtmlDecode
                                      (grdCust_accot_stmt.Rows[i].Cells[j].Text);
                    iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
 
                    //Set Color of Alternating row
                    if (i % 2 != 0)
                    {
                        cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#e8eff3"));
                    }
                    table.AddCell(cell);
                }
            }
        }
        Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 10f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(table);
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" +"filename=Reports.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();
 
Share this answer
 
Here is an article that explains how: iTextBox - Convert datalist and image in PDF[^]
 
Share this answer
 

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