How to convert html to pdf using c#?

     In this section we are going to see how to convert the pure html and css code to pdf using itextsharp and itexsharp.xmlworker dll's.


Download itextsharp.xmlworker.dll
Downlaod itextsharp.dll

Note:
     You should use the same version of the itextsharp.dll and itextsharp.xmlworker.dl

If you not use the same version you will see like the following error.
 Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1705: Assembly 'itextsharp.xmlworker, Version=5.5.8.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' uses 'itextsharp, Version=5.5.8.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' which has a higher version than referenced assembly 'itextsharp, Version=5.5.0.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'

Source Error:


[No relevant source lines]

Source File:    Line: 0


itextsharp.xmlworker and itextsharp error higher version
Client Side code:

<asp:HiddenField runat="server" ID="selectedhtml" />

<asp:ImageButton ID="lnkPDF" ImageUrl="~/App_Themes/Blue/images/adobe.jpg" ToolTip="Export to PDF"
                        runat="server" OnClick="lnkPDF_Clicked"></asp:ImageButton>&nbsp;&nbsp;&nbsp;

<div id="testdiv">
<div id="tdiv1">
<img src="sample.jpg" />
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Merbin</td>
<td>29</td>
</tr>
<tr>
<td>Franklin</td>
<td>30</td>
</tr>
<tr>
<td>Justus</td>
<td>45</td>
</tr>
</table>
</div>
</div>


 <script>

     var btnobj = document.getElementById("<%=lnkPDF.ClientID%>");
     btnobj.addEventListener("click", myfun);

     function myfun() {

             document.getElementById('<%=selectedhtml.ClientID%>').value = document.getElementById('testdiv').innerHTML.replace(/(<img[^>]+)/g, "$1 /");

         }

</script>  


HTML code are converted into xhtml and after only it will process so we have some limitation eg) you should need to close the image tag and etc.
If you are get the innerHTML content the image close tag won't get you so you can replace the image close tag by using the following line.

document.getElementById('<%=selectedhtml.ClientID%>').value = document.getElementById('testdiv').innerHTML.replace(/(]+)/g, "$1 /");


Server Side Code:
protected void lnkPDF_Clicked(object sender, EventArgs e)
{

Document Doc;
Doc = new Document(PageSize.A4, 10f, 10f, 50f, 20f);

string filename = "PaySlip";
string outXml = selectedhtml.Value;
outXml = "<style>#tdiv1{background:red;color:white;}</style>" + outXml;
outXml = outXml.Replace("px", "");
outXml = outXml.Replace("<br>", "<br/>");

MemoryStream memStream = new MemoryStream();
TextReader xmlString = new StringReader(outXml);
using (Document document = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, memStream);
document.Open();
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(outXml);
MemoryStream ms = new MemoryStream(byteArray);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, ms, System.Text.Encoding.UTF8);
document.Close();
}

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(memStream.ToArray());
Response.End();
Response.Flush();
}



No need to put the px in styles, so you can replace the px to "" value by using the following line.
outXml = outXml.Replace("px", "");

innerHTML won't close the <br> tag so you should need to replace the <br> tag to <br/>.
outXml = outXml.Replace("<br>", "<br/>");



View Generated PDF file




Tags:
convert pdf to html in allinworld99, css not support while generating pdf from html code in allinworld99, background color not supported in htmlworker in allinworld99, how to convert htmlwoker to xmlworker in allinworld99,

0 nhận xét:

Đăng nhận xét