Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Here i am trying to convert the content inside html div tag to pdf i found the following error:

Input string was not in a correct format is occur

Here is the code i tried using c#:

C#
<pre lang="c#">public string getWhileLoopData()
{
 string htmlStr = "";
 SqlConnection thisConnection = new SqlConnection("Data Source=VELU-PC\\SQLEXPRESS;Initial Catalog=EEP;Trusted_Connection=True;");
        SqlCommand thisCommand = thisConnection.CreateCommand();
        thisCommand.CommandText = "select * from Product_category";
        thisConnection.Open();
        SqlDataReader reader = thisCommand.ExecuteReader();
        while (reader.Read())
        {
            string id = reader.GetString(6);
            string Name = reader.GetString(3);
            string Pass = reader.GetString(5);
            htmlStr += "<pre lang="HTML"><table><tbody><tr><td><table width="200px"><tr><td align="center"><img src=" + id + " /></td></tr><tr><td align="center">" + Name + "</td></tr></table></td><td><table width="300px"><tr><td align="left" style="border:1px solid blue;border-radius:7px;box-shadow: 10px 0 10px #888888; padding: 8px 6px 0 7px;">Features: <br /><p style="margin-removed 10px;">" + Pass + "</p></td></tr></table></td></tr></tbody></table>
";
}
thisConnection.Close();
return htmlStr;
}
void generatetable()
{
divexcel.Visible = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
divexcel.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 80f, 80f, -2f, 35f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{ }
protected void button_Click(object sender, EventArgs e)
{
generatetable();
}



Here is my html code:

C#
<div id="divexcel"  runat="server">
    <table><tr><td><asp:Button ID="button" runat="server" Text="Submit" 
        onclick="button_Click" /></td></tr></table>
    <table align="center" width="100%">
        <tr>
            <td>
                <table>
                    <tr><td align="center">ID</td></tr>
                    <tr><td align="center">Name </td></tr>
                </table>
            </td>
            <td>
                <table>
                    <tr><td align="left">Features: <br /><p>Pass</p></td></tr>
                </table>
            </td>
            </tr>                       

        <%=getWhileLoopData()%>

    </table>




if i remove styles from htmlStr means its working fine without style.
Posted
Updated 28-Oct-13 20:22pm
v5
Comments
Thanks7872 29-Oct-13 2:00am    
Remove unnecessary code. Post only the code block you faced error with. Clearly point out line where you got this and the value.
velusamyips 29-Oct-13 2:25am    
found the error in the line

htmlStr += "<tr><td><table width='100%'><tr><td align='center'><img src=" + id + " /></td></tr><tr><td align='center'>" + Name + "</td></tr></table></td><td><table border='1' style='border-color:Blue; border-style:solid;border-radius:7px;><tr valign='middle'><td align='left'>Features: <br/><p style='margin-top: 10px;'>" + Pass + "</p></td></tr></table></td></tr>";
velusamyips 29-Oct-13 2:29am    
Here i removed the styles means it will work fine, but i need style.

1 solution

Use this

-------------
htmlStr += "<table><tbody><tr><td><table width=\"200px\"><tr><td align=\"center\"><img src=" + id + " /></td></tr><tr><td align=\"center\">" + Name + "</td></tr></table></td><td><table width=\"300px\"><tr><td align=\"left\" style=\"border:1px solid blue;border-radius:7px;box-shadow: 10px 0 10px #888888; padding: 8px 6px 0 7px;\">Features: <br /><p style=\"margin-removed 10px;\">" + Pass + "</p></td></tr></table></td></tr></tbody></table>";
 
Share this answer
 
Comments
velusamyips 29-Oct-13 3:14am    
error occurred: Input string was not in a correct format
Sakshii Arora 22-Jul-14 8:12am    
i want to do the same thing convert my html table to pdf.. but confused how to do so please anybody can help .i tried this code above but it gives error that input string is not in correct format..
here is my c# code :-
pdfPannel.Visible = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pdfPannel.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 80f, 80f, -2f, 35f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
and my html code is:
<asp:Panel ID="pdfPannel" runat="server" Height="466" Width="728">
<table cellpadding='2' cellspacing='2' width='100%'>
<tr><td colspan='100%'>
</td></tr>
<tr align="center" ><th colspan='100%' >INVOICE</th></tr>
<tr><td colspan='100%'>
</td></tr>
<tr><th colspan='4' align="right" >Bill No    :-</th><td align="left">njnn</td></tr>
<tr><th colspan='4' align="right" >Bill Date :-</th><td align="left">jnjnj</td></tr>
<tr><th colspan='100%'><br /></th></tr>
<tr><th align="left" >Customer Name:- </th><td colspan='4' align="left">ghgh</td></tr>
<tr><td colspan='100%'>
</td></tr>
<tr><th width='10%' align="left">S.no</th><th align="left">Particulars</th><th align="left">Quantity</th><th align="left">Rate</th><th align="left">Amount</th></tr>
<tr><td width='10%'>1.</td><td>pen</td><td>5</td><td>10</td><td>50</td></tr>
<tr><th colspan='100%'><br /></th></tr>
<tr><th colspan='100%'><br /></th></tr>
<tr><td colspan='100%'>
</td></tr>
<tr><th colspan='4' align='right'>Total Quantity :-</th><td>5.00</td></tr>
<tr><th colspan='4' align='right'>Sub Total         :-</th><td>50.00</td></tr>
<tr><th colspan='4' align='right'>Discount @      :-</th><td>0.00%</td></tr>
<tr><th colspan='4' align='right'>Total Amount   :-</th><td>50.00</td></tr>
<tr><td colspan='100%'>
</td></tr> </table>

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